Skip to content

Commit cd06146

Browse files
schiller-manuelSeanCassierelachlancollins
authored
feat(vite): add beforeWriteDeclarationFile (#215)
* feat(vite): add beforeWriteDeclarationFile * chore(vite-config): add changeset * chore(config): add a changeset for `@tanstack/config` as well --------- Co-authored-by: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> Co-authored-by: Lachlan Collins <1667261+lachlancollins@users.noreply.github.com>
1 parent 40362e7 commit cd06146

4 files changed

Lines changed: 32 additions & 8 deletions

File tree

.changeset/clean-ways-speak.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/vite-config': minor
3+
---
4+
5+
add `beforeWriteDeclarationFile` callback

.changeset/odd-planets-win.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/config': minor
3+
---
4+
5+
add `beforeWriteDeclarationFile` callback

packages/vite-config/src/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export type Options = {
1515
tsconfigPath?: string
1616
/** Additional dependencies to externalize if not detected by `vite-plugin-externalize-deps` */
1717
externalDeps?: Array<string | RegExp>
18+
/** Hook called prior to writing each declaration file; allows to transform the content */
19+
beforeWriteDeclarationFile?: (filePath: string, content: string) => string
1820
}
1921

2022
/** https://tanstack.com/config/latest/docs/vite */

packages/vite-config/src/index.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ export const tanstackViteConfig = (options) => {
5050
module: 99, // ESNext
5151
declarationMap: false,
5252
},
53-
beforeWriteFile: (filePath, content) => ({
54-
filePath,
55-
content: ensureImportFileExtension({ content, extension: 'js' }),
56-
}),
53+
beforeWriteFile: (filePath, content) => {
54+
content =
55+
options.beforeWriteDeclarationFile?.(filePath, content) || content
56+
return {
57+
filePath,
58+
content: ensureImportFileExtension({ content, extension: 'js' }),
59+
}
60+
},
5761
afterDiagnostic: (diagnostics) => {
5862
if (diagnostics.length > 0) {
5963
console.error('Please fix the above type errors')
@@ -72,10 +76,18 @@ export const tanstackViteConfig = (options) => {
7276
module: 1, // CommonJS
7377
declarationMap: false,
7478
},
75-
beforeWriteFile: (filePath, content) => ({
76-
filePath: filePath.replace('.d.ts', '.d.cts'),
77-
content: ensureImportFileExtension({ content, extension: 'cjs' }),
78-
}),
79+
beforeWriteFile: (filePath, content) => {
80+
content =
81+
options.beforeWriteDeclarationFile?.(filePath, content) ||
82+
content
83+
return {
84+
filePath: filePath.replace('.d.ts', '.d.cts'),
85+
content: ensureImportFileExtension({
86+
content,
87+
extension: 'cjs',
88+
}),
89+
}
90+
},
7991
afterDiagnostic: (diagnostics) => {
8092
if (diagnostics.length > 0) {
8193
console.error('Please fix the above type errors')

0 commit comments

Comments
 (0)