-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.base-config.ts
More file actions
148 lines (133 loc) · 4.69 KB
/
vite.base-config.ts
File metadata and controls
148 lines (133 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as url from 'node:url';
import vitePluginReact from '@vitejs/plugin-react';
import type { ModuleFormat } from 'rollup';
import type { LibraryFormats, LibraryOptions, TerserOptions } from 'vite';
import vitePluginDts from 'vite-plugin-dts';
import vitePluginNoBundle from 'vite-plugin-no-bundle';
import { defineConfig, type UserConfig } from 'vitest/config';
import { getCliParams } from './scripts/vite/cli-params';
import { viteMinifyBundlesPlugin } from './scripts/vite/plugin-minify-bundles';
import { toKebabCase, toPascalCase } from './scripts/vite/utils';
type TCliParams = 'minify' | 'unbundled';
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
const cliParams = getCliParams<TCliParams>(process.argv, true);
const bundleFormats: LibraryFormats[] = ['es', 'umd'];
// Exposed to use in expanded configs
export const envParams = {
dirname: __dirname,
isBuildMode: false,
isUnbundled: false,
isMinify: false,
isDts: false,
isDebugConfig: false,
terserOptions: {} as TerserOptions,
};
export default defineConfig((configEnv): UserConfig => {
envParams.isBuildMode = configEnv.command === 'build';
envParams.isUnbundled = envParams.isBuildMode && cliParams.has('unbundled');
envParams.isMinify = envParams.isBuildMode && !envParams.isUnbundled && cliParams.has('minify');
envParams.isDts = envParams.isBuildMode;
envParams.isDebugConfig = false;
const packageJsonPath = path.resolve(envParams.dirname, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8')) as Record<
string,
unknown
>;
const packageFilename = toKebabCase(packageJson.name as string);
const packageNamespace = toPascalCase(packageJson.name as string);
const baseConfig = {
base: './',
build: {
target: 'es2015',
outDir: './dist',
emptyOutDir: false,
cssCodeSplit: false,
minify: envParams.isMinify ? 'terser' : false,
terserOptions: envParams.terserOptions,
sourcemap: true,
lib: {
entry: path.resolve(envParams.dirname, 'src/index.ts'),
name: packageNamespace,
formats: bundleFormats,
fileName: ((format: LibraryFormats) => {
return `${packageFilename}.${format}${envParams.isMinify ? '.min' : ''}.js`;
}) as (format: ModuleFormat) => string,
},
rollupOptions: {
external: ['react', 'react/jsx-runtime', 'react/jsx-dev-runtime', 'react-dom'],
output: {
globals: {
react: 'React',
'react/jsx-runtime': 'React',
'react/jsx-dev-runtime': 'React',
'react-dom': 'ReactDOM',
},
},
},
},
optimizeDeps: {
exclude: ['react/jsx-runtime', 'react/jsx-dev-runtime'],
},
resolve: {
alias: [
{ find: '@core', replacement: path.resolve(envParams.dirname, '../../packages/core/src') },
],
},
plugins: [
vitePluginReact(),
envParams.isUnbundled && vitePluginNoBundle({ copy: '**/*.css' }),
envParams.isDts &&
vitePluginDts({
// https://github.com/qmhc/vite-plugin-dts#options
// Workaround for composite tsconfig with "references"
tsconfigPath: path.resolve(envParams.dirname, 'tsconfig.app.json'),
// Allow side effect imports
clearPureImport: false,
// See https://api-extractor.com/pages/configs/api-extractor_json/#bundledpackages
bundledPackages: [],
// Output to package.json "types" location
rollupTypes: !envParams.isUnbundled,
}),
envParams.isMinify && viteMinifyBundlesPlugin(envParams.terserOptions),
],
test: {
globals: true,
environment: 'jsdom',
setupFiles: [path.join(__dirname, './vitest.setup.ts')],
include: ['src/**/__test__/**/*.ts?(x)', 'src/**/*.{test.ts?(x),test-d.ts}'],
},
} satisfies UserConfig;
let mergedConfig: UserConfig = baseConfig;
// Unbundled ESM
if (envParams.isUnbundled) {
const unbundledConfig = {
build: {
outDir: './dist/es',
lib: {
formats: ['es'],
fileName: undefined,
} as LibraryOptions,
},
plugins: [],
} satisfies UserConfig;
// Custom merging logic that differs from mergeConfig
mergedConfig = {
...baseConfig,
build: {
...baseConfig.build,
...unbundledConfig.build,
lib: {
...baseConfig.build.lib,
...unbundledConfig.build.lib,
},
},
plugins: [...baseConfig.plugins, ...unbundledConfig.plugins],
};
}
if (envParams.isDebugConfig) {
console.dir(mergedConfig, { depth: 4 });
}
return mergedConfig;
});