Rollup
配置文件的各个详细配置项,可见 Configuration Options。
Rollup
本身是针对 Es Module
模块化规范的 JS
打包机。
因此本节主要梳理 Rollup
纯粹处理 JS
的逻辑。
js
// rollup.config.js
const resolve = require('@rollup/plugin-node-resolve')
const commonJs = require('@rollup/plugin-commonjs')
const babel = require('@rollup/plugin-babel')
const clear = require('rollup-plugin-clear')
const autoExternal = require('rollup-plugin-auto-external')
module.exports = function getBaseConfig ({ format }) {
return {
input: 'src/js/main.js',
output: {
dir: 'dist',
format,
sourcemap: true,
chunkFileNames: '[name].[hash:6].js',
globals: {
jquery: '$'
}
},
// 改用rollup-plugin-auto-external
// external: [],
plugins: [
clear({
targets: ['dist']
}),
resolve(),
commonJs(),
babel({
babelHelpers: 'runtime',
exclude: ['node_modules/**']
}),
autoExternal()
]
}
}
TIP
Rollup
本身不支持解析node_modules
中的第三方依赖,如果想要解析,则需要利用插件@rollup/plugin-node-resolve
。Rollup
本身也不支持CommonJs
模块化的导出,如果想要解析,则需要利用插件@rollup/plugin-commonjs
。利用
external
选项,可避免将node_modules
中的第三方依赖,打包进最终代码中。