Rollup.js overview
Use rollup as a bundler to build web apps
- compiles ES6+ JS code using ESM modules to a standalone bundle :
- bundles can be any flavor of module management system (amd, cjs, es, iife, umd, system)
- source files are statically analyzed, unused functions/variables are removed (treeshaking)
- for the node_modules imports to be resolved as well, the dependencies have to be installed
- compile and resolve all imports to a single js file containing a self-executing function ('iife')
npx rollup source/main.js --file build/bundle.js --format iife
- multiple bundles can be produced from multiple inputs (if
rollup.config.js
exports an array of configurations)
- when publishing ES6+ libraries that may run in pre ES6 environments (
"type": "commonjs"
inpackage.json
) :- use rollup to compile the library to any legacy module management system
- set the
"main"
property ofpackage.json
to point to the compiled version
-
@rollup/plugin-commonjs
: required to turn imported commonJS dependencies to ESM (probably for further processing) -
@rollup/plugin-node-resolve
: required to bundle code located in dependencies (uses node module resolution algorithm) -
@rollup/plugin-babel
: required for transpiling the bundles into pre ES6 code
- important note : babel should NOT process the core-js polyfills (set up the babel plugin exclude option)
- the semantics used by rollup to convert esm modules to cjs are :
- add names as properties of module.exports if an esm namespace is exported :
module.exports.something = something;
- if a default esm export if present :
module.exports = <whatever is exported as default in esm>
- add names as properties of module.exports if an esm namespace is exported :
- see
output.interop
option for details on the above
- a rollup plugin has to be distributed as a package which exports a function that :
- can be called with plugin specific options
- will return a plugin object
- a rollup plugin is an object with one or more :
- properties
- build hooks
- output generation hooks
- hooks are functions that are called at various stages of the build process and allow the plugin code to interact with it :
- by affecting the build process execution
- by providing information about it
- by modifying the build files once the build process is complete
- the different kinds of hooks are :
hook type | comment |
---|---|
sync/async | async hooks return promises resolving to a value of the same type as their input value, otherwise they are sync |
first | if several plugins implement this hook, they are run sequentially until one returns a value other than null or undefined |
sequential | if several plugins implement this hook, they are run in the specified plugin order (if such a hook is async, subsequent ones will wait until the current one's resolution) |
parallel | if several plugins implement this hook, they are run in the specified plugin order (if such a hook is async, subsequent ones execute in paralle of the current one) |
-
build hooks list (plugin-located placeholder functions for code that will execute at various stage of the build process ...) :
buildEnd
buildStart
closeWatcher
load
moduleParsed
options
resolveDynamicImport
resolveId
shouldTransformCachedModule
transform
watchChange
-
details on build hooks :
- see build hooks execution sequence
- build hooks process entries
- build hooks may be called multiple times, each time on a different module, as rollup builds the dependency graph
- build hooks are mainly concerned with locating, providing and transforming input files before treeshaking is performed
- build hooks are dependant on one another for execution a la systemd units
-
ouptut generation hooks :
augmentChunkHash
banner
closeBundle
footer
generateBundle
intro
outputOptions
outro
renderChunk
renderDynamicImport
renderError
renderStart
resolveFileUrl
resolveImportMeta
writeBundle
-
details on ouptut generation hooks :
- see output generation hooks execution sequence
- ouptut generation process chunks
- ouptut generation hooks may be called multiple times, once for each chunk before the actual bundle is rendered
- ouptut generation build hooks are mainly concerned with providing information about a generated bundle and modifying it after treeshaking is performed
- ouptut generation hooks are dependant on one another for execution (a la systemd units)
-
plugin context helpers are functions that can be accessed from inside hooks through ````this``` (hook function thus has to bind to something at runtime)
-
plugin context helpers list :
this.addWatchFile
this.emitFile
this.error
this.getCombinedSourcemap
this.getFileName
this.getModuleIds
this.getModuleInfo
this.getWatchFiles
this.load
this.meta
this.parse
this.resolve
this.setAssetSource
this.warn