调研探索
使用 Rollup 打包…
适用于开发和配置 Rollup.js 来打包库或包,需要生成 ESM/CJS/UMD 等多种格式并优化 tree-shaking 的场景。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 49/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 85/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 60/100 ×0.15
适用场景
使用 Rollup 打包 JavaScr通用
解决思路
- 使用 Rollup 作为面向 ES modules 的模块打包器,利用其深度执行路径分析做 tree-shaking,产出更干净、更小的 bundle;适合被其他项目消费的库和包。
- 按结构组织项目:src/ 放入口 index.js、内部 modules/、工具 utils/;dist/ 放构建产物 bundle.esm.js、bundle.cjs.js、bundle.umd.js;rollup.config.mjs 放配置;package.json 管理包。
- 基础配置:在 rollup.config.mjs 中 export default { input: 'src/index.js', output: { file: 'dist/bundle.js', format: 'esm', sourcemap: true } }。
- 多输出格式配置:output 使用数组,分别输出 { file: 'dist/bundle.esm.js', format: 'esm', sourcemap: true }、{ file: 'dist/bundle.cjs.js', format: 'cjs', sourcemap: true }、{ file: 'dist/bundle.umd.js', format: 'umd', name: 'MyLibrary', sourcemap: true }。
- 选择 ES Modules(esm)输出:保留为 ES module 文件,适合现代打包器和浏览器,并让消费项目继续 tree-shaking。
- 选择 CommonJS(cjs)输出:用于 Node.js 环境,兼容 require() 语法。
- 选择 UMD 输出:兼容 AMD、CJS 和 IIFE,适合需要广泛兼容性的库。
- 选择 IIFE 输出:自执行函数,适合浏览器 script 标签。
- 优先使用命名导出:如 export const add = (a, b) => a + b; export const subtract = (a, b) => a - b;,以提升 tree-shaking;尽量避免默认导出,如 export default { add, subtract }。
- 使用静态导入:如 import { specificFunction } from './utils';,以启用 tree-shaking;避免在库代码中使用动态 require,因为 CommonJS 无法 tree-shaking。
- 使用 @rollup/plugin-node-resolve 解析依赖:配置 resolve({ browser: true, preferBuiltins: false })。
- 使用 @rollup/plugin-commonjs 转换 CommonJS 模块:添加 commonjs()。
- 使用 @rollup/plugin-babel 转译:配置 babel({ babelHelpers: 'bundled', presets: ['@babel/preset-env'] })。
- 使用 @rollup/plugin-typescript 支持 TypeScript:配置 typescript({ tsconfig: './tsconfig.json' }),input 指向 src/index.ts。
- 使用 @rollup/plugin-terser 压缩代码:添加 terser()。
- 使用 @rollup/plugin-replace 替换环境变量:配置 replace({ preventAssignment: true, 'process.env.NODE_ENV': JSON.stringify('production') })。
- 将依赖标记为 external:配置 external: ['react', 'react-dom', 'lodash'],并在 UMD 输出中通过 globals: { react: 'React', 'react-dom': 'ReactDOM' } 提供全局变量映射。
- 使用 peerDependencies 模式:从 package.json 读取 pkg.peerDependencies 或 pkg.dependencies,并展开为 external 数组。
- 代码分割使用多入口:input 配置为对象 { main: 'src/index.js', utils: 'src/utils/index.js' },output 使用 { dir: 'dist', format: 'esm' }。
- 使用动态导入:Rollup 会自动处理 import(),可用于按需加载重模块,如 const module = await import('./heavy-module.js')。
- 优化 tree-shaking:用 /*#__PURE__*/ 注解标记纯函数,如 export const compute = /*#__PURE__*/ createCompute();。
- 在 package.json 中设置 'sideEffects': false,帮助打包器进行 tree-shaking。
- 开发时启用 watch 模式:配置 watch: { include: 'src/**', clearScreen: false },并运行 rollup -c -w。
- 配置 package.json 入口:main 指向 dist/bundle.cjs.js,module 指向 dist/bundle.esm.js,browser 指向 dist/bundle.umd.js,types 指向 dist/index.d.ts;exports 中为 '.' 配置 import 指向 ./dist/bundle.esm.js、require 指向 ./dist/bundle.cjs.js;files 包含 dist;sideEffects 为 false。
- 遵循最佳实践 Do:一致使用 ES6 模块语法;将第三方依赖标记为 external;为调试生成 source maps;使用命名导出以更好 tree-shaking;测试所有输出格式;开发时使用 watch 模式。
- 避免最佳实践 Avoid:不要打包 peer dependencies;ES modules 可用时不要使用 CommonJS 模块;不要忽略包体积;库不要跳过 TypeScript 声明文件。
- 常见库配置模式:综合使用 resolve()、commonjs()、typescript({ tsconfig: './tsconfig.json' })、terser() 插件;external 取 Object.keys(pkg.peerDependencies || {});输出 { file: pkg.main, format: 'cjs', sourcemap: true } 和 { file: pkg.module, format: 'esm', sourcemap: true }。
- 调试与分析:使用 --configDebug 标志调试配置;为所有格式生成 source maps;使用 rollup-plugin-visualizer 分析包;检查输出代码可读性。
适用边界 · 注意事项
- 项目是应用而非库,且已有 Vite、webpack 等打包方案满足需求时,不必仅为打包引入 Rollup。
- 代码大量依赖 CommonJS 且无法改成 ES modules 时,Rollup 的 tree-shaking 优势不明显,可能更适合其他打包器。
- 只需运行简单脚本或无需模块打包时,不需要配置 Rollup。