@@ -5,6 +5,27 @@ import dts from 'vite-plugin-dts'
55import packageJson from './package.json'
66import type { Options } from '@tanstack/config/vite'
77
8+ function ensureImportFileExtension ( {
9+ content,
10+ extension,
11+ } : {
12+ content : string
13+ extension : string
14+ } ) {
15+ // replace e.g. `import { foo } from './foo'` with `import { foo } from './foo.js'`
16+ content = content . replace (
17+ / ( i m | e x ) p o r t \s [ \w { } / * \s , ] + f r o m \s [ ' " ] (?: \. \. ? \/ ) + ?[ ^ . ' " ] + (? = [ ' " ] ; ? ) / gm,
18+ `$&.${ extension } ` ,
19+ )
20+
21+ // replace e.g. `import('./foo')` with `import('./foo.js')`
22+ content = content . replace (
23+ / i m p o r t \( [ ' " ] (?: \. \. ? \/ ) + ?[ ^ . ' " ] + (? = [ ' " ] ; ? ) / gm,
24+ `$&.${ extension } ` ,
25+ )
26+ return content
27+ }
28+
829const config = defineConfig ( {
930 // fix from https://github.com/vitest-dev/vitest/issues/6992#issuecomment-2509408660
1031 resolve : {
@@ -31,8 +52,6 @@ const config = defineConfig({
3152} )
3253
3354// copy from @tanstack /config/vite with changes:
34- // - dts removed ensureImportFileExtension
35- // - dts removed dts beforeWriteFile
3655// - dts outDir: dist/types
3756// - build - lib - fileName: [name.mjs]
3857// - rollup - output - preserveModulesRoot: src
@@ -56,6 +75,14 @@ export const tanstackViteConfig = (options: Options) => {
5675 module : 99 , // ESNext
5776 declarationMap : false ,
5877 } ,
78+ beforeWriteFile : ( filePath , content ) => {
79+ // content =
80+ // options.beforeWriteDeclarationFile?.(filePath, content) || content
81+ return {
82+ filePath,
83+ content : ensureImportFileExtension ( { content, extension : 'js' } ) ,
84+ }
85+ } ,
5986 afterDiagnostic : ( diagnostics ) => {
6087 if ( diagnostics . length > 0 ) {
6188 console . error ( 'Please fix the above type errors' )
0 commit comments