Webpack is your Achilles’ Heel
How much time do you spend configuring webpack when you should be improving the overall architecture of your site |
|
Webpack Concepts
Entry Point - the starting point of your application’s dependency graph |
|
Resolve - instructions on how to resolve files |
|
Output - tells webpack how to write the bundled code to disk |
|
Loaders - transforms html, scss/css, jpg into modules that are part of your application dependency |
|
Plugins - performs actions on your compilations / chunks. They do anything that loaders don’t |
|
A Beginner’s Guide to Webpack 2 and Module Bundling
How to chain webpack loaders:
|
|
Why import css in webpack?
|
|
Running |
Webpack included plugins from https://webpack.js.org/plugins/. These are libraries that do not require npm installation:
|
Code splitting - say you have a public facing and a section that requires authentication… this setup usually comprises of shared javascript libraries and application specific ones. These are useful plugins in this scenario:
|
The webpack configuration for such a case requires you to have 2
|
Hot Module Replacement (HMR) - modifies modules while an application is running without the need for a full reload
|
Webpack Deep Dive
Pass arguments to your npm run scripts through a double dash (--) as in |
Useful npm libraries:
|
Webpack Configuration Information |
|
|
Inline sourcemaps in production is not good practice because of the added weight to your file. |
Arbitrary npm scripts can have `pre` and `post` hooks when ran. This means that `npm run something` will automatically have a `presomething` and `postsomething` hooks you can use |
|
Hot Module Replacement - A development feature helpful in form filling scenario where you can avoid updating your modules without refreshing the page which can cause you to lose everything you’ve filled out |
|
Tree Shaking - deletes dead code by pulling out only the pieces you need | You have to use es6 imports/exports for tree shaking to work |
Webpack for Everyone
Multi entry configuration
|
This type of configuration is the equivalent of `require (‘./styles.css’)` if used together with ExtractTextPlugin |
Other useful loaders:
|
In a css build scenario
|
Hash vs chunkhash - hash is global, chunkhash is file specific |
|
Pass a function in `plugins` for a custom plugin https://laracasts.com/series/webpack-for-everyone/episodes/12?autoplay=true |
Do this in your webpack configuration file like so:
One useful technique when hooking your own plugin is to write a JSON file that can be read by your application |
Webpack an Introduction
It’s good practice to separate vendor libraries from your application libraries. These can then be webpack compiled into their own files that can be shared across pages. |
|
Webpack configuration files are stored in environment specific files, e.g. `webpack.prod.js` and `webpack.common.js`. These files are merged using the npm package `webpack-merge` |
Unambiguous Webpack config with Typescript
Webpack’s complexity comes from its flexibility to handle almost anything you require on your web application |
Take advantage of Typescript types in your IDE’s to make setting configuration easier through auto-completion. To do this:
|
Webpack (v1) – A Detailed Introduction
Webpack and module loaders in general solve the problem of bringing in code dependencies without all the manual maintenance |
|
|
|