-
Notifications
You must be signed in to change notification settings - Fork 93
/
rollup.config.mjs
151 lines (137 loc) · 3.56 KB
/
rollup.config.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
import babel from '@rollup/plugin-babel'
import commonjs from '@rollup/plugin-commonjs'
import nodeResolve from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import terser from '@rollup/plugin-terser'
import pkg from './package.json' with { type: 'json' }
const CJS_DEV = 'CJS_DEV'
const CJS_PROD = 'CJS_PROD'
const ES = 'ES'
const UMD_DEV = 'UMD_DEV'
const UMD_PROD = 'UMD_PROD'
const input = './compiled/index.js'
const getExternal = (bundleType) => {
const peerDependencies = Object.keys(pkg.peerDependencies)
const dependencies = Object.keys(pkg.dependencies)
// Hat-tip: https://github.com/rollup/rollup-plugin-babel/issues/148#issuecomment-399696316.
const makeExternalPredicate = (externals) => {
if (externals.length === 0) {
return () => false
}
const pattern = new RegExp(`^(${externals.join('|')})($|/)`)
return (id) => pattern.test(id)
}
switch (bundleType) {
case CJS_DEV:
case CJS_PROD:
case ES:
return makeExternalPredicate([...peerDependencies, ...dependencies])
case UMD_DEV:
return makeExternalPredicate([...peerDependencies, 'prop-types'])
default:
return makeExternalPredicate(peerDependencies)
}
}
const isProduction = (bundleType) =>
bundleType === CJS_PROD || bundleType === UMD_PROD
const getBabelConfig = (bundleType) => {
const options = {
babelHelpers: 'runtime',
babelrc: false,
exclude: 'node_modules/**',
inputSourceMap: true,
plugins: ['@babel/transform-runtime'],
presets: [['@babel/env', { loose: true, modules: false }], '@babel/react'],
}
switch (bundleType) {
case ES:
return {
...options,
plugins: [
...options.plugins,
['transform-react-remove-prop-types', { mode: 'wrap' }],
],
}
case UMD_PROD:
case CJS_PROD:
return {
...options,
plugins: [
...options.plugins,
['transform-react-remove-prop-types', { removeImport: true }],
],
}
default:
return options
}
}
const getPlugins = (bundleType) => [
nodeResolve(),
commonjs({
include: 'node_modules/**',
}),
babel(getBabelConfig(bundleType)),
replace({
preventAssignment: true,
'process.env.NODE_ENV': JSON.stringify(
isProduction(bundleType) ? 'production' : 'development',
),
}),
isProduction(bundleType) &&
terser({
compress: {
keep_infinity: true,
pure_getters: true,
},
ecma: 5,
output: { comments: false },
toplevel: false,
warnings: true,
}),
]
const getCjsConfig = (bundleType) => ({
external: getExternal(bundleType),
input,
output: {
file: `dist/react-svg.cjs.${
isProduction(bundleType) ? 'production' : 'development'
}.js`,
format: 'cjs',
sourcemap: true,
},
plugins: getPlugins(bundleType),
})
const getEsConfig = () => ({
external: getExternal(ES),
input,
output: {
file: pkg.module,
format: 'es',
sourcemap: true,
},
plugins: getPlugins(ES),
})
const getUmdConfig = (bundleType) => ({
external: getExternal(bundleType),
input,
output: {
file: `dist/react-svg.umd.${
isProduction(bundleType) ? 'production' : 'development'
}.js`,
format: 'umd',
globals: {
...(isProduction(bundleType) ? {} : { 'prop-types': 'PropTypes' }),
react: 'React',
},
name: 'ReactSVG',
sourcemap: true,
},
plugins: getPlugins(bundleType),
})
export default [
getCjsConfig(CJS_DEV),
getCjsConfig(CJS_PROD),
getEsConfig(),
getUmdConfig(UMD_DEV),
getUmdConfig(UMD_PROD),
]