近期维护组内的几个lib库,以前是webpack打包的,自己功能也不复杂,但由于性能是今年组内的重点,为了更小的体积,逐步将打包工具迁移到rollupcss
通过一段时间的探索,逐步抽离了一份通用的配置,隐藏细节,使用者能够很方便的使用rollup进行打包html
提供alias,eslint,resolve,common,babel,replace,postcss等基本插件,使用者可传入同名属性进行相应的plugin配置(见使用)webpack
git地址git
提供了基本的启动服务以及热更新功能,服务启动在http://127.0.0.1:8080,热更新默认监听./src
目录github
提供uglify和filesize功能web
安装json
yarn add cerate-rollup-config --dev
使用bash
// rollup.config.js const path = require('path') const baseConfig = require('create-rollup-config'); const config = baseConfig({ alias: { $common: './src/common' }, replace: { env: JSON.stringify(process.env.NODE_ENV) }, serve: { port: 7001 }, livereload: { watch: '/src' // default } }) export default [ { input: './src/test.js', output: [ { file: 'dist/test.js', format: 'cjs' } ], ...config } ]
package.json配置babel
{ ..., "scripts": { "build": "cross-env NODE_ENV=production rollup -c ./rollup.config.js", "server": "cross-env NODE_ENV=development rollup -c ./rollup.config.js --watch", ... }, ... }
默认开启了minimize功能app
将html文件转为字符串,并支持压缩
缘由是:
当引入commonjs包时,若是该包对exports进行了从新赋值,那么经过rollup打包时,获取不到该值,只能经过namedExports来告知rollup
因此咱们打包时,能够输出多个格式,cjs+umd
参考:https://github.com/rollup/rol...
写业务代码的时候,有时候会一块儿使用export default和export,但在rollup中一块儿使用的时候,须要注意,打包出来的包是这样的
// test.js export default { test: 'test' } export const test2 = 'test2' // 打包后 exports.default = { test: 'test' } exports.test2 = 'test2'
这样要注意,经过require('test'),导入的对象是
{ default: { test: 'test' }, test2: 'test2' }
可能与你的预期不一致,除非加default,require('test').default
babel有个插件能够解决这个问题:https://github.com/59naga/bab...