これらのツールは JavaScript アプリケーションの構築プロセスを支える重要なインフラです。webpack と rollup はモジュールを束ねるバンドラーとして機能し、gulp はタスク自動化を専門とし、parcel は設定不要のビルド体験を提供します。browserify は歴史的に重要な CommonJS バンドラーですが、現在は維持モードに近い状態です。プロジェクトの規模や目的に応じて、適切なツールを選ぶことが開発効率とパフォーマンスに直結します。
現代のフロントエンド開発において、コードをどのように束ねて配信するかは重要な決定です。webpack、rollup、parcel、gulp、browserify はそれぞれ異なる歴史と目的を持って誕生しました。これらを正しく理解し、プロジェクトに合ったツールを選ぶことは、長期的なメンテナンス性に影響します。ここでは、実際の設定コードや動作の違いを通して、各ツールの特徴を比較します。
ツールごとに設定の考え方が異なります。明示的に書くほど制御できますが、手間も増えます。
webpack は設定ファイルを明示的に書く必要があります。
webpack.config.js にエントリーポイントや出力先を定義します。// webpack.config.js
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: __dirname + '/dist'
}
};
rollup も設定ファイルを書きますが、構造はシンプルです。
rollup.config.js で入力と出力フォーマットを指定します。// rollup.config.js
export default {
input: 'src/index.js',
output: {
file: 'dist/bundle.js',
format: 'es'
}
};
parcel は設定ファイルが基本的に不要です。
// package.json
{
"scripts": {
"dev": "parcel src/index.html",
"build": "parcel build src/index.html"
}
}
gulp はタスク定義ファイルを使います。
gulpfile.js に処理の流れを関数で書きます。// gulpfile.js
const { src, dest } = require('gulp');
function build() {
return src('src/*.js').pipe(dest('dist'));
}
exports.build = build;
browserify は CLI または API で指定します。
# CLI usage
browserify src/index.js -o dist/bundle.js
JavaScript のモジュール形式への対応は、ツールの得意不得意が分かれるポイントです。
webpack は CommonJS と ESM の両方を混在させて扱えます。
// webpack handles both seamlessly
const lib = require('some-lib'); // CommonJS
import utils from './utils'; // ESM
rollup は ESM を第一に考えて設計されています。
// rollup prefers ESM
import utils from './utils';
export default function main() { /*...*/ }
parcel も両方を自動で処理します。
// parcel auto-detects
import React from 'react';
const mod = require('./module');
gulp 自体はモジュール形式を変換しません。
// gulp needs plugins for bundling
const browserify = require('browserify');
// gulp task pipes files through browserify
browserify は CommonJS をブラウザで動かすために作られました。
// browserify focuses on CommonJS
const dep = require('dependency');
module.exports = function() { /*...*/ };
JavaScript 以外のファイルをどう扱うかも重要な選定基準です。
webpack はローダーを使ってあらゆるファイルを扱えます。
// webpack.config.js
module.exports = {
module: {
rules: [
{ test: /\.css$/, use: ['style-loader', 'css-loader'] }
]
}
};
rollup はプラグインでアセットを扱います。
// rollup.config.js
import postcss from 'rollup-plugin-postcss';
export default {
plugins: [postcss()]
};
parcel は最初からアセット対応しています。
// parcel handles assets automatically
import './style.css';
import logo from './logo.png';
gulp はストリーム処理でアセットを移動または変換します。
// gulpfile.js
const cssmin = require('gulp-cssmin');
function styles() {
return src('src/*.css').pipe(cssmin()).pipe(dest('dist'));
}
browserify はアセット処理が苦手です。
// browserify needs transforms
browserify('main.js')
.transform('css-modulesify')
.bundle();
ツールのメンテナンス状況は、長期的なプロジェクトにおいてリスク管理に関わります。
webpack は現在も活発に開発されています。
rollup も積極的に更新されています。
parcel はバージョン 2 になり安定しました。
gulp はメンテナンスされていますが、役割が変わりました。
browserify は事実上のレガシーツールです。
それぞれのツールには明確な役割があります。目的に合わせて選ぶことが重要です。
webpack または parcel が適しています。複雑なら webpack、手軽さなら parcel です。rollup 一択です。出力コードの品質が最も優れています。gulp よりも npm scripts や Makefile を検討してください。それでも不足する場合に gulp です。browserify は既存システムの維持以外では使いません。ツール選びは正解が一つではありません。プロジェクトの要件とチームのスキルセットに合わせて、最適な組み合わせを選んでください。
JavaScript ライブラリやフレームワークを開発する際に最も適しています。ツリーシェイキング機能に優れており、クリーンな ESM 出力を生成できます。アプリケーション全体をビルドするよりも、部品を作るのに適しています。
複雑な依存関係や多様なアセットを扱う大規模アプリケーションに最適です。ローダーやプラグインによる拡張性が非常に高く、細かな制御が必要な場合に選ばれます。設定の学習コストは高いですが、その分柔軟性があります。
ファイルの変換やサーバー起動など、ビルド以外のタスク自動化を必要とする場合に選びます。バンドル機能単体ではなく、ワークフロー全体を管理したい時に有効です。バンドル自体は webpack や rollup に任せ、gulp はその前後の処理に使います。
小さなプロジェクトや CommonJS のみの環境で動作させる場合に適しています。しかし、現代の機能や最適化機能は不足しているため、新規の大規模プロジェクトでは避けるべきです。既存のレガシーコードを維持する必要がある場合を除き、他の選択肢を検討してください。
設定ファイルを一切書きたくない場合や、プロトタイプを素早く作成したい時に最適です。標準機能で多くの形式に対応しており、学習コストが低いのが特徴です。細かい制御が必要になった時に壁にぶつかる可能性があります。
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.