Angular build for production fails

2020-07-29 02:19发布

问题:

When I try to build for production my angular application with the following command ng build --prod --build-optimizer I get the following output:

Date: 2019-01-29T08:39:12.193Z
Hash: 05648c76cf91b1245f68
Time: 27920ms
chunk {0} runtime.1eee2a92f0ed121267d0.js (runtime) 2.22 kB [entry] [rendered]
chunk {1} 1.71c6b320e46d84e7f880.js () 88.8 kB  [rendered]
chunk {2} main.058aa675d38205a01c2e.js (main) 1.65 MB [initial] [rendered]
chunk {3} polyfills.6226af78bf8dfb509e3e.js (polyfills) 61.7 kB [initial] [rendered]
chunk {4} styles.dec6a5db702d518085c9.css (styles) 61.2 kB [initial] [rendered]
chunk {5} 5.639dab8d0d9c763ecce1.js () 639 kB  [rendered]
chunk {6} 6.eeb0ec1b5f6ee2bc4225.js () 130 kB  [rendered]

ERROR in 5.639dab8d0d9c763ecce1.js from Terser
--> Unexpected token: punc ()) [5.639dab8d0d9c763ecce1.js:4086,34] <--
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! second-frontend@0.0.0 build_prod: `ng build --prod --build-optimizer`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the second-frontend@0.0.0 build_prod script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Building for development and serve the application works fine so I suspect it might be related to the uglify process. The angular json configuration for production is as follows:

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

and the angular version and packages are:

Angular CLI: 7.2.3
Node: 11.1.0
OS: win32 x64
Angular: 7.2.0
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.12.1
@angular-devkit/build-angular     0.12.1
@angular-devkit/build-optimizer   0.12.1
@angular-devkit/build-webpack     0.12.1
@angular-devkit/core              7.2.1
@angular-devkit/schematics        7.2.3
@angular/cdk                      7.2.1
@angular/cli                      7.2.3
@angular/flex-layout              7.0.0-beta.23
@angular/material                 7.2.1
@ngtools/webpack                  7.2.1
@schematics/angular               7.2.3
@schematics/update                0.12.3
rxjs                              6.3.3
typescript                        3.2.2
webpack                           4.23.1

finally, the tsconfig.json file has the following content:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@app/*": ["src/app/*"],
      "@env/*": ["src/environments/*"]
    },
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

If I change angular.json "SourceMap" option from false to true and try to execute the production build it builds without throwing any error:

Date: 2019-01-29T08:47:50.705Z
Hash: 369c42b07abe57a96302
Time: 42331ms
chunk {0} runtime.90bf2a53d53699d5ef63.js, runtime.90bf2a53d53699d5ef63.js.map (runtime) 2.28 kB [entry] [rendered]
chunk {1} 1.9ac663afcd9a15f9ad98.js, 1.9ac663afcd9a15f9ad98.js.map () 88.9 kB  [rendered]
chunk {2} main.e3c4184e7cbc49903adc.js, main.e3c4184e7cbc49903adc.js.map (main) 1.65 MB [initial] [rendered]
chunk {3} polyfills.6c3e7ecffd41b4bb0d68.js, polyfills.6c3e7ecffd41b4bb0d68.js.map (polyfills) 61.7 kB [initial] [rendered]
chunk {4} styles.cb7c8ca5b54a30c69879.css, styles.cb7c8ca5b54a30c69879.css.map (styles) 61.2 kB [initial] [rendered]
chunk {5} 5.72034e2cb610bc9b5476.js, 5.72034e2cb610bc9b5476.js.map () 251 kB  [rendered]
chunk {6} 6.45111c2fd642148bbdd3.js, 6.45111c2fd642148bbdd3.js.map () 131 kB  [rendered]

Process finished with exit code 0

Any suggestion?

EDIT:
running ng build --prod --optimization=false compiles perfectly

回答1:

As Anton Mitsev stated, the problem was a trailing coma. When removing the trailing the error disappears.