ngx-build-plus
ngx-build-plus is an Angular CLI extension that allows developers to customize the webpack build process without needing to "eject" the Angular application. It enables integration of custom webpack configurations or functions, offering a flexible way to modify how Angular projects are built and served. The current stable version is 20.0.0, tightly coupled to Angular CLI 20.x, ensuring compatibility with the latest Angular ecosystem. It typically releases new major versions in alignment with Angular's major releases. Key differentiators include its non-ejecting approach, support for `ng serve` and `ng build` parity, schematics for common advanced use cases like webpack externals, and options for building single bundles, useful for Angular Elements. It provides a simple API for merging partial webpack configurations or using a custom hook function to modify the default CLI webpack config.
Common errors
-
NG8002: Unknown builder 'ngx-build-plus:browser'.
cause The `angular.json` file is not correctly configured to use `ngx-build-plus` builders, likely due to a failed or incomplete installation.fixRun `ng add ngx-build-plus` in your project root to automatically update `angular.json` and install the package. Ensure the `--project` flag is used if targeting a sub-project. -
Error: Cannot find module 'webpack' when running ng build/serve with --extra-webpack-config.
cause Your `webpack.partial.js` or custom config hook tries to `require('webpack')` but `webpack` is not installed as a direct dependency in your project.fixInstall `webpack` as a development dependency: `npm install webpack --save-dev` or `yarn add webpack --dev`. -
Schema validation failed for the 'extra-webpack-config' option. Value "path/to/non-existent-config.js" is not a valid path.
cause The path provided to the `--extra-webpack-config` flag is incorrect, the file does not exist, or the file is malformed (e.g., not exporting a valid webpack configuration object).fixVerify the path to your `webpack.partial.js` (or similar) file is correct and that the file exports a standard webpack configuration object. Check for typos in the file path or name. -
The `single-bundle` option (or `keepPolyfills`, `keepStyles`) is not producing the expected output after upgrade.
cause The default behavior of these options changed in major versions (e.g., `single-bundle` in v7, `keepPolyfills`/`keepStyles` in v9).fixExplicitly set the affected option in your `angular.json` builder options or via the CLI flag (e.g., `--single-bundle=true`, `--keepPolyfills=false`) to enforce the desired behavior, overriding the new defaults.
Warnings
- breaking The `single-bundle` switch default behavior changed from `true` to `false` in version 7. This aligns `ngx-build-plus` with the Angular CLI's default multi-bundle behavior.
- breaking The `--keepPolyfills` and `--keepStyles` options now default to `true` since version 9. This change was introduced to avoid confusion and ensure polyfills and styles are retained by default.
- gotcha ngx-build-plus is tightly coupled to Angular CLI and Angular versions. Always ensure you install the `ngx-build-plus` version that matches your project's Angular/CLI major version (e.g., ngx-build-plus@^20 for Angular 20).
- gotcha The recommended way to install and configure ngx-build-plus is by using `ng add ngx-build-plus`. This command automatically updates your `angular.json` to use the custom builders.
- gotcha Custom webpack configurations specified via `--extra-webpack-config` are only applied if the flag is explicitly provided during `ng build` or `ng serve`.
Install
-
npm install ngx-build-plus -
yarn add ngx-build-plus -
pnpm add ngx-build-plus
Imports
- Builder Configuration
In angular.json: "builder": "@angular-devkit/build-angular:browser"
In angular.json: "builder": "ngx-build-plus:browser"
- webpack module
const webpack = require('webpack'); - Config Hook Function
In angular.json options: "configHook": "path/to/my-webpack-hook.js"
Quickstart
const webpack = require('webpack');
module.exports = {
plugins: [
new webpack.DefinePlugin({
"VERSION": JSON.stringify("4711")
})
]
};
// src/app/app.component.ts
import { Component } from '@angular/core';
declare const VERSION: string; // Declare global variable defined by webpack
@Component({
selector: 'app-root',
template: `<h1>{{ title }}</h1>`,
styles: ['h1 { color: blue; }']
})
export class AppComponent {
title = 'Version: ' + VERSION;
}