-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.js
More file actions
128 lines (119 loc) · 3.2 KB
/
Copy pathrollup.config.js
File metadata and controls
128 lines (119 loc) · 3.2 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
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
import commonjs from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import postcss from 'rollup-plugin-postcss';
import autoprefixer from 'autoprefixer';
import alias from '@rollup/plugin-alias';
import image from '@rollup/plugin-image';
import license from 'rollup-plugin-license';
import progress from 'rollup-plugin-progress';
import nodeGlobals from 'rollup-plugin-node-globals';
// import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import { optimizeLodashImports } from '@optimize-lodash/rollup-plugin';
import svgr from '@svgr/rollup';
import pkg from './package.json';
import map from 'lodash/map';
import isArray from 'lodash/isArray';
import mergeWith from 'lodash/mergeWith';
import path from 'path';
import cp from 'child_process';
import comments from 'postcss-discard-comments';
function customizer(objValue, srcValue) {
if (isArray(objValue)) {
return objValue.concat(srcValue);
}
}
const resolve = function (...args) {
return path.resolve(__dirname, ...args);
};
const commitHash = cp.execSync('git rev-parse --short HEAD', { encoding: 'utf-8' }).trim();
const genLisence = license({
sourcemap: true,
banner: `/*! @datlas/design <%= pkg.version %> (${commitHash})*/`,
});
const baseConfig = {
input: resolve('./src/index.ts'),
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
},
external: map(pkg.peerDependencies, (_, k) => k),
plugins: [
// peerDepsExternal(),
alias({
entries: [{ find: '@', replacement: resolve('src') }],
}),
postcss({
extract: true, // 可配置生成绝对路径
minimize: process.env.FORMAT === 'min',
use: {
less: { javascriptEnabled: true },
},
extensions: ['css', 'less'],
plugins: [autoprefixer, comments({ removeAll: true })],
}),
commonjs(),
nodeResolve({
extensions: ['.js', '.jsx', '.ts', '.tsx', '.less'],
}),
image(),
svgr({
svgoConfig: {
plugins: {
removeViewBox: false,
},
},
}),
nodeGlobals(),
typescript({
useTsconfigDeclarationDir: true,
}),
optimizeLodashImports(),
progress(),
],
onwarn(error, warn) {
if (error.code === 'THIS_IS_UNDEFINED') {
return;
}
if (error.code !== 'CIRCULAR_DEPENDENCY') {
warn(error);
}
},
};
// 打包任务的个性化配置
const jobs = {
umd: {
output: {
inlineDynamicImports: true,
format: 'umd',
file: resolve(pkg.umd),
name: 'mdtd',
},
plugins: [genLisence],
},
min: {
output: {
inlineDynamicImports: true,
format: 'umd',
file: resolve(pkg.umd.replace(/(.\w+)$/, '.min$1')),
name: 'mdtd',
},
plugins: [terser(), genLisence],
},
iife: {
output: {
inlineDynamicImports: true,
format: 'iife',
file: resolve(pkg.browser),
name: 'mdtd',
},
plugins: [terser(), genLisence],
},
};
// 从环境变量获取打包特征
const mergeConfig = jobs[process.env.FORMAT || 'esm'];
const config = mergeWith(baseConfig, mergeConfig, customizer);
module.exports = config;