This readme explains why attaching Zephyr to Angular CLI is a bit more complicated they just one withZephyr()
line.
Since Angular v8, Angular CLI builds index.html
outside of webpack build process.
Meaning that we can't just add a plugin to webpack and expect it to work.
In a nutshell, Angular CLI does the following:
- Starts webpack build and waits for it to finish
- Builds
index.html
using@angular-devkit/build-angular:html
process.exit(0)
if there are no errors
withZephyr()
attaches to webpack build process, while to have index.html
we need to intercept it in index-html-transform and make Angular CLI wait for deployment to finish.
- Install
zephyr-webpack-plugin
as a dev dependency - Install
@angular-builders/custom-webpack
as a dev dependency (this is needed to modify Angular CLI webpack config) - Create a
webpack.config.js
file in the root of your projectconst {withZephyr} = require('zephyr-webpack-plugin'); // shorthand for: module.exports = (config) => withZephyr({wait_for_index_html: true})(config); module.exports = withZephyr({wait_for_index_html: true});
- Create
index-html-transform.js
file in the root of your projectconst { resolveIndexHtml, onDeploymentDone } = require('zephyr-webpack-plugin'); module.exports = async (targetOptions, indexHtml) => { resolveIndexHtml(indexHtml); await onDeploymentDone(); return indexHtml; };
- Modify build executor in
angular.json
orproject.json
to use custom webpack config and index transform"build": { "builder": "@angular-builders/custom-webpack:browser", "options": { "customWebpackConfig": { "path": "./webpack.config.js" }, "indexTransform": "./index-html-transform.js" } }