rollup打包


webpack偏向于应用的打包
rollup偏向于js类库的打包

roullup.js默认采用ES模块标准,我们可以通过rollup-plugin-commonjs插件使之支持commonJS标准

安装rollup.js:

npm i rollup -g

通过rollup指令打包

rollup src/main.js -f es -o dist/bundle.js
// 使用rollup打包main.js到dist目录下的bundle.JS中,采用esModule规范

-f --format的缩写,指定打包为xxx模块,可以是’amd’、‘cjs’、‘system’、‘esm’('es’也可以)、'iife’或’umd’中的任何一个

-o --output.file的缩写,指定输出到XX目录

通过rollup配置文件进行打包

配置文件默认命名为rollup.config.js文件

export default {
  input: './src/main.js',
  output: [{
    file: './dist/index-cjs.js',
    format: 'cjs',
    banner: '// welcome to imooc.com',
    footer: '// powered by sam'
  }, {
    file: './dist/index-es.js',
    format: 'es',
    banner: '// welcome to imooc.com',
    footer: '// powered by sam'
  }]
}
  • rollup的配置文件需要采用ES模块标准编写
  • input表示入口文件的路径(老版本为entry,已经废弃)
  • output表示输出文件的内容,它允许传入一个对象或一个数组,当为数组时,依次输出多个文件,它包含以下内容:
    • output.file:输出文件的路径(老版本为dest,已经废弃)
    • output.format:输出文件的格式
    • output.banner:文件头部添加的内容
    • output.footer:文件末尾添加的内容

不指定具体文件,会去找rollup.config.js文件进行打包:

  • 通过rollup -c指令进行打包,rollup.js会自动寻找名称为rollup.config.js的配置文件

也可以指定具体的打包文件:

rollup -c rollup.config.dev.js