Angular:仅在 ng build --prod 上构建 ES2015
Posted
技术标签:
【中文标题】Angular:仅在 ng build --prod 上构建 ES2015【英文标题】:Angular: Only building ES2015 on ng build --prod 【发布时间】:2019-11-28 03:31:48 【问题描述】:我希望生产构建输出仅产生 ES2015 构建而不是 ES5 构建。
我正在使用带有 Ivy 渲染引擎的 Angular 8。
我还附上了我的 angular.json、browserslist、tsconfig.json 文件。
angular.json
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects":
"front-end-inventory":
"projectType": "application",
"schematics":
"@schematics/angular:component":
"style": "scss"
,
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect":
"build":
"builder": "@angular-devkit/build-angular:browser",
"options":
"outputPath": "dist/front-end-inventory",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.app.json",
"aot": true,
"assets": [
"src/favicon.ico",
"src/assets",
"src/manifest.webmanifest",
"src/.htaccess"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.scss"
],
"scripts": [
"./node_modules/jquery/dist/jquery.slim.min.js"
]
,
"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,
"budgets": [
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
],
"serviceWorker": true,
"ngswConfigPath": "ngsw-config.json"
,
"staging":
"fileReplacements": [
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.staging.ts"
],
"optimization": true,
"outputHashing": "all",
"sourceMap": true,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true,
"budgets": [
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
],
"serviceWorker": true,
"ngswConfigPath": "ngsw-config.json"
,
"serve":
"builder": "@angular-devkit/build-angular:dev-server",
"options":
"browserTarget": "front-end-inventory:build"
,
"configurations":
"production":
"browserTarget": "front-end-inventory:build:production"
,
"extract-i18n":
"builder": "@angular-devkit/build-angular:extract-i18n",
"options":
"browserTarget": "front-end-inventory:build"
,
"test":
"builder": "@angular-devkit/build-angular:karma",
"options":
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "tsconfig.spec.json",
"karmaConfig": "karma.conf.js",
"assets": [
"src/favicon.ico",
"src/assets",
"src/manifest.webmanifest",
"src/.htaccess"
],
"styles": [
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
"src/styles.scss"
],
"scripts": []
,
"lint":
"builder": "@angular-devkit/build-angular:tslint",
"options":
"tsConfig": [
"tsconfig.app.json",
"tsconfig.spec.json",
"e2e/tsconfig.json"
],
"exclude": [
"**/node_modules/**"
]
,
"e2e":
"builder": "@angular-devkit/build-angular:protractor",
"options":
"protractorConfig": "e2e/protractor.conf.js",
"devServerTarget": "front-end-inventory:serve"
,
"configurations":
"production":
"devServerTarget": "front-end-inventory:serve:production"
,
"defaultProject": "front-end-inventory"
浏览器列表
# This file is used by the build system to adjust CSS and JS output to support the specified browsers below.
# For additional information regarding the format and rule options, please see:
# https://github.com/browserslist/browserslist#queries
# You can see what browsers were selected by your queries by running:
# npx browserslist
Chrome > 70
tsconfig.json
"compileOnSave": false,
"compilerOptions":
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"noImplicitAny": true,
"strictNullChecks": true,
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
当前构建结果
当前构建创建文件,但 index.html 不使用类型模块,但我希望 index.html 仅使用模块脚本
当前 Index.html 结果
<script src="runtime.dd2d878cd9ae88476a26.js"></script>
<script src="polyfills.8ce2a1027d74b4930bab.js"></script>
<script src="scripts.b7617d15d290ae695226.js"></script>
<script src="main.c6dce3ad2ed82428de43.js"></script>
预期的 Index.html 结果
<script src="runtime.dd2d878cd9ae88476a26.js" type="module"></script>
<script src="polyfills.8ce2a1027d74b4930bab.js" type="module"></script>
<script src="scripts.b7617d15d290ae695226.js" type="module"></script>
<script src="main.c6dce3ad2ed82428de43.js" type="module"></script>
生成的构建脚本也不支持 ES2015。
旁注
如果将浏览器列表恢复为 Angular 提供的默认配置会产生 ES5 和 ES2015 的结果。
【问题讨论】:
我正在与类似的事情作斗争。我看到了可能有用的关于 ng build 的信息:“es5BrowserSupport - 已弃用:这将根据 'browserslist' 文件中指定的支持浏览器列表确定。 - 启用有条件加载的 ES2015 polyfills。” link 更新:我认为 Angular 9 将为我的问题提供解决方案。一旦我测试了它,我会发布它。 我很想知道您是否尝试过我在下面提供的配置。这对我来说是有效的。 Angular 9 在这方面有什么变化? 【参考方案1】:要设置仅针对 ES2015 的构建,请执行以下操作:
在你的tsconfig.json
:
请确保将目标设置为 es2015:
"target": "es2015"
并设置您的browserlist
配置如下:
last 2 Chrome versions
last 2 Chromeandroid versions
last 2 Safari versions
last 2 ios versions
last 2 Firefox versions
last 2 FirefoxAndroid versions
last 2 Edge versions
这些设置仅针对 ES2015 编译,但我没有看到脚本标签中包含 type
属性,正如您在生成的索引页面中所指出的那样:
<script src="runtime.HASH.js" type="module"></script>
如果我错了,请有人纠正我,但我认为 type
属性是不必要的,除非您打算为旧版浏览器嵌入额外的脚本,这是您首先要避免的,因此,它在这种情况下没有必要。看到这个link for reference
最后,请注意,您可以使用以下命令检查您的 browserlist
配置将针对哪些浏览器版本:
npx browserslist
【讨论】:
以上是关于Angular:仅在 ng build --prod 上构建 ES2015的主要内容,如果未能解决你的问题,请参考以下文章
ng serve 和 ng build 的 cli config 中的不同资产 [Angular 5]
Angular ng build --watch 似乎在 Angular 版本 11 中被破坏了
Angular 8 ng-build 使用科尔多瓦抛出 MIME 错误
Angular 8 - ng build prod 在 ng serve 工作时显示错误 - 模块导入的意外值“未定义”