Skip to content

Incorrect output when transpiling to target ES6 and --prod #21948

Closed

Description

@jgutix

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[X] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

The bundle for production environment is outputting the following invalid code, when using the HttpClientModule

  return new ue.a(t=>{
    const n = this.xhrFactory.build();
    if (n.open(e.method, e.urlWithParams),
      e.withCredentials && (n.withCredentials = !0),
        e.headers.forEach((e,t)=>n.setRequestHeader(e, t.join(","))),
      e.headers.has("Accept") || n.setRequestHeader("Accept", "application/json, text/plain, */*"),
        !e.headers.has("Content-Type")) {
      const t = e.detectContentTypeHeader();
      null !== t && n.setRequestHeader("Content-Type", t)
    }
    if (e.responseType) {
      const t = e.responseType.toLowerCase();
      n.responseType = "json" !== t ? t : "text"
    }
    const r = e.serializeBody();
    let s = null;
    const o = ()=>{
      if (null !== s)
        return s;
      const t = 1223 === n.status ? 204 : n.status
        , r = n.statusText || "OK"
        , o = new zo(n.getAllResponseHeaders())
        , i = ("responseURL"in (n = n) && n.responseURL ? n.responseURL : /^X-Request-URL:/m.test(n.getAllResponseHeaders()) ? n.getResponseHeader("X-Request-URL") : null) || e.url;
      return s = new ei({
        headers: o,
        status: t,
        statusText: r,
        url: i
      })
    }

Especifically the error is in this line:

, i = ("responseURL"in (n = n) && n.responseURL ? n.responseURL : /^X-Request-URL:/m.test(n.getAllResponseHeaders()) ? n.getResponseHeader("X-Request-URL") : null) || e.url;

Because in the above lines the n was declared as constant: const n = this.xhrFactory.build();

Producing the following error in chrome:

ERROR TypeError: Assignment to constant variable.
    at o (main.afd9c695ba9c5bcbf50d.bundle.js:formatted:13612)
    at XMLHttpRequest.i (main.afd9c695ba9c5bcbf50d.bundle.js:formatted:13621)
    at e.invokeTask (polyfills.d14582d1423c077d31d1.bundle.js:1)
    at Object.onInvokeTask (main.afd9c695ba9c5bcbf50d.bundle.js:formatted:1694)
    at e.invokeTask (polyfills.d14582d1423c077d31d1.bundle.js:1)
    at t.runTask (polyfills.d14582d1423c077d31d1.bundle.js:1)
    at t.invokeTask [as invoke] (polyfills.d14582d1423c077d31d1.bundle.js:1)
    at _ (polyfills.d14582d1423c077d31d1.bundle.js:1)
    at XMLHttpRequest.k (polyfills.d14582d1423c077d31d1.bundle.js:1)

Similar in firefox:

ERROR 
TypeError: invalid assignment to const `n'
Stack trace:
o@http://localhost:4201/main.afd9c695ba9c5bcbf50d.bundle.js:1:298093
i@http://localhost:4201/main.afd9c695ba9c5bcbf50d.bundle.js:1:298355
eFQL/</</</c</e.prototype.invokeTask@http://localhost:4201/polyfills.d14582d1423c077d31d1.bundle.js:1:22948
onInvokeTask@http://localhost:4201/main.afd9c695ba9c5bcbf50d.bundle.js:1:27946
eFQL/</</</c</e.prototype.invokeTask@http://localhost:4201/polyfills.d14582d1423c077d31d1.bundle.js:1:22861
eFQL/</</</o</t.prototype.runTask@http://localhost:4201/polyfills.d14582d1423c077d31d1.bundle.js:1:18118
eFQL/</</</u</t.invokeTask@http://localhost:4201/polyfills.d14582d1423c077d31d1.bundle.js:1:24042
_@http://localhost:4201/polyfills.d14582d1423c077d31d1.bundle.js:1:34743
k@http://localhost:4201/polyfills.d14582d1423c077d31d1.bundle.js:1:34975

Expected behavior

A valid code should be outputted when targeting ES6 for production

Minimal reproduction of the problem with instructions

Given the downloadable example at the HTTP chapter of the Tour of heroes tutorial (https://angular.io/generated/zips/toh-pt6/toh-pt6.zip)

  1. Disable HttpClientInMemoryWebApiModule and HttpClientInMemoryWebApiModule in src/app/app.module.ts, so the app try to make an actual XhttpRequest
  2. Change the target to "target": "es6", in tsconfig.json
  3. Run the app like: ng serve --prod

What is the motivation / use case for changing the behavior?

The output from the compilation for ES6 is broken

Environment


Angular version: 5.0.0 and 5.2.2


Browser:
- [X] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [X] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: 8.9.1  
- Platform:  Mac

Others:

Worth noting this issue arises only with these three factors combined:

  1. Use of HttpClientModule
  2. Target = es6
  3. Building/serving with prod

Probably this is an issue that should be fixed in the angular-cli or uglify-webpack-plugin level, but I had no time to dig deeper.

Activity

  1. ethanpollitt commented on Feb 1, 2018

    @ethanpollitt

    Just ran into this issue today after migrating some code to use common/http, with references to old http in another part of the project (hence HttpModule and HttpClientModule both being imported).

    Temporarily solved by reverting usage of common/http.

  2. added
    area: core
    on Feb 1, 2018
  3. zijianhuang commented on Feb 2, 2018

    @zijianhuang

    Me 2. I have a complex business app, and a simple heros app both with HttpClient.

    With "ng build" and "ng build --aot", the apps are working fine.

    With "ng build --prod", the complex business app starts fine with the login screeen, however, when making the first HttpClient call for auth, the browser console will give:
    ERROR TypeError: Assignment to constant variable. at r (main.14d23ee13c3a14c67d46.bundle.js:1) at XMLHttpRequest.a (main.14d23ee13c3a14c67d46.bundle.js:1) at e.invokeTask (polyfills.8e8e9e6cb9f95b7ba9ab.bundle.js:1) at Object.onInvokeTask (main.14d23ee13c3a14c67d46.bundle.js:1) at e.invokeTask (polyfills.8e8e9e6cb9f95b7ba9ab.bundle.js:1) at t.runTask (polyfills.8e8e9e6cb9f95b7ba9ab.bundle.js:1) at t.invokeTask [as invoke] (polyfills.8e8e9e6cb9f95b7ba9ab.bundle.js:1) at _ (polyfills.8e8e9e6cb9f95b7ba9ab.bundle.js:1) at XMLHttpRequest.k (polyfills.8e8e9e6cb9f95b7ba9ab.bundle.js:1)

    The http call for auth is actually successful.

    with "ng build --prod --no-aot", the complex business app could not start properly, with error:
    Uncaught TypeError: Assignment to constant variable. at ih._compileModule (vendor.defd7bfff770299239f9.bundle.js:1) at j.then (vendor.defd7bfff770299239f9.bundle.js:1) at Object.then (vendor.defd7bfff770299239f9.bundle.js:1) at ih._compileModuleAndComponents (vendor.defd7bfff770299239f9.bundle.js:1) at ih.compileModuleAsync (vendor.defd7bfff770299239f9.bundle.js:1) at useClass.compileModuleAsync (vendor.defd7bfff770299239f9.bundle.js:1) at ls.bootstrapModule (vendor.defd7bfff770299239f9.bundle.js:1) at Object.cDNt (main.98d929d5770520d2cd22.bundle.js:1) at o (inline.66056c9a0e270b3547d6.bundle.js:1) at Object.0 (main.98d929d5770520d2cd22.bundle.js:1)

    The simple heros app has similar problem with prod.

    For now, I just use "ng build --aot" for production deployment.

    You may find the simple heros demo app at https://github.com/zijianhuang/webapiclientgen, and locate DemoNGCli.

    Stacks:
    NG 5.2.2
    NG Cli 1.6.6 or 1.6.7

  4. SelfishLucho commented on Feb 2, 2018

    @SelfishLucho

    I just ran into the same problem but with target:ES2015

  5. zijianhuang commented on Feb 2, 2018

    @zijianhuang

    (cont.)
    when target:es5, things become fine. I just tried this in 2 NG2+ apps. So apparently ng build --prod is having some problem with target es2015.

  6. added and removed
    area: core
    on Feb 6, 2018
  7. slanden commented on Feb 8, 2018

    @slanden

    I'm having the same problem as zijianhuang with a login and auth call in my app targeting es2015. except when using the command "ng build -prod -sourcemaps" the error reads
    ERROR TypeError: Assignment to constant variable. at u (http.js.pre-build-optimizer.js:1649) at XMLHttpRequest.i (http.js.pre-build-optimizer.js:1771) at e.invokeTask (zone.js:421) at Object.onInvokeTask (core.js.pre-build-optimizer.js:3979) at e.invokeTask (zone.js:420) at t.runTask (zone.js:188) at t.invokeTask [as invoke] (zone.js:496) at _ (zone.js:1517) at XMLHttpRequest.k (zone.js:1543)

    the line where it breaks in that file reads
    function defaultErrorLogger(console, ...values) { (/** @type {?} */ (console.error))(...values);

  8. zijianhuang commented on Feb 8, 2018

    @zijianhuang

    At this stage, I am not entire sure if the cause is in Angular/HttpClient or in Angular CLI. So I had reported to angular/angular-cli#9495 as well.

  9. johan-- commented on Feb 15, 2018

    @johan--

    Same issue here, resolved by using es5 as target.

  10. MariuszHenn commented on Feb 15, 2018

    @MariuszHenn

    I had the same issue, im not sure why, but removing node_modules and sets packages as below fixed it (target es6)

    Angular CLI: 1.6.8
    Node: 8.9.1
    OS: darwin x64
    Angular: 5.2.3
    ... common, compiler, compiler-cli, core, forms, http
    ... language-service, platform-browser, platform-browser-dynamic
    ... router, service-worker
    
    @angular/cli: 1.6.8
    @angular-devkit/build-optimizer: 0.3.1
    @angular-devkit/core: 0.0.29
    @angular-devkit/schematics: 0.0.52
    @ngtools/json-schema: 1.1.0
    @ngtools/webpack: 1.9.8
    @schematics/angular: 0.1.17
    typescript: 2.6.2
    webpack-dev-server: 2.11.1
    webpack: 3.10.0
    
  11. jameskentTX commented on Feb 18, 2018

    @jameskentTX

    I had the same problem, when target was ES2015. But app compiles and works with:

    1. "target": "es5" - in tsconfig.json
    2. "typescript": "~2.5.3" - in package.json

    I'm using "@angular/cli": "^1.7.0", "@angular/compiler": "^5.2.5"

  12. zijianhuang commented on Feb 22, 2018

    @zijianhuang

    As of CLI 1.7.1 and the Angular 5.2.6, the problem still persists.

  13. DcsMarcRemolt commented on Feb 22, 2018

    @DcsMarcRemolt

    I use @ngtools/webpack@1.10.0 and @angular-devkit/build-optimizer@0.4.0 outside of an angular-cli project (hand written webpack config) and I can also confirm this issue. Going down to es5 fixes it.

  14. trotyl commented on Feb 23, 2018

    @trotyl
    Contributor

    You can use conditionals: false in uglify-es compress option to fix it. Since it's not Angular nor Angular CLI problem, making an issue (by repro without any Angular code) to uglify-es could make it solved sooner.

  15. zijianhuang commented on Feb 23, 2018

    @zijianhuang

    @trotyl, thanks for hint. However, in Angular CLI, there seems no way to to define the uglify-es option in .angular-cli.json, or through other means.

    Can you give more hint?

    Anyway, I will report this to uglifyJS2.

  16. added this to the needsTriage milestone on Feb 26, 2018
  17. Cito commented on Mar 2, 2018

    @Cito

    Same problem here with Angular-CLI 1.7.2. When I set conditionals: false to const uglifyCompressOptions = { ... } in node_modules/@angular/cli/models/webpack-configs/production.js, as suggested by @trotyl, the problem disappears.

  18. zijianhuang commented on Mar 2, 2018

    @zijianhuang

    Apparently the solution is that NG CLI provides such switch for es2015. It seems this issue could be closed here, while I had reported to angular/angular-cli#9495.

  19. trotyl commented on Mar 10, 2018

    @trotyl
    Contributor
  20. pkozlowski-opensource commented on Jun 20, 2018

    @pkozlowski-opensource
    Member
  21. angular-automatic-lock-bot commented on Sep 13, 2019

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  22. locked and limited conversation to collaborators on Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions