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

Optimize ES2015 enums #13488

Closed
filipesilva opened this issue Jan 21, 2019 · 3 comments · Fixed by #13495
Closed

Optimize ES2015 enums #13488

filipesilva opened this issue Jan 21, 2019 · 3 comments · Fixed by #13495
Labels
area: devkit/build-optimizer feature Issue that requests a new feature
Milestone

Comments

@filipesilva
Copy link
Contributor

🚀 Feature request

Description

Using the ES2015 target, TypeScript transpiles enums as consts plus property assignments.

As an example, the ChangeDetectionStrategy enum in https://github.com/angular/angular/blob/13eb57a59fd4db27e88edb188947297b6637177f/packages/compiler/src/core.ts#L82-L85:

export enum ChangeDetectionStrategy {
  OnPush = 0,
  Default = 1
}

Is transpiled to ES2015 as:

const ChangeDetectionStrategy = {
    OnPush: 0,
    Default: 1
};
ChangeDetectionStrategy[ChangeDetectionStrategy.OnPush] = "OnPush";
ChangeDetectionStrategy[ChangeDetectionStrategy.Default] = "Default";

The property assignment in the transpiled code prevents the class from being removed by minifiers such as terser.

Describe the solution you'd like

Build Optimizer converts ES2015 enums in such a way that they are not retained after minification.

@michaeljota
Copy link

You could try const enum. They are not available at runtime, and can only be used by the reference.

@filipesilva filipesilva added feature Issue that requests a new feature area: devkit/build-optimizer labels Jan 22, 2019
@ngbot ngbot bot added this to the Backlog milestone Jan 22, 2019
@filipesilva
Copy link
Contributor Author

I've seen this happen on the Angular core esm2015 bundle, but the fesm2015 uses the transform we currently have. Might be a matter of detection.

@michaeljota we want to be able to do these transformations on arbitrary third party code, where a user most likely not has the option to change underlying source.

mgechev pushed a commit that referenced this issue Jan 22, 2019
…mitted by tsickle

tsickle emits es2015 enums with an object literal followed by an export declaration

Example:
```
      const RendererStyleFlags3 = {
          Important: 1,
          DashCase: 2,
      };
      export { RendererStyleFlags3 };
      RendererStyleFlags3[RendererStyleFlags3.Important] = 'Important';
      RendererStyleFlags3[RendererStyleFlags3.DashCase] = 'DashCase';
```

This PR adds support for the enums to be optimized by wrapping them in an iife and marks them as pure.

Fixes #13488
@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 Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: devkit/build-optimizer feature Issue that requests a new feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants