-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite.config.ts
182 lines (168 loc) · 5.44 KB
/
vite.config.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* @Author: theajack
* @Date: 2023-04-04 23:20:27
* @Description: Coding something
*/
import type { LibraryOptions, UserConfig } from 'vite';
import { defineConfig } from 'vite';
import legacy from '@vitejs/plugin-legacy';
import { resolve } from 'path';
import { babel } from '@rollup/plugin-babel';
import { buildPackageName, upcaseFirstLetter } from './build/utils';
import vue from '@vitejs/plugin-vue';
// import { uglify } from 'rollup-plugin-uglify';
// mode_[lib-name]_[extra]
const Mode = {
Dev: 'dev', // dev/index.ts
BuildApp: 'app', // dev/index.ts
BuildLib: 'lib', // packages/xxx/src/index.ts
};
// https://vitejs.dev/config/
// @ts-ignore
export default defineConfig(({ mode }: {mode: string}) => {
const [ buildMode, dirName, extra ] = mode.split('_');
console.log(`vite build mode=${buildMode}; dirName=${dirName}; extra=${extra}`);
console.log(`${Date.now()}:INFO: mode=${buildMode}, dirName=${dirName}`);
const config = {
[Mode.Dev]: geneDevAppConfig,
[Mode.BuildApp]: geneBuildAppConfig,
[Mode.BuildLib]: () => geneBuildLibConfig(dirName),
};
const isDev = buildMode === Mode.Dev;
const CommonConfig: UserConfig = {
define: {
__DEV__: `${isDev}`,
__APP__: `${isDev || buildMode === Mode.BuildApp}`,
__VERSION__: `"${getVersion()}"`,
},
base: isDev ? '/mac' : 'https://cdn.shiyix.cn/mac',
plugins: [
],
resolve: {
alias: {
'@/': '/packages/main/src/', // 格式一定要写对喽不然没有代码提示或者报错
},
},
};
return deepAssign(CommonConfig, config[buildMode]());
});
// ! Dev App 时的配置
function geneDevAppConfig (): UserConfig {
return {
plugins: [
vue(),
legacy({
targets: [ 'defaults', 'not IE 11' ],
}),
],
publicDir: './dev/public',
server: {
host: '0.0.0.0',
port: 8091,
hmr: true,
headers: { // 使用sharedArrayBuffer
'Cross-Origin-Embedder-Policy': 'require-corp',
'Cross-Origin-Opener-Policy': 'same-origin',
},
},
};
}
// ! 构建 App 时的配置
function geneBuildAppConfig (): UserConfig {
return {
plugins: [
vue(),
legacy({
targets: [ 'defaults', 'not IE 11' ],
}),
],
build: {
rollupOptions: {
output: {
dir: resolve(__dirname, './dist'),
},
input: {
main: resolve(__dirname, './index.html'),
},
},
},
};
}
// ! 构建 lib 时的配置
function geneBuildLibConfig (dirName: string): UserConfig {
const pkgRoot = resolve(__dirname, `./packages/${dirName}`);
// 取lib包的依赖;
const deps = require(resolve(pkgRoot, './package.json'));
// ! VITE 文档说明: 注意,在 lib 模式下使用 'es' 时,build.minify 选项不会缩减空格,因为会移除掉 pure 标注,导致破坏 tree-shaking。
return {
build: {
minify: true,
lib: {
entry: resolve(pkgRoot, 'src/index.ts'), // 打包的入口文件
...SDKlibConfig(dirName),
},
rollupOptions: {
// 不需要
external: [ ...Object.keys(deps.dependencies) ],
plugins: [
babelPlugin(),
// uglify(),
],
},
outDir: resolve(pkgRoot, 'dist'), // 打包后存放的目录文件
},
};
}
function getVersion () {
return require('./package.json').version;
}
const babelPlugin = () => (
babel({
exclude: 'node_modules/**',
extensions: [ '.js', '.ts', 'tsx' ],
configFile: resolve(__dirname, './build/babel.config.js'),
})
);
function SDKlibConfig (dirName: string): Partial<LibraryOptions> {
return {
name: upcaseFirstLetter(dirName), // 包名
formats: [ 'es', 'iife' ], // 打包模式,默认是es和umd都打
fileName: (format: string) => `${buildPackageName(dirName)}.${format}.min.js`,
};
}
/*
! test
console.log('-----------------', deepAssign(
{ a: { x: [1], b: 1, f: 'xx' }, c: 1 },
{ a: { x: [2], b: 2 }, x: { a: 1 } },
{ a: { c: 3 }, d: 2 }
));
*/
function deepAssign<T extends Record<string, any>> (...args: T[]): T {
if (args.length === 0) {throw new Error('deepAssign 必须有值');}
if (args.length === 1) {return args[0];}
if (args.length > 2) {
const head = args.shift() as T;
return deepAssign(head, deepAssign(...args));
}
const assign2 = (o1: T, o2: T): T => {
const isObj = v => (typeof v === 'object' && v !== null);
const isArr = v => Array.isArray(v);
for (const k in o2) {
const v1 = o1[k], v2 = o2[k];
if (isArr(v1) && isArr(v2)) {
// @ts-ignore
v1.push(...v2);
} else if (isObj(v1) && isObj(v2)) {
// @ts-ignore
o1[k] = deepAssign(v1, v2);
} else {
o1[k] = v2;
}
}
return o1;
};
const [ head, tail ] = args;
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
return assign2(assign2({} as T, head), tail);
}