Rollup 与 Parcel
0x01 Rollup
(1)简介
-
Rollup 是一个用于 JavaScript 的模块打包工具,将小的代码片段编译成更大、更复杂的代码
- 如:库、应用程序
-
与 Webpack 类似,但 Rollup 更加小巧
-
特点:
- Rollup 不支持 HMR(Hot Module Replacement,模块热替换)
- Rollup 采用 ESM 标准,充分利用了 ESM 各项特性
- Rollup 默认使用 Tree-shaking
-
优点:
- 输出结果扁平
- 自动移除未引用的代码
- 打包结果可读性高
-
缺点:
- 加载非 ESM 第三方模块复杂
- 无法使用 HMR 优化开发体验
- 浏览器环境中代码拆分依赖 AMD 库,如 RequireJS
-
适用场景:开发自定义框架或自定义模块
(2)安装
-
使用命令
npm init -y初始化 NodeJS 环境 -
准备文件
-
src/apple.js
export default { label: "an apple a day keeps the doctor away", }; -
src/calc.js
export const add = (a, b) => a + b; export const sub = (a, b) => a - b; -
src/index.js
import apple from "./apple"; import { add } from "./calc"; console.log(apple.label); console.log(add(1, 2));
-
-
使用命令
npm install --save-dev rollup安装 Rollup -
使用命令
npx rollup ./src/index.js --format iife --file dist/bundle.js将 src/index.js 以 IIFE 格式打包到 dist/bundle.js 文件中
(3)配置文件
-
./rollup.config.js
export default { input: "src/index.js", // 输入文件 output: { file: "dist/bundle.js", // 输出文件 format: "iife", // 输出格式,如 iife-立即执行函数 }, }; -
使用命令
npx rollup --config rollup.config.js或npx rollup --config执行打包
(4)使用插件
插件是 Rollup 唯一的扩展方式
举例:使用插件实现在代码文件中导入 JSON 文件
-
使用命令
npm install --save-dev rollup-plugin-json安装插件 -
./rollup.config.js
import json from "rollup-plugin-json"; export default { // ... plugins: [json()], // 导入插件 }; -
src/index.js
import { name } from "../package.json"; console.log(name); -
使用命令
npx rollup --config打包
(5)使用模块
- Rollup 默认只能按照文件路径的方式加载本地的文件模块
a. NPM
需要使用 rollup-plugin-node-resolve 实现导入 NPM 模块
-
使用命令
npm install --save-dev rollup-plugin-node-resolve安装插件 -
./rollup.config.js
import nodeResolve from "rollup-plugin-node-resolve"; export default { // ... plugins: [nodeResolve()], }; -
使用命令
npm install --save lodash-es安装 ES 规范的 Lodash(用于演示,可以替换为其他包) -
src/index.js
import { camelCase } from "lodash-es"; console.log(camelCase("hello world")); -
使用命令
npx rollup --config打包
b. CJS
需要使用 rollup-plugin-commonjs 实现导入 CJS 模块
-
使用命令
npm install --save-dev rollup-plugin-commonjs安装插件 -
./rollup.config.js
import commonjs from "rollup-plugin-commonjs"; export default { // ... plugins: [commonjs()], }; -
使用命令
npm install --save lodash-es安装 Lodash(用于演示,可以替换为其他包) -
src/cjs.module.js
module.exports = { name: "apple", label: "an apple a day keeps the doctor away", description: "apple description", }; -
src/index.js
import cjsModule from "./cjs.module"; console.log(cjsModule); -
使用命令
npx rollup --config打包
(6)代码拆分
-
代码拆分可以理解为按需导入或分包
-
Rollup 中不支持使用 IIFE 标准直接实现代码拆分,需要使用 AMD 标准
- Webpack 打包时使用了引导代码实现了在 IIFE 中进行代码拆分
-
示例:
-
src/index.js
import("./apple").then((result) => console.log(result)); import("./calc").then((result) => console.log(result.sub(3, 2))); -
./rollup.config.js
export default { input: "src/index.js", output: { dir: "dist", format: "amd", }, }; -
使用命令
npx rollup --config打包
-
(7)多入口打包
多入口打包涉及代码拆分,需要使用 AMD 标准
-
准备文件
-
src/index_1.js
import { add } from "./calc"; console.log(add(1, 2)); -
src/index_2.js
import { sub } from "./calc"; console.log(sub(3, 2));
-
-
./rollup.config.js
export default { input: ["src/index_1.js", "src/index_2.js"], output: { dir: "dist", format: "amd", }, }; -
使用命令
npx rollup --config打包
0x02 Parcel
(1)简介
- 官网:https://parceljs.org/
- Parcel 是一个零配置的前端应用打包工具
- Parcel 打包速度比 Webpack 更快
- Parcel 采用多进程进行打包工作,Webpack 中需要通过 happypack 插件实现
(2)安装
-
使用命令
npm init -y初始化 NodeJS 环境 -
准备文件
-
src/js/calc.js
export const add = (a, b) => a + b; export const sub = (a, b) => a - b; -
src/js/main.js
import { add } from "./calc"; document.getElementById("title").textContent = add(1, 2); -
src/index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> </head> <body> <h1 id="title"></h1> <script src="js/main.js"></script> </body> </html>
-
-
使用命令
npm install --save-dev parcel-bundler安装 Parcel -
使用命令
npx parcel src/index.html打包并开启开发服务器
(使用命令npx parcel build src/index.html构建)
(3)特性
-
支持热更新
// src/main.js // ... // 允许热更新时触发 if (module.hot) { // 执行热更新时触发 module.hot.accept(() => { console.log("[HMR] accepted"); }); } -
支持自动安装所需依赖,即导入存在但未安装的模块时,Parcel 会自动检测并自动安装
-
支持加载不同类型的资源文件,且无需安装额外的插件
-
支持动态导入
-
支持代码压缩
-
支持 SourceMap
-End-

浙公网安备 33010602011771号