Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization build moves private class fields before the super() call for third party dependencies #22486

Closed
2 of 15 tasks
Julien-Marcou opened this issue Jan 12, 2022 · 1 comment · Fixed by #22501
Closed
2 of 15 tasks
Assignees
Labels
Milestone

Comments

@Julien-Marcou
Copy link
Contributor

Julien-Marcou commented Jan 12, 2022

🐞 Bug report

Command (mark with an x)

  • new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • extract-i18n
  • run
  • config
  • help
  • version
  • doc

Is this a regression?

I don't think so

Description

When you import a third party dependecy that uses private class fields (Native private class features), they end up being inserted into the constructor before the super() call.

🔬 Minimal Reproduction

  • Create a new Angular project
  • Add a third party library that uses private class fields ( npm install [email protected])
  • Import the library (import 'scrollable-component'; inside your main.ts)
  • Build the project (ng build or ng serve --configuration production, optimization should be set to true inside your angular.json)
  • The generated code is incorrect

(here is the third party imported file)

🔥 Exception or Error


Uncaught ReferenceError: must call super constructor before using 'this' in derived class constructor
vendor.js

🌍 Your Environment


@angular-devkit/architect       0.1301.3
@angular-devkit/build-angular   13.1.3
@angular-devkit/core            13.1.3
@angular-devkit/schematics      13.1.3
@angular/cli                    13.1.3
@schematics/angular             13.1.3
rxjs                            7.5.2
typescript                      4.5.4

Anything else relevant?

The bug only occurs when your build configuration contains the "optimization": true (or does not contain the "optimization": false) rule, which is the default for the production configuration.

From what I can understand, something like this happens:

// Before optimization
class Test extends HTMLElement {
  #privateField;
  constructor() {
    super();
  }
}
// After optimization
class Test extends HTMLElement {
  constructor() {
    __definePrivateField(this, privateField);
    super();
  }
}

I found this issue evanw/esbuild#1918 which may be related and tried several options in my tsconfig.json, like:

  • "target": "esnext"
  • "module": "esnext"

but I haven't been able to make it work (the problem also occurs with private class methods).

@ngbot ngbot bot modified the milestone: Backlog Jan 13, 2022
@alan-agius4 alan-agius4 self-assigned this Jan 13, 2022
filipesilva pushed a commit that referenced this issue Jan 16, 2022
…Script optimizations

Previously, when ESNext was used, we fallbacked to ES2020 which caused ESBuild to output broken code.

Closes #22486
filipesilva pushed a commit that referenced this issue Jan 16, 2022
…Script optimizations

Previously, when ESNext was used, we fallbacked to ES2020 which caused ESBuild to output broken code.

Closes #22486

(cherry picked from commit b5c4a23)
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants