Skip to content

Commit e33271c

Browse files
anomiexanomiexgziolosirrealsgomes
authored
DEWP: Check for magic comments before minification (#65582)
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 <bjorsch@git.wordpress.org> Co-authored-by: gziolo <gziolo@git.wordpress.org> Co-authored-by: sirreal <jonsurrell@git.wordpress.org> Co-authored-by: sgomes <sergiomdgomes@git.wordpress.org>
1 parent b08b18b commit e33271c

File tree

6 files changed

+101
-4
lines changed

6 files changed

+101
-4
lines changed

packages/dependency-extraction-webpack-plugin/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Enhancements
6+
7+
- Detection of magic comments is now done before minification ([#65582](https://github.com/WordPress/gutenberg/pull/65582)).
8+
59
### Bug Fixes
610

711
- Fix a bug where cycles in dependent modules could enter infinite recursion ([#65291](https://github.com/WordPress/gutenberg/pull/65291)).

packages/dependency-extraction-webpack-plugin/lib/index.js

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ class DependencyExtractionWebpackPlugin {
162162
compiler.hooks.thisCompilation.tap(
163163
this.constructor.name,
164164
( compilation ) => {
165+
compilation.hooks.processAssets.tap(
166+
{
167+
name: this.constructor.name,
168+
stage: compiler.webpack.Compilation
169+
.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY,
170+
},
171+
() => this.checkForMagicComments( compilation )
172+
);
165173
compilation.hooks.processAssets.tap(
166174
{
167175
name: this.constructor.name,
@@ -174,6 +182,60 @@ class DependencyExtractionWebpackPlugin {
174182
);
175183
}
176184

185+
/**
186+
* Check for magic comments before minification, so minification doesn't have to preserve them.
187+
* @param {webpack.Compilation} compilation
188+
*/
189+
checkForMagicComments( compilation ) {
190+
// Accumulate all entrypoint chunks, some of them shared
191+
const entrypointChunks = new Set();
192+
for ( const entrypoint of compilation.entrypoints.values() ) {
193+
for ( const chunk of entrypoint.chunks ) {
194+
entrypointChunks.add( chunk );
195+
}
196+
}
197+
198+
// Process each entrypoint chunk independently
199+
for ( const chunk of entrypointChunks ) {
200+
const chunkFiles = Array.from( chunk.files );
201+
202+
const jsExtensionRegExp = this.useModules ? /\.m?js$/i : /\.js$/i;
203+
204+
const chunkJSFile = chunkFiles.find( ( f ) =>
205+
jsExtensionRegExp.test( f )
206+
);
207+
if ( ! chunkJSFile ) {
208+
// There's no JS file in this chunk, no work for us. Typically a `style.css` from cache group.
209+
continue;
210+
}
211+
212+
// Prepare to look for magic comments, in order to decide whether
213+
// `wp-polyfill` is needed.
214+
const processContentsForMagicComments = ( content ) => {
215+
const magicComments = [];
216+
217+
if ( content.includes( '/* wp:polyfill */' ) ) {
218+
magicComments.push( 'wp-polyfill' );
219+
}
220+
221+
return magicComments;
222+
};
223+
224+
// Go through the assets to process the sources.
225+
// This allows us to look for magic comments.
226+
chunkFiles.sort().forEach( ( filename ) => {
227+
const asset = compilation.getAsset( filename );
228+
const content = asset.source.buffer();
229+
230+
const wpMagicComments =
231+
processContentsForMagicComments( content );
232+
compilation.updateAsset( filename, ( v ) => v, {
233+
wpMagicComments,
234+
} );
235+
} );
236+
}
237+
}
238+
177239
/** @param {webpack.Compilation} compilation */
178240
addAssets( compilation ) {
179241
const {
@@ -286,8 +348,11 @@ class DependencyExtractionWebpackPlugin {
286348

287349
// Prepare to look for magic comments, in order to decide whether
288350
// `wp-polyfill` is needed.
289-
const processContentsForMagicComments = ( content ) => {
290-
if ( content.includes( '/* wp:polyfill */' ) ) {
351+
const handleMagicComments = ( info ) => {
352+
if ( ! info ) {
353+
return;
354+
}
355+
if ( info.includes( 'wp-polyfill' ) ) {
291356
chunkStaticDeps.add( 'wp-polyfill' );
292357
}
293358
};
@@ -299,7 +364,7 @@ class DependencyExtractionWebpackPlugin {
299364
const content = asset.source.buffer();
300365

301366
processContentsForHash( content );
302-
processContentsForMagicComments( content );
367+
handleMagicComments( asset.info.wpMagicComments );
303368
} );
304369

305370
// Finalise hash.

packages/dependency-extraction-webpack-plugin/test/__snapshots__/build.js.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ exports[`DependencyExtractionWebpackPlugin modules Webpack \`polyfill-magic-comm
257257

258258
exports[`DependencyExtractionWebpackPlugin modules Webpack \`polyfill-magic-comment\` should produce expected output: External modules should match snapshot 1`] = `[]`;
259259

260+
exports[`DependencyExtractionWebpackPlugin modules Webpack \`polyfill-magic-comment-minified\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = `
261+
"<?php return array('dependencies' => array('wp-polyfill'), 'version' => '31d6cfe0d16ae931b73c', 'type' => 'module');
262+
"
263+
`;
264+
265+
exports[`DependencyExtractionWebpackPlugin modules Webpack \`polyfill-magic-comment-minified\` should produce expected output: External modules should match snapshot 1`] = `[]`;
266+
260267
exports[`DependencyExtractionWebpackPlugin modules Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'a.asset.php' should match snapshot 1`] = `
261268
"<?php return array('dependencies' => array('@wordpress/blob'), 'version' => 'a1906cfc819b623c86f8', 'type' => 'module');
262269
"
@@ -666,6 +673,13 @@ exports[`DependencyExtractionWebpackPlugin scripts Webpack \`polyfill-magic-comm
666673

667674
exports[`DependencyExtractionWebpackPlugin scripts Webpack \`polyfill-magic-comment\` should produce expected output: External modules should match snapshot 1`] = `[]`;
668675

676+
exports[`DependencyExtractionWebpackPlugin scripts Webpack \`polyfill-magic-comment-minified\` should produce expected output: Asset file 'main.asset.php' should match snapshot 1`] = `
677+
"<?php return array('dependencies' => array('wp-polyfill'), 'version' => '31d6cfe0d16ae931b73c');
678+
"
679+
`;
680+
681+
exports[`DependencyExtractionWebpackPlugin scripts Webpack \`polyfill-magic-comment-minified\` should produce expected output: External modules should match snapshot 1`] = `[]`;
682+
669683
exports[`DependencyExtractionWebpackPlugin scripts Webpack \`runtime-chunk-single\` should produce expected output: Asset file 'a.asset.php' should match snapshot 1`] = `
670684
"<?php return array('dependencies' => array('wp-blob'), 'version' => 'd3cda564b538b44d38ef');
671685
"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/* wp:polyfill */
2+
3+
// Nothing else, really.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
const DependencyExtractionWebpackPlugin = require( '../../..' );
5+
6+
module.exports = {
7+
optimization: {
8+
minimize: true,
9+
},
10+
plugins: [ new DependencyExtractionWebpackPlugin() ],
11+
};

tools/webpack/shared.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const baseConfig = {
2525
parallel: true,
2626
terserOptions: {
2727
output: {
28-
comments: /(translators:|wp:polyfill)/i,
28+
comments: /translators:/i,
2929
},
3030
compress: {
3131
passes: 2,

0 commit comments

Comments
 (0)