-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
186 lines (180 loc) · 6.55 KB
/
rollup.config.js
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
183
184
185
186
process.env.NODE_ENV = 'production';
const path = require('path');
const commonjs = require('rollup-plugin-commonjs');
const replace = require('rollup-plugin-replace');
const nodeResolve = require('rollup-plugin-node-resolve');
const babel = require('rollup-plugin-babel');
const sourceMaps = require('rollup-plugin-sourcemaps');
const filesize = require('rollup-plugin-filesize');
const copy = require('rollup-plugin-copy');
const sass = require('rollup-plugin-sass');
const { terser } = require('rollup-plugin-terser');
const pkg = require('./package.json');
/**
* 如果希望将某些模块代码直接构建进输出文件,可以再这里指定这些模块名称
*/
const externalExclude = [
/*'@babel/runtime', 'regenerator-runtime'*/
];
const exportName = pkg.exportName || pkg.name.split('/').slice(-1)[0];
/**
* 如果你希望编译后的代码里依然自动包含进去编译后的css,那么这里可以设置为 true
*/
const shouldPreserveCss = false;
function createConfig(env, module) {
const isProd = env === 'production';
// for umd globals
const globals = {
react: 'React',
'react-dom': 'ReactDOM',
'prop-types': 'PropTypes'
};
return {
/**
* 入口文件位置,如果你更改了entryFile,别忘了同时修改 npm/index.cjs.js 和 npm/index.esm.js 里的文件引用名称
*/
input: pkg.entryFile || 'src/index.ts',
external:
module === 'umd'
? Object.keys(globals)
: id =>
!externalExclude.some(name => id.startsWith(name)) && !id.startsWith('.') && !path.isAbsolute(id),
output: {
name: exportName,
file: `dist/${exportName}.${module}.${env}.js`,
format: module,
exports: 'named',
sourcemap: false,
intro:
module !== 'umd' && shouldPreserveCss
? module === 'cjs'
? `require('./${exportName}.css');`
: `import('./${exportName}.css');`
: undefined,
globals
},
treeshake: {
/**
* 如果你有引入一些有副作用的代码模块,或者构建后的代码运行异常,可以尝试将该项设置为 true
*/
moduleSideEffects: false
},
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify(env)
}),
nodeResolve({
extensions: ['.js', '.jsx', '.ts', '.tsx']
}),
commonjs({
include: /node_modules/
}),
babel({
exclude: 'node_modules/**',
extensions: ['.js', '.jsx', '.ts', '.tsx'],
runtimeHelpers: true,
babelrc: false,
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'entry',
corejs: 3,
modules: false,
exclude: ['transform-typeof-symbol']
}
],
[
'@babel/preset-react',
{
development: false,
useBuiltIns: true
}
],
['@babel/preset-typescript']
],
plugins: [
'babel-plugin-macros',
[
'@babel/plugin-transform-destructuring',
{
// https://github.com/facebook/create-react-app/issues/5602
loose: false,
useBuiltIns: true,
selectiveLoose: [
'useState',
'useEffect',
'useContext',
'useReducer',
'useCallback',
'useMemo',
'useRef',
'useImperativeHandle',
'useLayoutEffect',
'useDebugValue'
]
}
],
['@babel/plugin-proposal-decorators', { legacy: true }],
[
'@babel/plugin-proposal-class-properties',
{
loose: true
}
],
[
'@babel/plugin-proposal-object-rest-spread',
{
useBuiltIns: true
}
],
[
'@babel/plugin-transform-runtime',
{
version: require('@babel/helpers/package.json').version,
corejs: false,
helpers: true,
regenerator: true,
useESModules: module === 'esm',
absoluteRuntime: false
}
],
isProd && [
// Remove PropTypes from production build
'babel-plugin-transform-react-remove-prop-types',
{
removeImport: true
}
]
].filter(Boolean)
}),
sass({
output: `dist/${exportName}.css`
}),
sourceMaps(),
isProd &&
terser({
sourcemap: true,
output: { comments: false },
compress: false,
warnings: false,
ecma: 5,
ie8: false,
toplevel: true
}),
filesize(),
copy({
targets: [`npm/index.${module}.js`],
verbose: true
})
].filter(Boolean)
};
}
module.exports = [
createConfig('development', 'cjs'),
createConfig('production', 'cjs'),
createConfig('development', 'esm'),
createConfig('production', 'esm'),
createConfig('development', 'umd'),
createConfig('production', 'umd')
];