Skip to content

Commit a45e9c8

Browse files
committed
refactor(rollup-config): extract common plugin replace logic
1 parent 30ec60e commit a45e9c8

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

rollup.config.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@ module.exports = () => {
33

44
const rollupConfig = getRollupConfig()
55

6-
const commonJsIndex = rollupConfig.plugins.findIndex(
7-
plugin => plugin.name === 'commonjs',
8-
)
9-
10-
if (commonJsIndex >= 0) {
6+
replace(rollupConfig.plugins, 'commonjs', () => {
117
const commonjs = require('rollup-plugin-commonjs')
128

13-
rollupConfig.plugins[commonJsIndex] = commonjs({
9+
return commonjs({
1410
include: 'node_modules/**',
1511
namedExports: {
1612
'react-is': ['isValidElementType'],
1713
},
1814
})
19-
}
15+
})
2016

2117
if (process.env.BUILD_FORMAT !== 'umd') {
22-
const babelIndex = rollupConfig.plugins.findIndex(
23-
plugin => plugin.name === 'babel',
24-
)
25-
26-
if (babelIndex >= 0) {
18+
replace(rollupConfig.plugins, 'babel', () => {
2719
const babel = require('rollup-plugin-babel')
2820

29-
rollupConfig.plugins[babelIndex] = babel({
21+
return babel({
3022
exclude: 'node_modules/**',
3123
babelrc: true,
3224
runtimeHelpers: true,
3325
})
34-
}
26+
})
3527
}
3628

3729
return rollupConfig
3830
}
31+
32+
function replace(plugins, name, factory) {
33+
const idx = plugins.findIndex(plugin => plugin.name === name)
34+
35+
if (idx >= 0) {
36+
plugins[idx] = factory(plugins[idx])
37+
}
38+
}

0 commit comments

Comments
 (0)