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 angularQueryEntry = path.join(
11 packageDir,
12 '../angular-query/dist/index.mjs',
13)
14const angularQueryInternalEntry = path.join(
15 packageDir,
16 '../angular-query/dist/internal.mjs',
17)
18const queryDevtoolsEntry = path.join(
19 packageDir,
20 '../query-devtools/src/index.ts',
21)
22β
23function ensureImportFileExtension({
24 content,
25 extension,
26}: {
27 content: string
28 extension: string
29}) {
30 content = content.replace(
31 /(im|ex)port\s[\w{}/*\s,]+from\s['"](?:\.\.?\/)+?[^.'"]+(?=['"];?)/gm,
32 `$&.${extension}`,
33 )
34β
35 content = content.replace(
36 /import\(['"](?:\.\.?\/)+?[^.'"]+(?=['"];?)/gm,
37 `$&.${extension}`,
38 )
39 return content
40}
41β
42const config = defineConfig({
43 resolve: {
44 conditions: ['@tanstack/custom-condition'],
45 ...(process.env.VITEST === 'true'
46 ? {
47 alias: {
48 '@tanstack/angular-query/internal': angularQueryInternalEntry,
49 '@tanstack/angular-query': angularQueryEntry,
50 '@tanstack/query-devtools': queryDevtoolsEntry,
51 },
52 }
53 : {}),
54 },
55})
56β
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 })
108}
109β
110export default mergeConfig(
111 config,
112 tanstackViteConfig({
113 cjs: false,
114 entry: [
115 './src/index.ts',
116 './src/stub.ts',
117 './src/production/index.ts',
118 './src/devtools-panel/index.ts',
119 './src/devtools-panel/stub.ts',
120 './src/devtools-panel/production/index.ts',
121 ],
122 exclude: ['src/__tests__'],
123 srcDir: './src',
124 tsconfigPath: 'tsconfig.prod.json',
125 }),
126)