Angular:仅在ng build --prod上构建ES2015

时间:2019-07-19 07:16:07

标签: javascript angular typescript ecmascript-6 angular-cli

我希望生产构建输出仅生成ES2015构建,而不是ES5构建。

我在启用Ivy渲染引擎的情况下使用Angular 8。

我还将附加我的 angular.json,浏览器列表,tsconfig.json 文件。

angular.json

A   B   C
----------
3   1   1

浏览器列表

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

tsconfig.json

# 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

当前构建结果

当前版本创建文件,但 index.html 不使用模块类型,但我希望index.html仅使用模块脚本

当前Index.html结果

{
  "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结果

<script src="runtime.dd2d878cd9ae88476a26.js"></script>
<script src="polyfills.8ce2a1027d74b4930bab.js"></script>
<script src="scripts.b7617d15d290ae695226.js"></script>
<script src="main.c6dce3ad2ed82428de43.js"></script>

生成的生成脚本也不支持ES2015。

旁注

如果将浏览器列表恢复为angular提供的默认配置,则会同时产生ES5和ES2015结果。

2 个答案:

答案 0 :(得分:0)

1)TSConfig.json文件中存在编译选项

2)您需要设置以下目标以使用ES5进行构建

"target": "es5",

3)这是您可以参考的Configuration

答案 1 :(得分:0)

要设置仅针对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