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

vite.config.ts

126 lines3.3 KB

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