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

DEWP: Check for magic comments before minification #65582

Conversation

anomiex
Copy link
Contributor

@anomiex anomiex commented Sep 23, 2024

What?

Look for magic wp:polyfill comments before minification.

Why?

Avoids having to configure Terser to preserve these comments, and also avoids having them in the minified JS.

See https://github.com/WordPress/gutenberg/pull/65292/files#r1767653086

How?

Minifiers run in the processAssets hook at the
PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE stage. If we hook at the PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY stage to check for the magic comments, we don't have to worry about configuring Terser to preserve them (and won't have to have them making the output slightly larger either).

Testing Instructions

Build everything, then compare the result with the results of a build from the equivalent trunk version. Changes should be limited to removing unnecessary /* wp:polyfill */ comments in .min.js output files. wp-polyfills shoud not be being removed from any .asset.php files.

When I did this myself, I found that a few of the outputs (build/editor/index.min.asset.php, build/edit-site/index.min.asset.php, build/router/index.min.asset.php) now have wp-polyfills added in .assets.php where they didn't have it previously. I think the comments were getting dropped when Terser converts code like this

const foo = 1;
;// CONCATENATGED MODULE: ./packages/whatever/index.js
/* wp:polyfill */
const bar = 2;

to be like this

const foo = 1, bar = 2;

(compare https://www.npmjs.com/package/@automattic/i18n-check-webpack-plugin#user-content-lost-comments-due-to-expression-movement where something similar is discussed in relation to translator comments)

Testing Instructions for Keyboard

N/A

Minifiers run in the `processAssets` hook at the
`PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE` stage. If we hook at the
`PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY` stage to check for
the magic comments, we don't have to worry about configuring Terser to
preserve them (and won't have to have them making the output slightly
larger either).
Copy link

github-actions bot commented Sep 23, 2024

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: anomiex <[email protected]>
Co-authored-by: gziolo <[email protected]>
Co-authored-by: sirreal <[email protected]>
Co-authored-by: sgomes <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@sgomes
Copy link
Contributor

sgomes commented Sep 24, 2024

When I did this myself, I found that a few of the outputs (build/editor/index.min.asset.php, build/edit-site/index.min.asset.php, build/router/index.min.asset.php) now have wp-polyfills added in .assets.php where they didn't have it previously.

Good catch! 👍 It looks like these modules were already getting polyfills via transitive dependencies, but this would not necessarily always be the case going forward, so this should help us avoid future bugs.


module.exports = {
optimization: {
minimize: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was contemplating why the version didn't change for existing test cases, where the wp-polyfill was listed. However, I figured out we didn't use modification before.

@gziolo gziolo added [Tool] Dependency Extraction Webpack Plugin /packages/dependency-extraction-webpack-plugin [Type] Enhancement A suggestion for improvement. labels Sep 24, 2024
@sirreal
Copy link
Member

sirreal commented Sep 24, 2024

This seems great, will you add a CHANGELOG entry to the package? I think this can be an "enhancement."

@anomiex
Copy link
Contributor Author

anomiex commented Sep 24, 2024

This seems great, will you add a CHANGELOG entry to the package? I think this can be an "enhancement."

Added!

@sirreal
Copy link
Member

sirreal commented Sep 24, 2024

One thought about this, it's now hooked into another phase and doing more processing, what do you think of making it configurable now?

@anomiex
Copy link
Contributor Author

anomiex commented Sep 24, 2024

Personally I think it's probably fine. But it's up to all of you.

Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, thanks @anomiex!

@sirreal sirreal added the Backport to Gutenberg RC Pull request that needs to be backported to a Gutenberg release candidate (RC) label Sep 25, 2024
@sirreal
Copy link
Member

sirreal commented Sep 25, 2024

With #65292 in the latest package releases and considering #65582 (comment), I think this should be merged to the release branch so it's released soon. It's not a package that ships with Core but it should be the latest released version. I'm not totally sure how the flows work in this case so I've added backport labels.

@sirreal sirreal added the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Sep 25, 2024
@sirreal sirreal merged commit e33271c into WordPress:trunk Sep 25, 2024
66 checks passed
@github-actions github-actions bot added this to the Gutenberg 19.4 milestone Sep 25, 2024
@github-actions github-actions bot removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Sep 25, 2024
gutenbergplugin pushed a commit that referenced this pull request Sep 25, 2024
Look for magic wp:polyfill comments before minification.

Avoids having to configure Terser to preserve these comments, and also avoids having them in the minified JS.

Minifiers run in the processAssets hook at the
`PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE` stage. If we hook at the `PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY` stage to check for the magic comments, we don't have to worry about configuring Terser to preserve them (and won't have to have them making the output slightly larger either).

---------

Co-authored-by: anomiex <[email protected]>
Co-authored-by: gziolo <[email protected]>
Co-authored-by: sirreal <[email protected]>
Co-authored-by: sgomes <[email protected]>
Copy link

I just cherry-picked this PR to the wp/6.7 branch to get it included in the next release: 2f96654

@github-actions github-actions bot added the Backported to WP Core Pull request that has been successfully merged into WP Core label Sep 25, 2024
@anomiex anomiex deleted the update/check-magic-polyfill-comment-before-minification branch September 30, 2024 20:28
cbravobernal pushed a commit that referenced this pull request Oct 7, 2024
Look for magic wp:polyfill comments before minification.

Avoids having to configure Terser to preserve these comments, and also avoids having them in the minified JS.

Minifiers run in the processAssets hook at the
`PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE` stage. If we hook at the `PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY` stage to check for the magic comments, we don't have to worry about configuring Terser to preserve them (and won't have to have them making the output slightly larger either).

---------

Co-authored-by: anomiex <[email protected]>
Co-authored-by: gziolo <[email protected]>
Co-authored-by: sirreal <[email protected]>
Co-authored-by: sgomes <[email protected]>
@cbravobernal
Copy link
Contributor

Cherry picked for Gutenberg 19.4 RC in 6cc91c7

@cbravobernal cbravobernal removed the Backport to Gutenberg RC Pull request that needs to be backported to a Gutenberg release candidate (RC) label Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported to WP Core Pull request that has been successfully merged into WP Core [Tool] Dependency Extraction Webpack Plugin /packages/dependency-extraction-webpack-plugin [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants