Web Front-end Notes - Bundling
Webpack
Webpack is used to compile JavaScript modules. Once installed, you can interact with webpack either from its CLI or API.
- webpack:
- webpack-cli: If you’re using webpack v4 or later and want to call
webpack
from the command line, you’ll also need to install the CLI.
Commonly used options in webpack.config.js
:
entry
: Tell webpack the starting point to build the dependency graph.output
: Tell webpack the output file or folder it should put.target
: Instructs webpack to target a specific environment. Defaults to'browserslist'
or to'web'
when no browserslist configuration was found.
Dependency Graph:
Any time one file depends on another, webpack treats this as a dependency. This allows webpack to take non-code assets, such as images or web fonts, and also provide them as dependencies for your application. Starting from these entry points, webpack recursively builds a dependency graph that includes every module your application needs, then bundles all of those modules into a small number of bundles - often, just one - to be loaded by the browser.
Webpack Concepts:
Entry
: An entry point indicates which module webpack should use to begin building out its internal dependency graph.Output
: The output property tells webpack where to emit the bundles it creates and how to name these files.Loaders
: Out of the box, webpack only understands JavaScript and JSON files. Loaders allow webpack to process other types of files and convert them into valid modules that can be consumed by your application and added to the dependency graph.test
: Thetest
property identifies which file or files should be transformed.use
: Theuse
property indicates which loader should be used to do the transforming.
Plugins
: While loaders are used to transform certain types of modules, plugins can be leveraged to perform a wider range of tasks like bundle optimization, asset management and injection of environment variables.Mode
: By setting themode
parameter to eitherdevelopment
,production
ornone
, you can enable webpack’s built-in optimizations that correspond to each environment. The default value isproduction
. Learn more about the mode configuration and what optimizations take place on each value.
Webpack with TypeScript
- typescript: The TypeScript language and compiler package.
- ts-loader: TypeScript loader for webpack.
ts-loader
usestsc
, the TypeScript compiler, and relies on yourtsconfig.json
configuration. Make sure to avoid settingmodule
to “CommonJS”, or webpack won’t be able to tree-shake your code.
Rollup
Rollup specializes in outputting ES modules which can be used directly by the browser.
- Tree-Shaking: Rollup analyzes the code you are importing, and exclude anything that isn’t actually used.
- Compatibility:
- Importing CommonJS modules: Rollup can import existing CommonJS modules through a plugin.
- Publishing ES Modules: You can use Rollup to compile to UMD or CommonJS format which can be used by NodeJs and webpack.
See more at Rollup Introduction page.
Plugins
By default, rollup only support import es modules, to load other data as modules, we need plugins.
- Plugins: Plugins are used to process different kind of data for importing.
- Output Plugins: are applied specifically to some outputs. They can only modify code after the main analysis of Rollup has completed. One possible use-case is minification of bundles to be consumed in a browser.
For more details, see Using Plugins.
Tricky notes on Plugins
- The default typescript plugin @rollup/plugin-typescript is problematic, use rollup-plugin-typescript2 instead.
- The @rollup/plugin-node-resolve plugin locates modules using the Node resolution algorithm, for using third party modules in
node_modules
.browser: true
: instructs the plugin to use the browser module resolutions inpackage.json
and adds'browser'
toexportConditions
if it is not present so browser conditionals in exports are applied.
- The @rollup/plugin-commonjs plugin converts CommonJS modules to ES6, so they can be included in a Rollup bundle.
transformMixedEsModules: true
will allow mixed importing syntax ofimport
andrequire
expressions.
- The @rollup/plugin-alias does NOT apply to style importing in
.vue
files, need to handle that inrollup-plugin-vue
(https://github.com/vuejs/rollup-plugin-vue) plugin.
SystemJs
In-browser Javascript modules features supported by systemjs
:
- import maps
- one file with multiple modules
- Introspection of the module registry.
const vueUrl = import.meta.resolve('vue');
const currentModuleUrl = import.meta.url;
- JSON modules, CSS modules, HTML modules and wasm(WebAssembly) modules.