swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
30.93k stars 1.21k forks source link

add `/*#__PURE__*/` for ts enum #9385

Open SoonIter opened 1 month ago

SoonIter commented 1 month ago

Describe the feature

at present, swc transforms enum without /*#__PURE__*/, which causes enums cannot be deleted during treeshaking and minimizing

playground

// before
enum A {
  A = 1,
  B = 2
}

enum B {
  A = 1,
  B = 2
}

export { A, B }

// after
var A;
(function(A) {
    A[A["A"] = 1] = "A";
    A[A["B"] = 2] = "B";
})(A || (A = {}));
var B;
(function(B) {
    B[B["A"] = 1] = "A";
    B[B["B"] = 2] = "B";
})(B || (B = {}));
export { A, B };

It is very common in large type files (more than 3,000 lines) and I think this is a very important optimization.

image

Babel plugin or link to the feature description

https://babeljs.io/repl#?browsers=iOS%20%3E%3D%209%2C%0AAndroid%20%3E%3D%204.4%2C%0Alast%202%20versions%2C%0A%3E%200.2%25%2C%0Anot%20dead&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=KYOwrgtgBAglDeAoKsoF4oEYA0yoCF0oAmRAXyA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Cstage-2%2Ctypescript&prettier=false&targets=&version=7.25.3&externalPlugins=&assumptions=%7B%7D

Additional context

rspack repo https://github.com/SoonIter/rspack-enum-treeshaking-pure

image

SoonIter commented 1 month ago

related to typescript: https://github.com/microsoft/TypeScript/issues/27604 esbuild: https://github.com/evanw/esbuild/blob/main/CHANGELOG-2021.md

SoonIter commented 1 month ago

repeated https://github.com/swc-project/swc/issues/4453