Note-02-09-spring-webpack
Spring
Remember to includes
spring-boot-starter-webwhich contains web dependenciesMaven dependency have
scope, a dependency is only visiable in the correctscope, say only test file in test folder can access to denpendencies intestscope
Webpack --continue
Code splitting
Three approaches:
Entry Points: Manually split with
entryPrevent Duplication: use
SplitChunksPluginDynamic Imports: split code via inline func call
Entry points
Issues: By default, it is a static code splitting that treat bundles as independent self-contained code.
If there are any duplicated modules between entry chunks they will be included in both bundles.
It isn't as flexible and can't be used to dynamically split code with the core application logic.
Can solve be SplitChunksPlugin.
Prevent Duplication
// ...
optimization: {
splitChunks: {
chunks: 'all',
},
}
// ...Entrys will be chunked into bundles with shared vendors file. There are more plugins to split chunks: mini-css-extract-plugin, bundle-loader, etc
Tips: The chunks will be loaded by jsonp
Dynamic Imports
Old: require.ensure(); New: import(); Webpack use import() as promise out of box, it will be transformed to a internal promisefy module loading. For browser support, polyfill need to be added.
import()calls use promises internally. If you useimport()with older browsers, remember to shim Promise using a polyfill such ases6-promiseorpromise-polyfill.
import() can be use with async and await with babel transpile.
Prefetch
import(/* webpackPrefetch: true */ 'LoginModal');
<link rel="prefetch" href="login-modal-chunk.js">
Parallel loading with parent chunk.
Preload:
import(/* webpackPreload: true */ 'ChartingLibrary');
<link rel="preload">
Loading while browser is idling.
webpack will add the prefetch hint once the parent chunk has been loaded.
How it works in browser:
Browser will load the resource in advance. When the module is referenced, webpack will append the script tag(as usual) and use jsonp to load the resource, browser will now serve the resource with preload/prefetched cache.
Last updated
Was this helpful?