-
Notifications
You must be signed in to change notification settings - Fork 13
/
rollup.config.js
173 lines (167 loc) · 6.14 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
process.env.NODE_ENV = 'production';
const path = require('path');
const fs = require('fs');
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').default;
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 eslint = require('@rollup/plugin-eslint');
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: 'ReactFormutil',
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: [
eslint({
fix: true,
throwOnError: true,
throwOnWarning: true
}),
nodeResolve({
extensions: ['.js', '.jsx', '.ts', '.tsx']
}),
commonjs({
include: /node_modules/
}),
replace({
'process.env.NODE_ENV': JSON.stringify(env)
}),
babel({
exclude: 'node_modules/**',
extensions: ['.js', '.jsx', '.ts', '.tsx'],
babelHelpers: 'runtime',
babelrc: false,
configFile: false,
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'entry',
corejs: 3,
modules: false,
exclude: ['transform-typeof-symbol']
}
],
[
'@babel/preset-react',
{
development: false,
useBuiltIns: true,
runtime: 'classic'
}
],
['@babel/preset-typescript']
],
plugins: [
'babel-plugin-macros',
['@babel/plugin-proposal-decorators', { legacy: true }],
[
'@babel/plugin-proposal-class-properties',
{
loose: 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
}
],
require('@babel/plugin-proposal-optional-chaining').default,
require('@babel/plugin-proposal-nullish-coalescing-operator').default,
// Adds Numeric Separators
require('@babel/plugin-proposal-numeric-separator').default
].filter(Boolean)
}),
module !== 'umd' &&
sass({
output: `dist/${exportName}.css`
}),
isProd &&
terser({
output: { comments: false },
compress: false,
warnings: false,
ecma: 5,
ie8: false,
toplevel: true
}),
filesize(),
copy({
targets: [
{
src: `npm/index.${module}.js`,
dest: 'dist'
}
],
verbose: false
})
].filter(Boolean)
};
}
module.exports = ['cjs', 'esm', 'umd'].reduce((configQueue, module) => {
return fs.existsSync(`./npm/index.${module}.js`)
? configQueue.concat(createConfig('development', module), createConfig('production', module))
: configQueue;
}, []);