I am building a library with Angular 6.1.0
ng new lib-demo
ng generate library my-lib
All the articles advise running the build for the library with a --prod
flag like so:
ng build my-lib --prod
However, this throws an error
Configuration 'production' could not be found in project 'my-lib'.
Which is probably correct because when I look at the angular.json there is no definition for a production build configuration
in the library project. It is present for the application project only.
the following is what I have under the build configuration for library project that uses ng-packagr
"build": { "builder": "@angular-devkit/build-ng-packagr:build", "options": { "tsConfig": "projects/my-lib/tsconfig.lib.json", "project": "projects/my-lib/ng-package.json" } }
So the question here is that is the --prod
flag not required anymore and just running ng build m-lib
will generate a prod build?
Looking at the contents of dist folder it looks so but I am not 100% sure. If someone could validate this, it will be great.
The angular.json file at the root level of an Angular workspace provides workspace-wide and project-specific configuration defaults. These are used for build and development tools provided by the Angular CLI.
Angular CLI uses JSON Schema to enforce the configuration schema. The Angular team created Schematics packages which are used by the CLI. We can configure the options of Schematics packages, as we please, for the root project and internal projects as well.
Beginning with version 6.1, Angular always does a production build of our library, i.e. in new versions of Angular we don't need the --prod
flag anymore when building it, libraries are always built in AOT mode. To ensure, you can take a look at these issues in Angular-CLI repository:
https://github.com/angular/angular-cli/issues/12290
https://github.com/angular/angular-cli/issues/12226
And this article ("Building the Library" section):
https://blog.angularindepth.com/creating-a-library-in-angular-6-87799552e7e5
If you are still using version 6.0.x (or lower) you will want to use the --prod flag when building your library.
You still have an option to pass a configuration
as a parameter if needed: ng build --configuration=configuration
(see docs). If necessary, you can specify build rules in angular.json
, for example, for production
build:
"configurations": { "production": { // Options here } }
And the command should be ng build --configuration=production
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With