如何在 <lib> angular8 升级中将 public_api.ts 重命名为 public-api.ts 时修复 ng 构建错误



我正在为我公司使用"ng 生成库"构建的组件库将角度 7 迁移到角度 8。我试图尽可能接近标准的角度 cli 构建。我创建了一个全新的应用程序和一个带有"@angular/cli"的库:"~8.2.0"。

https://github.com/Annie-Huang/my-angular8-app/tree/create-lib

我注意到在角度 8 中,他们将 public_api.ts 更改为 public-api.ts https://github.com/Annie-Huang/my-angular8-app/blob/create-lib/projects/ea-ui/src/public-api.ts

所以我将现有库中的入口文件从 public_api.ts 重命名为 public-api.ts 并在ng-package.json中更新它

我得到这个错误

Bradleys-MacBook-Pro:ea-component-library anniehuang$ ng build ea-ui
Building Angular Package
ERROR: error TS6053: File '/Users/anniehuang/projects/ea-component-library/projects/ea-ui/src/public_api.ts' not found.
An unhandled exception occurred: error TS6053: File '/Users/anniehuang/projects/ea-component-library/projects/ea-ui/src/public_api.ts' not found.
See "/private/var/folders/sw/1rxh903n6y39kkwbgr737n8m0000gp/T/ng-SE3jbI/angular-errors.log" for further details.

当我在我自己的存储库(ng build ea-ui(中在全新的 angular 8 应用程序中构建它时。我没有这个错误。

当我对公司组件库进行全局搜索时,没有 public_api.ts 字符串。

ng-package.json:
---------------------
{
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/ea-ui",
"lib": {
"entryFile": "src/public-api.ts"
}
}
public-api.ts (file content not change, just renamed):
----------------------------------------------------------
export * from './lib/accordion/accordion.component';
export * from './lib/accordion/accordion.module';
export * from './lib/autocomplete/autocomplete-trigger.directive';
export * from './lib/autocomplete/autocomplete.component';
export * from './lib/autocomplete/autocomplete.module';
....
Bradleys-MacBook-Pro:ea-component-library anniehuang$ ng build ea-ui
Building Angular Package
ERROR: error TS6053: File '/Users/anniehuang/projects/ea-component-library/projects/ea-ui/src/public_api.ts' not found.
An unhandled exception occurred: error TS6053: File '/Users/anniehuang/projects/ea-component-library/projects/ea-ui/src/public_api.ts' not found.
See "/private/var/folders/sw/1rxh903n6y39kkwbgr737n8m0000gp/T/ng-SE3jbI/angular-errors.log" for further details.
Open up angular-errors.log:
------------------------------
Bradleys-MacBook-Pro:~ anniehuang$ cat /private/var/folders/sw/1rxh903n6y39kkwbgr737n8m0000gp/T/ng-MET38P/angular-errors.log
[error] Error: error TS6053: File '/Users/anniehuang/projects/ea-component-library/projects/ea-ui/src/public_api.ts' not found.
at analyseEntryPoint (/Users/anniehuang/projects/ea-component-library/node_modules/ng-packagr/lib/ng-v5/init/analyse-sources.transform.js:45:15)
at MapSubscriber.exports.analyseSourcesTransform.rxjs_1.pipe.operators_1.map.graph [as project] (/Users/anniehuang/projects/ea-component-library/node_modules/ng-packagr/lib/ng-v5/init/analyse-sources.transform.js:15:9)
at MapSubscriber._next (/Users/anniehuang/projects/ea-component-library/node_modules/rxjs/internal/operators/map.js:49:35)
at MapSubscriber.Subscriber.next (/Users/anniehuang/projects/ea-component-library/node_modules/rxjs/internal/Subscriber.js:66:18)
at SwitchMapSubscriber.notifyNext (/Users/anniehuang/projects/ea-component-library/node_modules/rxjs/internal/operators/switchMap.js:86:26)
at InnerSubscriber._next (/Users/anniehuang/projects/ea-component-library/node_modules/rxjs/internal/InnerSubscriber.js:28:21)
at InnerSubscriber.Subscriber.next (/Users/anniehuang/projects/ea-component-library/node_modules/rxjs/internal/Subscriber.js:66:18)
at MapSubscriber._next (/Users/anniehuang/projects/ea-component-library/node_modules/rxjs/internal/operators/map.js:55:26)
at MapSubscriber.Subscriber.next (/Users/anniehuang/projects/ea-component-library/node_modules/rxjs/internal/Subscriber.js:66:18)

其实找到了原因。事实证明,如果您图书馆的package.jsonngPackage部分,它将把信息带到那里,而不是ng-package.json中的信息。如果该ngPackage部分没有entryfile,则默认情况下需要src/public_api.ts

因此,在失败的情况下,我的文件内容是: 在/projects/ea-ui/package.json 中:

{
"name": "@ea/ea-ui",
"version": "1.13.5",
"peerDependencies": {
"@angular/common": "^8.2.0",
"@angular/core": "^8.2.0"
},
"dependencies": {
"dayjs": "^1.8.15",
"ngx-take-until-destroy": "^5.4.0",
"ngx-mask": "^8.0.2",
"@angular/cdk": "^8.1.2"
},
"ngPackage": {
"dest": "../../dist/ea-ui",
"whitelistedNonPeerDependencies": [
"dayjs",
"ngx-take-until-destroy",
"ngx-mask",
"@angular/cdk"
],
"lib": {
"styleIncludePaths": [
"./src/assets/scss"
]
}
}
}

在/projects/ea-ui/ng-package.json 中:

{
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/ea-ui",
"lib": {
"entryFile": "src/public-api.ts"
}
}

现在我将其更改为以下内容 它通过: 在/projects/ea-ui/package.json 中:

{
"name": "@ea/ea-ui",
"version": "1.13.5",
"peerDependencies": {
"@angular/common": "^8.2.0",
"@angular/core": "^8.2.0"
},
"dependencies": {
"dayjs": "^1.8.15",
"ngx-take-until-destroy": "^5.4.0",
"ngx-mask": "^8.0.2",
"@angular/cdk": "^8.1.2"
}
}

在/projects/ea-ui/ng-package.json 中:

{
"$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
"dest": "../../dist/ea-ui",
"lib": {
"styleIncludePaths": [
"./src/assets/scss"
],
"entryFile": "src/public-api.ts"
},
"whitelistedNonPeerDependencies": [
"dayjs",
"ngx-take-until-destroy",
"ngx-mask",
"@angular/cdk"
]
}

最新更新