Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

vite.config.ts

91 lines2.4 KB

1import { isBuiltin } from 'node:module'
2import { defineConfig } from 'vite'
3import dts from 'vite-plugin-dts'
4import tsconfigPaths from 'vite-tsconfig-paths'
5import packageJson from './package.json' with { type: 'json' }
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 (specifier) =>
18 /\.(?:[cm]?js|json)$/.test(specifier)
19 ? specifier
20 : `${specifier}.${extension}`,
21 )
22​
23 // replace e.g. `import('./foo')` with `import('./foo.js')`
24 content = content.replace(
25 /import\(['"](?:\.\.?\/)+?[^'"]+(?=['"];?)/gm,
26 (specifier) =>
27 /\.(?:[cm]?js|json)$/.test(specifier)
28 ? specifier
29 : `${specifier}.${extension}`,
30 )
31 return content
33​
34const externalDependencies = Object.keys({
35 ...packageJson.dependencies,
36 ...packageJson.peerDependencies,
37})
38​
39export default defineConfig({
40 // fix from https://github.com/vitest-dev/vitest/issues/6992#issuecomment-2509408660
41 resolve: {
42 conditions: ['@tanstack/custom-condition'],
43 },
44 plugins: [
45 tsconfigPaths({ projects: ['tsconfig.prod.json'] }),
46 dts({
47 outDir: 'dist',
48 entryRoot: './src',
49 include: './src',
50 exclude: ['src/**/__tests__/**', 'src/**/__test__/**'],
51 tsconfigPath: 'tsconfig.prod.json',
52 compilerOptions: {
53 module: 99, // ESNext
54 declarationMap: false,
55 },
56 beforeWriteFile: (filePath, content) => {
57 return {
58 filePath,
59 content: ensureImportFileExtension({ content, extension: 'js' }),
60 }
61 },
62 afterDiagnostic: (diagnostics) => {
63 if (diagnostics.length > 0) {
64 console.error('Please fix the above type errors')
65 process.exit(1)
66 }
67 },
68 }),
69 ],
70 build: {
71 outDir: 'dist',
72 minify: false,
73 sourcemap: true,
74 lib: {
75 entry: ['./src/index.ts', './src/internal.ts'],
76 formats: ['es'],
77 fileName: () => '[name].mjs',
78 },
79 rolldownOptions: {
80 external: (id) =>
81 isBuiltin(id) ||
82 externalDependencies.some(
83 (dependency) => id === dependency || id.startsWith(`${dependency}/`),
84 ),
85 output: {
86 preserveModules: true,
87 preserveModulesRoot: 'src',
88 },
89 },
90 },
91})