Angular 应用未使用 es5 转译库
Angular app not using es5 transpiled library
我正在按照 https://angular.io/guide/creating-libraries 上的指南创建库。我使用此配置构建它:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"target": "es5",
"declaration": true,
"inlineSources": true,
"types": [],
"lib": [
"dom",
"es2018"
]
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableResourceInlining": true
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
与开箱即用的内容相比,我唯一改变的是我将目标从 es2015 更改为 es5。
将其发布到 npm 并将其安装到我的应用程序后,我尝试使用 ng serve --configuration es5.
运行 我的应用程序 (Angular 8)
es5 配置说:
"configurations": {
"es5": {
"tsConfig": "./tsconfig.es5.json"
}
}
tsconfig.es5.json:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ES5",
}
}
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./src",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "ESNext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "ES2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"ES2018",
"DOM"
],
"paths": {
"@config/*": ["app/config/*"],
"@core/*": ["app/core/*"],
"@environments/*": ["environments/*"],
"@modules/*": ["app/modules/*"],
"@requests/*": ["app/core/request-interfaces/*"],
"@responses/*": ["app/core/response-interfaces/*"],
"@shared/*": ["app/shared/*"]
}
},
"angularCompilerOptions": {
"enableIvy": false
}
}
当 运行按照描述安装我的应用程序时,我从导入的库中收到此错误:
未捕获的类型错误:Class 构造函数 MyClass 不能在没有 'new'
的情况下被调用
它是由我从包中扩展一个 class 触发的:
export class MyClass extends PackageClass {}
我知道这个错误是由不支持 class 关键字的 es2015 触发的,但是用 es5 目标构建库不应该阻止我的应用程序这样使用它吗?我在库文档中找不到任何关于支持 es5 浏览器的内容。
事实证明,这是由于 Angular 库不向后兼容,仅向前兼容:https://github.com/angular/angular-cli/issues/19909
我正在按照 https://angular.io/guide/creating-libraries 上的指南创建库。我使用此配置构建它:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "../../out-tsc/lib",
"target": "es5",
"declaration": true,
"inlineSources": true,
"types": [],
"lib": [
"dom",
"es2018"
]
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"enableResourceInlining": true
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}
与开箱即用的内容相比,我唯一改变的是我将目标从 es2015 更改为 es5。 将其发布到 npm 并将其安装到我的应用程序后,我尝试使用 ng serve --configuration es5.
运行 我的应用程序 (Angular 8)es5 配置说:
"configurations": {
"es5": {
"tsConfig": "./tsconfig.es5.json"
}
}
tsconfig.es5.json:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ES5",
}
}
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./src",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"module": "ESNext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "ES2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"ES2018",
"DOM"
],
"paths": {
"@config/*": ["app/config/*"],
"@core/*": ["app/core/*"],
"@environments/*": ["environments/*"],
"@modules/*": ["app/modules/*"],
"@requests/*": ["app/core/request-interfaces/*"],
"@responses/*": ["app/core/response-interfaces/*"],
"@shared/*": ["app/shared/*"]
}
},
"angularCompilerOptions": {
"enableIvy": false
}
}
当 运行按照描述安装我的应用程序时,我从导入的库中收到此错误: 未捕获的类型错误:Class 构造函数 MyClass 不能在没有 'new'
的情况下被调用它是由我从包中扩展一个 class 触发的: export class MyClass extends PackageClass {}
我知道这个错误是由不支持 class 关键字的 es2015 触发的,但是用 es5 目标构建库不应该阻止我的应用程序这样使用它吗?我在库文档中找不到任何关于支持 es5 浏览器的内容。
事实证明,这是由于 Angular 库不向后兼容,仅向前兼容:https://github.com/angular/angular-cli/issues/19909