rollup.js is a JavaScript packaging tool. This article describes its basic usage.
1. Introduction
The purpose of the packager tool is to combine multiple JavaScript scripts into a single script for the browser.
Browsers need script packaging for three main reasons.
- Early browsers did not support modules, and large web projects could only be merged into a single script before execution.
- The module mechanism of Node.js is not compatible with browsers and must be handled for compatibility by packaging tools.
- Browsers load one large script with better performance than multiple small scripts.
Currently, the most commonly used packaging tool is Webpack, which is powerful, but difficult to learn and use, and has been criticized.
rollup.js was developed with the intention of creating an easy-to-use ES module packaging tool that doesn’t need to be configured and is straightforward to use. This it did.
As it evolved, it could also package CommonJS modules. However, this requires complex configuration and is actually not much simpler than Webpack.
So it is recommended that only rollup.js be used to package ES modules so that it can take full advantage of it. As you’ll see below, that’s a very simple thing to do.
If your project uses CommonJS modules, it is not recommended to use rollup.js, there is not much advantage.
2. Installation
This article uses a global installation of rollup.js.
|
|
However, you can also use it directly without installing it. Just replace rollup
in all the commands below with npx rollup
.
The first time you use it, you can run the following command and check the help.
3. Example
Below, two simple scripts are packaged using rollup.js: the library file add.js and the entry script main.js.
In the above code, the module add.js outputs two tool functions addPi()
and addE()
.
In the above code, the entry script main.js loads the tool function addPi()
inside add.js.
Then, it is packaged with rollup.js.
|
|
To package, simply give the entry script main.js and rollup will automatically package the dependencies.
The result is output to the screen by default.
As you can see, the import
and export
statements are gone and have been replaced with the original code.
Also, the function addE()
is not packaged because it is not used. This feature is called tree-shaking, which means that unused code is automatically removed when packaging.
Because of these two things, rollup outputs very neat code and is smaller than other packaging tools.
Use the parameter -file [FILENAME]
to save the result to the specified file.
|
|
The above command saves the package results to bundle.js.
4. Notes on use
-
If there are multiple entry scripts, fill in their filenames in turn and specify the output directory with the parameter
--dir
.1
$ rollup m1.js m2.js --dir dist
The above command will generate multiple files in the directory
dist
, packaged: m1.js, m2.js, and their common dependencies (if any). -
The parameter
--format iife
, will put the packing result inside an auto-execute function.1
$ rollup main.js --format iife
-
If you want to minimize the code after packaging, use the parameter
--compact
.1
$ rollup main.js --compact
Another approach is to use specialized tools.
1
$ rollup main.js | uglifyjs --output bundle.js
The above command is divided into two steps, the first step is to rollup the package, the second step is to uglifyjs for code minimization, and finally write the bundle.js.
-
rollup supports using config file (rollup.config.js) to write all parameters in it, here is an example.
The
-c
parameter enables the profile.1
$ rollup -c
I do not recommend using configuration files, which add additional complexity. The default scenario is that command line parameters are sufficient and easier to read.
5. Converting to CommonJS modules
Finally, rollup also supports converting ES modules to CommonJS modules, using the parameter -format cjs
.
|
|
The converted CommonJS module with the following code.
Reference http://www.ruanyifeng.com/blog/2022/05/rollup.html