-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Keycodes: improve tree shaking by annotating exports as pure #67615
Conversation
modifiers, | ||
( /** @type {WPModifier} */ modifier ) => { | ||
export const rawShortcut = | ||
/* @__PURE__ */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this something supported by all bundlers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's supported by all, but generally, most would support it, as it's declared in the spec:
https://github.com/javascript-compiler-hints/compiler-notations-spec/blob/main/pure-notation-spec.md
also see compatibility table:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The webpack + Terser combo also supports it. Webpack adds the comment to code that is pure (typically re-exports) and then Terser uses the comment to eliminate the dead code.
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: 0 B Total Size: 1.83 MB ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should do the job, but I wonder if we can instruct webpack and other bundlers to always consider mapValues()
as a pure function call. That way, we won't have to manually do it everywhere.
I never heard about such a feature or plugin. Also, it's not enough that |
That said, I believe another approach to work around the issue would be to move each of these exports into its own module, and then re-export them all from this one. Since we already have the |
Yes, that should also solve the problem, but it's a lot of work, there are many modules and they are tiny and interdependent. We could connect that with a TypeScript rewrite. |
Co-authored-by: jsnajdr <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: tyxla <[email protected]> Co-authored-by: sgomes <[email protected]>
The
keycodes
package has a few exports whose values are result of a function call:The problem with this is that bundlers like esbuild cannot tree shake this code when the
rawShortcut
export is unused. Because the bundler doesn't know ifmapValues
has any side effects.The esbuild docs recommend using a
/* @__PURE__ */
comment:They also recommend using an IIFE but in our case the expression is already a
mapValues
function call, so we don't need to wrap it.The packages has the
sideEffects: false
flag inpackage.json
but it doesn't help here.How to test:
This is very visible in the
packages/dataviews/build-wp/index.js
bundle. The only thing that the bundle uses fromkeycodes
is theESCAPE
constant. But the bundle contains also all the code with possible side effects.After this PR, the definition of
ESCAPE
is all that remains fromkeycodes
.It would be nice to also test this with webpack, but I didn't do that yet.