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

vite.config.ts

114 lines2.8 KB

1import { isBuiltin } from 'node:module'
2import path from 'node:path'
3import { fileURLToPath } from 'node:url'
4import { defineConfig } from 'vite'
5import dts from 'vite-plugin-dts'
6import tsconfigPaths from 'vite-tsconfig-paths'
7import packageJson from './package.json' with { type: 'json' }
8​
9const packageDir = path.dirname(fileURLToPath(import.meta.url))
10const angularQueryEntry = path.join(
11 packageDir,
12 '../angular-query/dist/index.mjs',
14const angularQueryInternalEntry = path.join(
15 packageDir,
16 '../angular-query/dist/internal.mjs',
18const queryDevtoolsEntry = path.join(
19 packageDir,
20 '../query-devtools/src/index.ts',
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
41​
42const externalDependencies = Object.keys({
43 ...packageJson.dependencies,
44 ...packageJson.peerDependencies,
45})
46​
47export default defineConfig({
48 resolve: {
49 conditions: ['@tanstack/custom-condition'],
50 ...(process.env.VITEST === 'true'
51 ? {
52 alias: {
53 '@tanstack/angular-query/internal': angularQueryInternalEntry,
54 '@tanstack/angular-query': angularQueryEntry,
55 '@tanstack/query-devtools': queryDevtoolsEntry,
56 },
57 }
58 : {}),
59 },
60 plugins: [
61 tsconfigPaths({ projects: ['tsconfig.prod.json'] }),
62 dts({
63 outDir: 'dist',
64 entryRoot: './src',
65 include: './src',
66 exclude: ['src/__tests__'],
67 tsconfigPath: 'tsconfig.prod.json',
68 compilerOptions: {
69 module: 99, // ESNext
70 declarationMap: false,
71 },
72 beforeWriteFile: (filePath, content) => {
73 return {
74 filePath,
75 content: ensureImportFileExtension({ content, extension: 'js' }),
76 }
77 },
78 afterDiagnostic: (diagnostics) => {
79 if (diagnostics.length > 0) {
80 console.error('Please fix the above type errors')
81 process.exit(1)
82 }
83 },
84 }),
85 ],
86 build: {
87 outDir: 'dist',
88 minify: false,
89 sourcemap: true,
90 lib: {
91 entry: [
92 './src/index.ts',
93 './src/stub.ts',
94 './src/production/index.ts',
95 './src/devtools-panel/index.ts',
96 './src/devtools-panel/stub.ts',
97 './src/devtools-panel/production/index.ts',
98 ],
99 formats: ['es'],
100 fileName: () => '[name].mjs',
101 },
102 rolldownOptions: {
103 external: (id) =>
104 isBuiltin(id) ||
105 externalDependencies.some(
106 (dependency) => id === dependency || id.startsWith(`${dependency}/`),
107 ),
108 output: {
109 preserveModules: true,
110 preserveModulesRoot: 'src',
111 },
112 },
113 },
114})