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