webpack、rollup 和 parcel 是现代前端模块打包工具,负责将代码转换为浏览器可运行的格式。gulp 是一个任务运行器,用于自动化工作流。browserify 是早期的模块打包工具,允许在浏览器中使用 CommonJS。这些工具解决了代码分割、依赖管理和资源优化的问题,但各自的设计理念和适用场景不同。
在前端工程化领域,选择合适的构建工具直接影响开发效率和项目维护成本。webpack、rollup、parcel、gulp 和 browserify 代表了不同时期的解决方案。虽然它们都能处理代码转换,但核心目标和使用方式有很大区别。本文将从配置、入口处理、插件扩展和维护状态四个维度进行深度对比。
不同的工具对配置文件的依赖程度不同。有的追求零配置,有的追求完全可控。
webpack 需要明确的配置文件,通常命名为 webpack.config.js。
// webpack: webpack.config.js
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: __dirname + '/dist'
},
module: {
rules: [{ test: /\.css$/, use: 'style-loader' }]
}
};
rollup 也需要配置文件,通常命名为 rollup.config.js。
// rollup: rollup.config.js
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'iife'
},
plugins: [require('@rollup/plugin-node-resolve')()]
};
parcel 主打零配置,通常不需要配置文件。
.parcelrc。// parcel: .parcelrc (可选)
{
"extends": "@parcel/config-default",
"transformers": {
"*.js": ["@parcel/transformer-js"]
}
}
gulp 使用 gulpfile.js 定义任务流。
// gulp: gulpfile.js
const { src, dest } = require('gulp');
exports.build = function() {
return src('src/*.js').pipe(dest('dist'));
};
browserify 主要通过命令行或 API 使用,较少使用配置文件。
-t 参数添加转换。# browserify: CLI
browserify src/main.js -o dist/bundle.js -t babelify
处理代码入口的方式决定了工具的灵活性。
webpack 支持灵活的入口定义。
// webpack: 多入口配置
module.exports = {
entry: {
main: './src/main.js',
admin: './src/admin.js'
},
output: {
filename: '[name].bundle.js'
}
};
rollup 通常专注于单入口。
// rollup: 多格式输出
export default {
input: 'src/index.js',
output: [
{ file: 'dist/bundle.cjs.js', format: 'cjs' },
{ file: 'dist/bundle.esm.js', format: 'es' }
]
};
parcel 自动识别入口类型。
# parcel: 自动识别
parcel build src/index.html
gulp 使用 glob 匹配文件。
// gulp: 文件流匹配
exports.styles = function() {
return src('src/scss/**/*.scss').pipe(dest('dist/css'));
};
browserify 指定单个入口文件。
require()。// browserify: API 用法
const browserify = require('browserify');
browserify('./src/main.js').bundle().pipe(require('fs').createWriteStream('bundle.js'));
当默认功能不足时,插件系统决定了工具的上限。
webpack 拥有最庞大的插件生态。
plugins 数组添加功能。// webpack: 插件示例
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
plugins: [new HtmlWebpackPlugin({ template: './src/index.html' })]
};
rollup 插件专注于模块转换。
plugins 数组中。// rollup: 插件示例
import terser from '@rollup/plugin-terser';
export default {
plugins: [terser()]
};
parcel 插件系统较新,基于配置。
.parcelrc 指定转换器。// parcel: 自定义转换器
{
"transformers": {
"*.md": ["@parcel/transformer-markdown"]
}
}
gulp 插件基于流式处理。
.pipe() 连接插件。// gulp: 插件链
const uglify = require('gulp-uglify');
exports.minify = function() {
return src('src/*.js').pipe(uglify()).pipe(dest('dist'));
};
browserify 使用 Transform 流。
-t 或 API 添加转换。// browserify: Transform
const b = require('browserify')();
b.transform('babelify');
b.bundle().pipe(require('fs').createWriteStream('bundle.js'));
工具的生命周期直接影响项目长期维护。
webpack 处于活跃维护状态(v5)。
rollup 处于活跃维护状态(v3/v4)。
parcel 处于活跃维护状态(v2)。
gulp 处于维护状态(v4)。
browserify 处于低维护状态。
| 特性 | webpack | rollup | parcel | gulp | browserify |
|---|---|---|---|---|---|
| 核心定位 | 应用打包 | 库打包 | 零配置打包 | 任务运行 | 早期模块打包 |
| 配置难度 | 高 | 中 | 低 | 中 | 低 |
| 模块支持 | CommonJS/ESM | 优先 ESM | 通用 | 文件流 | CommonJS |
| 代码分割 | 强大 | 支持 | 自动 | 不支持 | 不支持 |
| 热更新 | 支持 (HMR) | 支持 | 支持 | 需插件 | 需插件 |
| 当前状态 | ✅ 活跃 | ✅ 活跃 | ✅ 活跃 | ⚠️ 辅助 | ❌ 遗留 |
webpack 是大型应用的安全选择 🏢。如果你需要处理复杂的路由、资源加载和代码分割,它的生态能解决几乎所有问题。虽然配置繁琐,但可控性最高。
rollup 是库开发的标准工具 📦。如果你发布 npm 包,它能生成最干净的代码,且对 Tree-shaking 支持最好。不要用它构建复杂的应用页面。
parcel 是原型开发的神器 🚀。如果你想在几分钟内启动项目且不想配置 Webpack,它是最佳选择。适合中小型项目或内部工具。
gulp 是自动化脚本的补充 🤖。不要用它打包 JS 模块,但可以用它处理图片压缩、文件复制等构建后的任务。
browserify 是历史的一部分 🕰️。除非你正在维护 5 年前的老项目,否则不要在新架构中引入它。现代工具在性能和体验上都远超它。
总结:新应用首选 webpack 或 parcel,开发库首选 rollup,辅助任务用 gulp,避免使用 browserify。
选择 rollup 如果你主要开发 JavaScript 库或组件。它对 ES 模块支持最好,生成的代码最干净,树摇(tree-shaking)效果优秀,适合需要发布 npm 包的场景。
选择 webpack 如果你构建大型复杂的应用程序。它拥有最强大的插件系统和代码分割能力,适合需要精细控制构建流程、加载各种资源类型的大型企业级项目。
选择 gulp 如果你需要编排复杂的构建任务流程,比如压缩图片、编译 Sass 或部署文件。它不适合单独作为模块打包器,通常与 webpack 或 rollup 配合使用来处理非代码资源。
仅在选择维护旧项目时使用 browserify。它是最早让浏览器支持 require() 的工具,但已不再适合新架构。现代打包工具提供了更好的性能和生态支持,新项目应避免使用。
选择 parcel 如果你想要零配置启动项目。它适合快速原型开发或中小型应用,默认支持多种格式,无需编写复杂的配置文件,能自动处理大多数构建需求。
Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application. It uses the standardized ES module format for code, instead of previous idiosyncratic solutions such as CommonJS and AMD. ES modules let you freely and seamlessly combine the most useful individual functions from your favorite libraries. Rollup can optimize ES modules for faster native loading in modern browsers, or output a legacy module format allowing ES module workflows today.
Install with npm install --global rollup. Rollup can be used either through a command line interface with an optional configuration file or else through its JavaScript API. Run rollup --help to see the available options and parameters. The starter project templates, rollup-starter-lib and rollup-starter-app, demonstrate common configuration options, and more detailed instructions are available throughout the user guide.
These commands assume the entry point to your application is named main.js, and that you'd like all imports compiled into a single file named bundle.js.
For browsers:
# compile to a <script> containing a self-executing function
rollup main.js --format iife --name "myBundle" --file bundle.js
For Node.js:
# compile to a CommonJS module
rollup main.js --format cjs --file bundle.js
For both browsers and Node.js:
# UMD format requires a bundle name
rollup main.js --format umd --name "myBundle" --file bundle.js
Developing software is usually easier if you break your project into smaller separate pieces, since that often removes unexpected interactions and dramatically reduces the complexity of the problems you'll need to solve, and simply writing smaller projects in the first place isn't necessarily the answer. Unfortunately, JavaScript has not historically included this capability as a core feature in the language.
This finally changed with ES modules support in JavaScript, which provides a syntax for importing and exporting functions and data so they can be shared between separate scripts. Most browsers and Node.js support ES modules. However, Node.js releases before 12.17 support ES modules only behind the --experimental-modules flag, and older browsers like Internet Explorer do not support ES modules at all. Rollup allows you to write your code using ES modules, and run your application even in environments that do not support ES modules natively. For environments that support them, Rollup can output optimized ES modules; for environments that don't, Rollup can compile your code to other formats such as CommonJS modules, AMD modules, and IIFE-style scripts. This means that you get to write future-proof code, and you also get the tremendous benefits of...
In addition to enabling the use of ES modules, Rollup also statically analyzes and optimizes the code you are importing, and will exclude anything that isn't actually used. This allows you to build on top of existing tools and modules without adding extra dependencies or bloating the size of your project.
For example, with CommonJS, the entire tool or library must be imported.
// import the entire utils object with CommonJS
var utils = require('node:utils');
var query = 'Rollup';
// use the ajax method of the utils object
utils.ajax('https://api.example.com?search=' + query).then(handleResponse);
But with ES modules, instead of importing the whole utils object, we can just import the one ajax function we need:
// import the ajax function with an ES import statement
import { ajax } from 'node:utils';
var query = 'Rollup';
// call the ajax function
ajax('https://api.example.com?search=' + query).then(handleResponse);
Because Rollup includes the bare minimum, it results in lighter, faster, and less complicated libraries and applications. Since this approach is based on explicit import and export statements, it is vastly more effective than simply running an automated minifier to detect unused variables in the compiled output code.
Rollup can import existing CommonJS modules through a plugin.
To make sure your ES modules are immediately usable by tools that work with CommonJS such as Node.js and webpack, you can use Rollup to compile to UMD or CommonJS format, and then point to that compiled version with the main property in your package.json file. If your package.json file also has a module field, ES-module-aware tools like Rollup and webpack will import the ES module version directly.
This project exists thanks to all the people who contribute. [Contribute]. . If you want to contribute yourself, head over to the contribution guidelines.
Thank you to all our backers! 🙏 [Become a backer]
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
TNG has been supporting the work of Lukas Taegert-Atkinson on Rollup since 2017.