ngx-build-plus v19.0.0
ngx-build-plus
Extend the Angular CLI's default build behavior without ejecting:
- 📄 Extend the default behavior by providing a partial config that just contains your additional settings
- 📄 Alternative: Extend the default behavior by providing a custom function
- 📦 Optional: Build a single bundle (e. g. for Angular Elements)
- ☑️ Inherits from the default builder, hence you have the same options
- ☑️ Provides schematics for some advanced use cases like webpack externals
- 🍰 Simple to use
- ⏏️ No eject needed
Credits
Big thanks to Rob Wormald and David Herges!
Get the right version
- Angular 6-7/ CLI 6-7: ngx-build-plus@^7
- Angular 8/ CLI 8: ngx-build-plus@^8.0.0
- Angular 9/ CLI 9: ngx-build-plus@^9.0.0
- Angular 10/ CLI 10: ngx-build-plus@^10.0.0
- Angular 11/ CLI 11: ngx-build-plus@^11.0.0
- Angular 12/ CLI 12: ngx-build-plus@^12.0.0
- Angular 13/ CLI 13: ngx-build-plus@^13.0.0
- Angular 14/ CLI 14: ngx-build-plus@^14.0.0
- Angular 15/ CLI 15: ngx-build-plus@^15.0.0
- Angular 16/ CLI 16: ngx-build-plus@^16.0.0
- Angular 17/ CLI 17: ngx-build-plus@^17.0.0
- Angular 18/ CLI 18: ngx-build-plus@^18.0.0
Updating to Version 8
ng update ngx-build-plus --forceBreaking Changes
Version 7
- The switch
single-bundlenow defaults tofalseto align with the CLI's default behavior.
Version 9
keepPolyfillsandkeepStylesdefault to true to avoid misunderstandings.
Schematics and Options
Options
ng build --single-bundle: Puts everything reachable from the main entry point into one bundle. Polyfills, scripts, and styles stay in their own bundles as the consuming application might have its own versions of these.
Adding ngx-build-plus
ng add ngx-build-plus
Getting started
This shows a minimal example for getting started. It uses a minimal partial webpack configuration that is merged into the CLI's one. Representative for all possible custom webpack configurations, the used one just leverages the DefinePlugin to create a global VERSION constant during the build.
Please find the example shown here in the sample application in the folder projects/getting-started.
- Create a new Angular project with the CLI
Add ngx-build-plus:
ng add ngx-build-plusNote: If you want to add it to specific sub project in your
projectsfolder, use the--projectswitch to point to it:ng add ngx-build-plus --project getting-startedRemark: This step installs the package via npm and updates your angular.json so that your project uses custom builders for
ng serveandng build.Add a file
webpack.partial.jsto the root of your (sub-)project:const webpack = require('webpack'); module.exports = { plugins: [ new webpack.DefinePlugin({ "VERSION": JSON.stringify("4711") }) ] }Use the global variable VERSION in your
app.component.ts:import { Component } from '@angular/core'; declare const VERSION: string; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'Version: ' + VERSION; }Start your application with the
--extra-webpack-configswitch pointing to your partial webpack config:ng serve --extra-webpack-config webpack.partial.js -oIf your project is a CLI based sub project, use the
--projectswitch too:ng serve --project getting-started -o --extra-webpack-config webpack.partial.jsHint: Consider creating a npm script for this command.
Make sure that the VERSION provided by your webpack config is displayed.
ngx-build-plus and Angular Elements
While ngx-build-plus can be used in every Angular configuration, it also comes with some schematics automating some scenarios for Angular Elements. More information about can be found here.
Using Plugins
Plugins allow you to provide some custom code that modifies your webpack configuration. In addition to that, they also provide a pre- and a post-hook for tasks that need to take happen before and after bundling. This is an example for an plugin:
export default {
pre(options) {
console.debug('pre');
},
config(cfg) {
console.debug('config');
return cfg;
},
post(options) {
console.debug('post');
}
}As this plugin is written with TypeScript you need to compile it.
The config method works like a configHook (see above).
To use a plugin, point to it's JavaScript representation (not the TypeScript file) using the --plugin switch:
ng build --plugin ~dist\out-tsc\hook\pluginThe prefix ~ points to the current directory. Without this prefix, ngx-build-plus assumes that the plugin is an installed node_module.
Using different merging strategies
You can also use plugins to implement different merging strategies. The following plugin demonstrates this:
var merge = require('webpack-merge');
var webpack = require('webpack');
exports.default = {
config: function(cfg) {
const strategy = merge.strategy({
'plugins': 'prepend'
});
return strategy (cfg, {
plugins: [
new webpack.DefinePlugin({
"VERSION": JSON.stringify("4711")
})
]
});
}
}To execute this, use the following command:
ng build --plugin ~my-plugin.jsOne more time, the ~ tells ngx-build-plus that the plugin is not an installed node_module but a local file.
Advanced example: Externals and Angular Elements
Please note, that we don't recomment webpack externals anymore for several reasons (better alternatives, Angular now ships without UMD bundles, etc.). Instead we recomment Webpack Module Federation.
This shows another example for using ngx-build-plus. It uses a custom webpack configuration to define some dependencies of an Angular Element as external which can be loaded separately into the browser and shared among several bundles.
If you are not interested into this very use case, skip this section.
The result of this description can be found in the repository's sample directory.
Create a new Angular CLI based project and install
@angular/elementsas well as@webcomponents/custom-elementswhich provides needed polyfills:npm i @angular/elements --saveExpose a component as an Custom Element:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule, Injector } from '@angular/core'; import { createCustomElement } from '@angular/elements'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule ], declarations: [ AppComponent ], providers: [], bootstrap: [], entryComponents:[AppComponent] }) export class AppModule { constructor(private injector: Injector) { } ngDoBootstrap() { const elm = createCustomElement(AppComponent, { injector: this.injector }); customElements.define('custom-element', elm); } }Install
ngx-build-plus:When using Angular >= 7 and CLI >= 7, you can simply use
ng addfor installingngx-build-plus:ng add ngx-build-plusIf you are using a monorepo, mention the project you want to install ngx-build-plus for:
ng add ngx-build-plus --project myProjectAdd polyfills:
ng g ngx-build-plus:wc-polyfill --project myProjectExecute the externals schematic:
ng g ngx-build-plus:externals --project myProjectThis creates a partial webpack config in your project's root:
module.exports = { "externals": { "rxjs": "rxjs", "@angular/core": "ng.core", "@angular/common": "ng.common", "@angular/platform-browser": "ng.platformBrowser", "@angular/elements": "ng.elements" } }Build your application. You can use the npm script created by the above mentioned schematic:
npm run build:myProject:externalsAngular will now be compiled into a
scripts.jsand can be reused amongs several seperately compiled bundles. Your code is in the main bundle which is quite tiny b/c it does not contain Angular.
Further information about this can be found in my blog here.
Angular Trainings, Consultings, Schulungen
10 months ago
1 year ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago