• 开源类库优先选择
  • 以 ESM 标准为目标的构建工具
  • Tree Shaking

    以命令行方式打包

  • 安装 rollup

    1. npm install -g rollup
  • 创建 index.js 文件

    1. import path from "path";
    2. console.log("hello rollop", path.join("", "hello"));
  • 打包

    1. rollup -i index.js --file dist.js --format umd
    • —file:打包输出文件
    • —format:打包格式(umd, cjs, es, iife)

      Tree Shaking

      只会打包我们用到的代码,没有用到的不会打包
  • 新建 a.js 文件

    1. export const funA = () => {
    2. console.log("a");
    3. };
    4. export const funB = () => {
    5. console.log("b");
    6. };
  • index.js 引入 a.js

    1. import { funA } from "./a";
    2. funA();
    3. console.log("hello rollup");
  • 打包文件

    1. rollup -i index.js --file dist.js --format es

    输出代码,代码进行了合并,并且只打包了用到的代码 ```javascript const funA = () => { console.log(“a”); };

funA(); console.log(“hello rollop”); ```