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

vite.config.ts

131 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: {
48 enabled: true,
49 provider: 'istanbul',
50 include: ['src/**/*'],
51 exclude: ['src/__tests__/**'],
52 },
53 typecheck: { enabled: true },
54 globals: true,
55 restoreMocks: true,
56 },
57})
58​
59// copy from @tanstack/config/vite with changes:
60// - build - lib - fileName: [name.mjs]
61// - rollup - output - preserveModulesRoot: src
62export const tanstackViteConfig = (options: Options) => {
63 const outDir = options.outDir ?? 'dist'
64 const cjs = options.cjs ?? true
65​
66 return defineConfig({
67 plugins: [
68 externalizeDeps({ include: options.externalDeps ?? [] }),
69 tsconfigPaths({
70 projects: options.tsconfigPath ? [options.tsconfigPath] : undefined,
71 }),
72 dts({
73 outDir,
74 entryRoot: options.srcDir,
75 include: options.srcDir,
76 exclude: options.exclude,
77 tsconfigPath: options.tsconfigPath,
78 compilerOptions: {
79 module: 99, // ESNext
80 declarationMap: false,
81 },
82 beforeWriteFile: (filePath, content) => {
83 return {
84 filePath,
85 content: ensureImportFileExtension({ content, extension: 'js' }),
86 }
87 },
88 afterDiagnostic: (diagnostics) => {
89 if (diagnostics.length > 0) {
90 console.error('Please fix the above type errors')
91 process.exit(1)
92 }
93 },
94 }),
95 ],
96 build: {
97 outDir,
98 minify: false,
99 sourcemap: true,
100 lib: {
101 entry: options.entry,
102 formats: cjs ? ['es', 'cjs'] : ['es'],
103 fileName: () => '[name].mjs',
104 },
105 rollupOptions: {
106 output: {
107 preserveModules: true,
108 preserveModulesRoot: 'src',
109 },
110 },
111 },
112 })
114​
115export default mergeConfig(
116 config,
117 tanstackViteConfig({
118 cjs: false,
119 entry: [
120 './src/index.ts',
121 './src/inject-queries-experimental/index.ts',
122 './src/devtools-panel/index.ts',
123 './src/devtools-panel/stub.ts',
124 './src/devtools/index.ts',
125 './src/devtools/stub.ts',
126 ],
127 exclude: ['src/__tests__'],
128 srcDir: './src',
129 tsconfigPath: 'tsconfig.prod.json',
130 }),