Skip to content

Proposal: more annotations #32

Description

@kermanx

#__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~

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions