在项目 'my-lib' 中找不到配置'production'



我正在用 Angular 6.1.0 构建一个库

  • ng new lib-demo
  • ng generate library my-lib

所有文章都建议使用如下所示的--prod标志运行库的构建:

ng build my-lib --prod

但是,这会引发错误

Configuration 'production' could not be found in project 'my-lib'.

这可能是正确的,因为当我查看angular.json时,库项目中没有production build configuration的定义。它仅存在于应用程序项目中。

以下是我在使用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"
}
}

所以这里的问题是,是否不再需要--prod标志,只需运行ng build m-lib就会生成生产构建?

查看dist文件夹的内容,看起来是这样,但我不是100%确定。如果有人能验证这一点,那就太好了。

从 6.1 版开始,Angular 总是对我们库进行生产构建,即在新版 Angular 中,我们在构建它时不再需要--prod标志,库总是在 AOT 模式下构建。为了确保这一点,您可以在 Angular-CLI 存储库中查看这些问题:

https://github.com/angular/angular-cli/issues/12290

https://github.com/angular/angular-cli/issues/12226

还有这篇文章("构建库">部分(:

https://blog.angularindepth.com/creating-a-library-in-angular-6-87799552e7e5

如果您仍在使用 6.0.x(或更低版本(,则需要在构建库时使用 --prod 标志。

如果需要,您仍然可以选择将configuration作为参数传递:ng build --configuration=configuration(请参阅文档(。如有必要,您可以在angular.json中指定构建规则,例如,对于production构建:

"configurations": {
"production": {
// Options here
}
}

并且命令应该是ng build --configuration=production.

在 Angular 6+ 中,它是ng build --configuration=production

然后将生产配置放入 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
}
}

在架构师部分下查找"配置"(如果没有(。 尝试添加一个如下所示的。

"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"
}
}
}

不再需要--prod,因为库始终在 AOT 模式下构建,而没有 --prod 您只需要运行 ng build

最新更新