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.
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.
In Angular 6+ it is ng build --configuration=production
Then put a production configuration in angular.json
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
under the architect section look for "configurations", if you don't have any. try adding one like below.
"configurations": {
"production": {
"project": "projects/PROJECT-NAME/ng-package.json"
}
}
your architect section should like this
"architect": {
"build": {
"builder": "@angular-devkit/build-ng-packagr:build",
"options": {
"tsConfig": "projects/PROJECT-NAME/tsconfig.lib.json",
"project": "projects/PROJECT-NAME/ng-package.json"
},
"configurations": {
"production": {
"project": "projects/PROJECT-NAME/ng-package.json"
}
}
}