企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 概述 打包工具的作用是,将多个 JavaScript 脚本合并成一个脚本,供浏览器使用 是打造一款简单易用的 ES 模块打包工具 ## 安装 ``` npm install --global rollup ``` 使用 ``` $ rollup --help # 或者 $ npx rollup --help ``` ## 示例 ### 快速入门 ``` // add.js const PI = 3.14; const E = 2.718; export function addPi(x) { return x + PI; } export function addE(x) { return x + E; } ``` ``` // main.js import { addPi } from './add.js'; console.log(addPi(10)); ``` 执行,默认输出到屏幕 ``` > rollup main.js const PI = 3.14; function addPi(x) { return x + PI; } console.log(addPi(10)); ``` 会自动打包成一个文件,并且没用到的函数不会输出 ``` rollup main.js --file bundle.js ``` 输出到指定文件 ### 多入口文件 ``` rollup m1.js m2.js --dir dist ``` 就依次填写它们的文件名,并使用参数`--dir`指定输出目录 ### 压缩代码 ``` rollup main.js --compact ``` ### 文件配置 ``` // rollup.config.js export default { input: 'main.js', output: { file: 'bundle.js', format: 'es' } }; ``` ``` rollup -c ``` 启动参数