-
Notifications
You must be signed in to change notification settings - Fork 196
/
build.config.base.mjs
163 lines (149 loc) · 3.71 KB
/
build.config.base.mjs
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
/* eslint-disable max-lines-per-function */
/* eslint-disable import/order */
import { viteCommonjs } from '@originjs/vite-plugin-commonjs';
import { toLower } from 'lodash';
import path from 'path';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import { visualizer } from 'rollup-plugin-visualizer';
export const getBaseConfig = ({
aliasReact = false,
aliasReactComponents = false,
} = {}) => {
const entry = './src/index.ts';
const OUT_DIR_NAME_MAP = {
es: 'esm',
cjs: 'lib',
umd: 'dist',
};
const format = process.env.FORMAT;
const isAnalysisMode = process.env.ANALYSIS;
const isDevMode = process.env.PLAYGROUND;
const outDir = OUT_DIR_NAME_MAP[format];
const isUMD = format === 'umd';
const isESM = format === 'es';
const define = {
'process.env.NODE_ENV': JSON.stringify(
isDevMode ? 'development' : 'production',
),
};
const resolve = {
mainFields: ['src', 'module', 'main'],
alias: [],
};
if (isDevMode) {
// 防止开发模式下直接加载 s2-core 中的主题 less
resolve.alias.push(
...[
{
find: /^(.*)\/theme\/(.*)\.less$/,
replacement: '$1/theme/$2.less?inline',
},
{
find: /^@antv\/s2$/,
replacement: path.join(__dirname, './packages/s2-core/src'),
},
{
find: /^@antv\/s2\/extends$/,
replacement: path.join(__dirname, './packages/s2-core/src/extends'),
},
aliasReact && {
find: /^@antv\/s2-react$/,
replacement: path.join(__dirname, './packages/s2-react/src'),
},
aliasReactComponents && {
find: /^@antv\/s2-react-components$/,
replacement: path.join(
__dirname,
'./packages/s2-react-components/src',
),
},
].filter(Boolean),
);
}
const getViteConfig = (
{ port, name, libName, plugins } = {
port: 3001,
plugins: [],
},
) => {
const filename = isUMD ? `${toLower(name || libName)}.min` : '[name]';
return {
server: {
port,
hmr: true,
},
resolve,
define: {
'process.env.NODE_ENV': JSON.stringify(
isDevMode ? 'development' : 'production',
),
},
plugins: [
peerDepsExternal(),
!isDevMode && viteCommonjs(),
isAnalysisMode &&
visualizer({
open: true,
gzipSize: true,
brotliSize: true,
}),
...plugins,
].filter(Boolean),
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true,
},
},
modules: {
/**
* 样式小驼峰转化
* css: goods-list => tsx: goodsList
*/
localsConvention: 'camelCase',
},
},
build: {
target: 'es2015',
minify: isUMD ? 'esbuild' : false,
sourcemap: true,
lib: {
name: libName,
entry,
formats: [format],
},
outDir,
rollupOptions: {
output: {
dir: outDir,
entryFileNames: `${filename}.js`,
assetFileNames: `${filename}.[ext]`,
globals: {
vue: 'Vue',
react: 'React',
'react-dom': 'ReactDOM',
'@antv/s2': 'S2',
'@antv/s2-react': 'S2React',
lodash: '_',
antd: 'antd',
'ant-design-vue': 'antd',
},
},
},
},
};
};
return {
entry,
getViteConfig,
define,
format,
resolve,
isAnalysisMode,
outDir,
OUT_DIR_NAME_MAP,
isDevMode,
isUMD,
isESM,
};
};