Giter VIP home page Giter VIP logo

Comments (2)

evanw avatar evanw commented on May 12, 2024 1

I have tried to add various /* @__PURE__ */ annotations, but the class A does not get tree shaken.

The way to add a @__PURE__ annotation for the class A is as follows:

https://esbuild.github.io/try/#dAAwLjE5LjExAC0tdHJlZS1zaGFraW5nPXRydWUAY29uc3QgbWV0aG9kID0gJ21ldGhvZCc7CgpsZXQgQSA9IC8qIEBfX1BVUkVfXyAqLyAoKCkgPT4gY2xhc3MgewogIGdyZWV0KCkgewogICAgY29uc29sZS5sb2coJ2hlbGxvIGZyb20gQScpOwogIH0KICAKICBbbWV0aG9kXSgpIHsKICAgIHJldHVybiAnQSc7CiAgfQp9KSgpCgpjbGFzcyBCIHsKICBncmVldCgpIHsKICAgIGNvbnNvbGUubG9nKCdoZWxsbyBmcm9tIEInKTsKICB9Cn0KCm5ldyBCKCkuZ3JlZXQoKTs

You cannot add the pure annotation to the method variable reference like this because that's annotating the wrong thing. The @__PURE__ annotation says the result can be removed if it's not used. Except it is being used (as the name of the method) so the annotation doesn't have any effect. That's why it has to go around the whole class declaration. Another way to think about it: Just because the evaluation of the expression method has no side effects doesn't necessarily mean the evaluation of the ToString operation on that value also has no side effects.

The above example is deliberately simple to demonstrate the issue, but the context where it came from is that any class that declares a custom inspect function like below prevents treeshaking:

[Symbol.for('nodejs.util.inspect.custom')]() {
  return '...';
}

I appreciate you mentioning the actual use case. It's possible that esbuild could special-case this syntax to still allow tree shaking in this case.

from esbuild.

quentinadam avatar quentinadam commented on May 12, 2024

Thank you ! I appreciate you taking the time to explain the correct way to place the /* @PURE */ annotation !

from esbuild.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.