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

Proposal: more annotations #32

Open
KermanX opened this issue Dec 6, 2024 · 0 comments
Open

Proposal: more annotations #32

KermanX opened this issue Dec 6, 2024 · 0 comments

Comments

@KermanX
Copy link
Owner

KermanX commented Dec 6, 2024

#__PURE__ and #__NO_SIDE_EFFECT__ have been widely supported. Their spec can be found at https://github.com/javascript-compiler-hints/compiler-notations-spec/.

For this tree-shaker, we may introduce some other annotations for a better tree-shake result. The following are some possible annotations to add:

#__FINITE_RECURSION__

Used before a function declaration/expression, indicating this function's recursed call can be analyzed in finite steps.

/* #__FINITE_RECURSION__ */
function render(comp) {
  if (comp.alwaysFalsy) {
    // Treeshakable with the help of the annotation
  }
  const result = renderSelf(comp)
  // A recursed call. Considered as an unknown call without the annotation.
  result.children = comp.children.map(render)
}

#__ALWAYS_PLAIN__

Used before an object expression, indicating this object will always be used like a Map: no getters/setters, no special properties.

const someMap = /* #__ALWAYS_PLAIN__ */ {}
unknownFunction(someMap)
someMap[key]  // This property access is considered side-effectless
someMap.x = 1 // Can be removed if `someMap` is no longer used

#__FORGET__

Forget a property of an object, indicating it will no longer be used. If the property is not used before this annotation, it'll be removed.

const app = {
  mount(root, options) {
    /* #__FORGET__ */ app.mount
    if (options.ssr) {
      // ... Really long code, now can be removed,
      //  because the `mount` method has been forgotten before calling `unknownFunction`.
      //  Otherwise, `unknownFunction` may pass any arguments to `app.mount`, which de-optimizes this branch.
    }
  },
  // ...
}
app.mount('#app', { ssr: false })
unknownFunction(app)

#__SAFE_MANGLING__

The names of the properties of the object are meaningless. Currently, this tree-shaker doesn't have the ability to mangle object properties yet, but it will.

const colors = /* #__SAFE_MANGLING__ */ {
  red: [255, 0, 0],
  // ...
}

Is this really useful?

...

Please comment your ideas~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant