目录
rollup 官网:简介 | rollup.js 中文文档 | rollup.js 中文网
一、Rollup概述
- Rollup 与 Webpack 类似,仅仅是一款 ESM 打包器
- Rollup 中并不支持类似 HMR 这种高级特性
- 提供一个充分利用ESM 各项特性的高效打包器
二、Rollup 快速上手
- yarn add rollup --dev
- yarn rollup ./src/index.js --format iife
- yarn rollup ./src/index.js --format iife --file dist/bundle.js
三、Rollup 配置文件
- 在根目录新建 rollup.config.js
- yarn rollup --config
- yarn rollup --config rollup.config.js
export default {
input:'./src/index.js',
output: {
file: 'dist/bundle.js',
format:'iife'
}
}
四、Rollup 使用插件
- yan add rollup-plugin-json --dev
- 在 index.js 导入 import { name, version } from '../package.json'
// index.js
// 导入模块成员
import { log } from './logger'
import messages from './messages'
import { name, version } from '../package.json'
// 使用模块成员
const msg = messages.hi
log(msg)
log(name)
log(version)
// rollup.config.js
import json from 'rollup-plugin-json'
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'iife'
},
plugins: [
json()
]
}
五、Rollup 加载 NPM 模块
- yarn add rollup-plugin-node-resolve --dev
// index.js
// 导入模块成员
import _ from 'lodash-es'
import { log } from './logger'
import messages from './messages'
import { name, version } from '../package.json'
// 使用模块成员
const msg = messages.hi
log(msg)
log(name)
log(version)
log(_.camelCase('hello world'))
// rollup.config.js
import json from 'rollup-plugin-json'
import resolve from 'rollup-plugin-node-resolve'
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'iife'
},
plugins: [
json(),
resolve()
]
}
六、Rollup 加载 CommonJS 模块
- yarn add rollup-plugin-commonjs --dev
- yarn rollup --config
// rollup.config.js
import json from 'rollup-plugin-json'
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'iife'
},
plugins: [
json(),
resolve(),
commonjs()
]
}
七、Rollup 代码拆分
- yarn rollup --config --format amd
import json from 'rollup-plugin-json'
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
export default {
input:'./src/index.js',
output: {
// file: 'dist/bundle.js',
// format:'iife'
dir:'dist',
format:'amd'
},
plugins: {
json(),
resolve(),
commonjs()
}
}
八、Rollup 多入口打包
// roll.config.js
import json from 'rollup-plugin-json'
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
export default {
//input:'./src/index.js',
input:{
foo:'src/index.js',
bar:'src/album.js'
},
output: {
// file: 'dist/bundle.js',
// format:'iife'
dir:'dist',
format:'amd'
},
plugins: [
json(),
resolve(),
commonjs()
]
}
- 在 dist 目录新建index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<!-- AMD 标准格式的输出 bundle 不能直接引用 -->
<!-- <script src="foo.js"></script> -->
<!-- 需要 Require.js 这样的库 -->
<script src="https://unpkg.com/requirejs@2.3.6/require.js" data-main="foo.js"></script>
</body>
</html>
- serve dist
九、Rollup 选用原则
优点
- 输出结果更加扁平
- 自动移除未引用代码
- 打包结果依然完全刻度
缺点
- 加载非 WSM 的第三方模块比较复杂
- 模块最终都被打包到一个函数中,无法实现 HMR
- 浏览器环境中,代码拆分功能依赖 AMD 库
总之, Webpack 大而全,Rollup 小而美 ,如果开发应用程序,用 Webpack,如果开发库/框架开发使用 Rollup.