Here I would share my understanding according to the webpack guide . And by the way point out those points that our project touches.
A JS Static Module bundler.
- Bundle dynamically by dependency graph
- Bundle static modules
- An NPM package(so it’s based on Node.js)
- Not a task runner(e.g. Grunt, Gulp, npm scripts)
-
Modulization need while develop.
JS
- CommonJS — require(‘.a.js’)
- AMD — requirejs
- ES6 — import
CSS
- SCSS
- LESS
Images, fonts, .etc
-
After modulized, project files are not runnable by itself.
- Needs translation, bundling
-
It’s the result of evolution
- NPM Script
- Out of the box.
- Too simple, not many hooks available.
- Grunt
- Tasks + dependency
- Flexible, Reusable plugins that encapsulate common tasks
- Needs lots of config
- Gulp
- Workflow
- task, run, watch, src, dest
- Stronger than Grunt, but also needs lots of config to run.
- Webpack
- Everything is a module.
- Good at handling modulized projects; Plugins; Active community;
- Cons: could only adapt to modulized projects.
- NPM Script
-
- npx webpack --param
- npx webpack --config webpack.config.js
- npm scripts
-
Core concepts —Take config file from our project as an example
-
entry What we use as the roots of dependency graph
-
output Define where the bundled files would go to publicPath: When insert bundled files to index.html, the prefix to add before filename. —change it to watch the effect .
-
loaders — Asset Management: CSS/SCSS, Images/Font files, data files
-
plugins — Output Management and others, more powerful than loaders.
HtmlWebpackPlugin
- Add bundles.
- Replace variables
- Generate new index.html
CleanWebpackPlugin -- Clean up before build
-
- Lazy-load
- Code splitting — Optimise load time. * Entry Points * Prevent Duplication — CommonsChunkPlugin * Dynamic Imports
- Caching * hash
- Tree-shaking — UglifyJSPlugin
- Source map
- DevServer + Hot Module Replacement
- Shimming
- Provide package as global
- Browser polyfill
- Babel — to support ES2015
- PWA support, offline web APP
- webpack-merge — divide config to common, develop, production.