Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: Only building ES2015 on ng build --prod

I want the production build output to only produce ES2015 builds and not ES5 builds.

I am using Angular 8 with Ivy rendering engine on.

I am also attaching my angular.json, browserslist, tsconfig.json files.

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

browserlist

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

Current build result

The current build creates files but the index.html does not use type module but I want the index.html to use only module script only

Current Index.html Result

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

Expected Index.html Result

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

The resultant built scripts are also not ES2015 ready.

Side Note

If reverting back the browserlist to default configuration provided by angular produces both ES5 and ES2015 results.

like image 360
Max Gaurav Avatar asked Jul 19 '19 07:07

Max Gaurav


People also ask

What does ng build -- prod does?

The ng build command is intentionally for building the apps and deploying the build artifacts. The command does not generate an output folder. The output folder is – dist/. The ng serve builds artifacts from memory instead for a faster development experience.

Is it possible to disable differential loading in Angular?

How To Disable Differential Loading in Angular Applications. Adjust browserslist to only consider the web browsers that we actually want to support. Ensure that we have specified "target": "es2015" as the target in tsconfig. json .


1 Answers

To setup a build to target ES2015 only, do the following:

in your tsconfig.json:

make sure, as you already have, that the target is set to es2015:

"target": "es2015"

and set your browserlist config as follows:

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

Those settings compile for ES2015 only, although, i am not seeing the type attribute included in the script tags as you noted in the resulting index page:

<script src="runtime.HASH.js" type="module"></script>

Someone please correct me if i am wrong, but i do not think the type attribute is necessary unless you intend on embedding additional scripts for legacy browsers which is what you were trying to avoid in the first place and thus, it is not necessary in this scenario. See this link for reference

Lastly, note that you can use the following command to check which browser versions your browserlist config will target:

npx browserslist
like image 176
DevMike Avatar answered Oct 20 '22 06:10

DevMike