What is webpack? https://webpack.js.org
Webpack is a module bundler. It turns source files into deployable web application artifacts.
- Processes JavaScript and JSON by default.
- Can be extended to process other file types.
- Creates a
compiler
instance which crawls import tree from entry points. - Compiler populates a
compilation
object as it traverses. - Provides hooks to give loaders and plugins access to the compiler and compilation object to perform additional, custom operations.
- Outputs bundles.
Examples of various webpack configs, generally increasing in complexity.
For each subdirectory:
npm install
For 01-03:
./node_modules/.bin/webpack
For 04-14:
npm start
- Webpack without a configuration file.
mode
argument (development
orproduction
)dist
output directory- Take a look at the output bundle!
- Run
node dist/main.js
for kicks.
- Webpack with a simple configuration file.
- Uses default configuration file name/location
- A mildly customized configuration file, demonstrating some basic settings.
- Example of how dev/prod configs start to differ
- Fingerprinting
- Basic example of having environment-specific (dev, prod) configurations.
- Common configuration entry point which branches on NODE_ENV.
- Extracting common configuration elements into a separate file, merged into environment configs.
- Usage with webpack-dev-server (very common).
- Note generated HTML file.
- Note that bundle is not written to filesystem. It's stored in memory.
- Running webpack-dev-middleware and webpack-hot-middleware with express
- Note how hot reloading becomes difficult.
- Needs a "real" HTML file.
- Using some common loaders for other file types.
- There are lots of loaders: https://webpack.js.org/loaders/
- If you didn't realize my love for my cat earlier, you will now.
- Using some common plugins for more exciting output.
- There are lots of plugins: https://webpack.js.org/plugins/
- Using babel-loader for JavaScript transpilation.
- Example of babel preset-env.
- Note how output changes (
const
, for instance) if the output target is changed to"ie": "11"
- Basic example of authoring a plugin.
- Many more hooks are available:
- Having multiple entry points and common code extraction.
- Using DLL plugins to extract third party code for faster builds / caching.
- Note: Example 13 (DLLs) is currently a bit broken. It doesn't properly exclude jquery from the main application bundle.
- Code splitting with dynamic imports.
import()