Open History to see the last commit
1import { defineConfig, mergeConfig } from 'vite'
2import { externalizeDeps } from 'vite-plugin-externalize-deps'
3import tsconfigPaths from 'vite-tsconfig-paths'
4import dts from 'vite-plugin-dts'
5import type { Options } from '@tanstack/vite-config'
6β
7function ensureImportFileExtension({
8 content,
9 extension,
10}: {
11 content: string
12 extension: string
13}) {
14 // replace e.g. `import { foo } from './foo'` with `import { foo } from './foo.js'`
15 content = content.replace(
16 /(im|ex)port\s[\w{}/*\s,]+from\s['"](?:\.\.?\/)+?[^.'"]+(?=['"];?)/gm,
17 `$&.${extension}`,
18 )
19β
20 // replace e.g. `import('./foo')` with `import('./foo.js')`
21 content = content.replace(
22 /import\(['"](?:\.\.?\/)+?[^.'"]+(?=['"];?)/gm,
23 `$&.${extension}`,
24 )
25 return content
26}
27β
28const config = defineConfig({
29 // fix from https://github.com/vitest-dev/vitest/issues/6992#issuecomment-2509408660
30 resolve: {
31 conditions: ['@tanstack/custom-condition'],
32 },
33})
34β
35// copy from @tanstack/config/vite with changes:
36// - build - lib - fileName: [name.mjs]
37// - rollup - output - preserveModulesRoot: src
38export const tanstackViteConfig = (options: Options) => {
39 const outDir = options.outDir ?? 'dist'
40 const cjs = options.cjs ?? true
41β
42 return defineConfig({
43 plugins: [
44 externalizeDeps({ include: options.externalDeps ?? [] }),
45 tsconfigPaths({
46 projects: options.tsconfigPath ? [options.tsconfigPath] : undefined,
47 }),
48 dts({
49 outDir,
50 entryRoot: options.srcDir,
51 include: options.srcDir,
52 exclude: options.exclude,
53 tsconfigPath: options.tsconfigPath,
54 compilerOptions: {
55 module: 99, // ESNext
56 declarationMap: false,
57 },
58 beforeWriteFile: (filePath, content) => {
59 return {
60 filePath,
61 content: ensureImportFileExtension({ content, extension: 'js' }),
62 }
63 },
64 afterDiagnostic: (diagnostics) => {
65 if (diagnostics.length > 0) {
66 console.error('Please fix the above type errors')
67 process.exit(1)
68 }
69 },
70 }),
71 ],
72 build: {
73 outDir,
74 minify: false,
75 sourcemap: true,
76 lib: {
77 entry: options.entry,
78 formats: cjs ? ['es', 'cjs'] : ['es'],
79 fileName: () => '[name].mjs',
80 },
81 rollupOptions: {
82 output: {
83 preserveModules: true,
84 preserveModulesRoot: 'src',
85 },
86 },
87 },
88 })
89}
90β
91export default mergeConfig(
92 config,
93 tanstackViteConfig({
94 cjs: false,
95 entry: [
96 './src/index.ts',
97 './src/inject-queries-experimental/index.ts',
98 './src/devtools-panel/index.ts',
99 './src/devtools-panel/stub.ts',
100 './src/devtools/index.ts',
101 './src/devtools/stub.ts',
102 ],
103 exclude: ['src/__tests__'],
104 srcDir: './src',
105 tsconfigPath: 'tsconfig.prod.json',
106 }),
107)