Getting Started with Rollup
Offical Document:
https://rollupjs.org/introduction/
Introduction to Rollup
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 custom solutions like CommonJS and AMD. Rollup allows you to write your code using ES modules, and then efficiently bundle it for production.
Unlike webpack, which is designed primarily for applications, Rollup excels at bundling libraries and focuses on producing smaller, more efficient bundles by leveraging ES modules.
Creating Your First Bundle
1 |
|
Create a simple project:
1 |
|
src/main.js
1 |
|
src/foo.js
1 |
|
1 |
|
save the bundle
1 |
|
Using Config Files
Instead of using command-line arguments, you can create a configuration file:
rollup.config.mjs
1 |
|
1 |
|
Installing Rollup locally
For project-specific installations:
1 |
|
Update your package.json
:
1 |
|
Using pluings
Install a plugin to handle JSON files:
1 |
|
Update src/main.js
1 |
|
rollup.config.mjs
1 |
|
Comprehensive Rollup Configuration
Rollup’s configuration can be quite extensive.
Below is a comprehensive guide to the major configuration options.
Basic Configuration Structure
A comprehensive Rollup config might look like this:
1 |
|
Key Configuration Options
Input Options
input (String | String[] | { [entryName: string]: string }): The entry point(s) for your bundle.
external (Array | Function | RegExp): IDs of modules that should remain external to the bundle.
plugins (Array): An array of plugin objects to be applied to the bundle.
cache (Object): Previous bundle. Used to speed up incremental builds.
Output Options
file (String): The file to write to. Cannot be used with
dir
.format (String): The format of the generated bundle. Options:
amd
- Asynchronous Module Definitioncjs
- CommonJSes
oresm
- ES modulesiife
- Immediately-invoked Function Expressionumd
- Universal Module Definitionsystem
- SystemJS
dir (String): Directory for chunk files. Used for code-splitting or multiple entry points.
name (String): Variable name for IIFE/UMD bundles.
globals (Object): Map of external imports to global variables for UMD/IIFE bundles.
sourcemap (Boolean | ‘inline’ | ‘hidden’): Controls source map generation.
banner/footer (String): Code to prepend/append to the bundle.
Advanced Configuration
Code Splitting
Rollup supports code splitting by specifying multiple entry points or using dynamic imports:
1 |
|
Tree Shaking
One of Rollup’s strengths is tree shaking - eliminating unused code.
This works automatically when using ES modules, but can be affected by certain plugins and configurations.
Preserving Modules
1 |
|
Popular Rollup Plugins
Rollup’s functionality can be extended with plugins:
- @rollup/plugin-node-resolve: Locates modules using the Node resolution algorithm.
1 |
|
- @rollup/plugin-commonjs: Converts CommonJS modules to ES modules.
1 |
|
- @rollup/plugin-babel: Integrates Babel with Rollup.
1 |
|
- @rollup/plugin-terser: Minifies the bundle using Terser.
1 |
|
- @rollup/plugin-replace: Replace strings in the code with other values.
1 |
|
Babel and Its Common Plugins
Introduction to Babel
Babel is a JavaScript compiler that transforms modern JavaScript code into backward compatible versions for older browsers or environments. It’s commonly used with Rollup to ensure wide browser compatibility.
Setting Up Babel with Rollup
1 |
|
Configure Babel with Rollup:
1 |
|
Create a Babel configuration file:
1 |
|
Common Babel Presets
Presets are collections of plugins used to support particular language features.
- @babel/preset-env:
Transforms ES2015+ code to be compatible with specified environments.- Purpose: Automatically determines plugins needed based on browser targets.
- Features: Transforms new JavaScript syntax, doesn’t transform new JavaScript APIs.
1 |
|
- @babel/preset-react:
Transforms JSX syntax for React applications.- Purpose: Provides plugins for React development.
- Features: Transforms JSX into
React.createElement()
calls, supports React development mode.
1 |
|
- @babel/preset-typescript:
Transforms TypeScript code to JavaScript.- Purpose: Strips TypeScript type annotations.
- Features: Allows using TypeScript without requiring the TypeScript compiler.
1 |
|
Common Babel Plugins
Beyond presets, individual plugins can add specific functionality:
- @babel/plugin-transform-runtime: Externalizes references to helpers and builtins.
- Purpose: Avoids duplication of helper functions in every file.
- Use case: When building libraries or larger applications.
1 |
|
- @babel/plugin-proposal-decorators: Enables decorator syntax support.
- Purpose: Transforms decorator syntax for classes and class properties.
- Use case: When using libraries like MobX or TypeScript decorators.
1 |
|
- @babel/plugin-proposal-class-properties: Transforms static class properties.
- Purpose: Enables class instance and static properties.
- Use case: When using modern class syntax features.
1 |
|
- @babel/plugin-proposal-object-rest-spread: Transforms rest and spread properties.
- Purpose: Enables object rest/spread syntax.
- Use case: When using modern object manipulation syntax.
1 |
|
- @babel/plugin-syntax-dynamic-import: Adds support for the dynamic import syntax.
- Purpose: Enables parsing of
import()
syntax for code splitting. - Use case: When implementing code splitting in applications.
- Purpose: Enables parsing of
1 |
|
Complete Example: Rollup with Babel and TypeScript
Here’s a comprehensive example combining Rollup, Babel, and TypeScript:
Install dependencies:
1 |
|
rollup.config.mjs
:
1 |
|
tsconfig.json
:
1 |
|