@wordpress/dependency-extraction-webpack-plugin 6.8.2 → 6.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +68 -3
  2. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -162,6 +162,14 @@ class DependencyExtractionWebpackPlugin {
162
162
  compiler.hooks.thisCompilation.tap(
163
163
  this.constructor.name,
164
164
  ( 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
+ );
165
173
  compilation.hooks.processAssets.tap(
166
174
  {
167
175
  name: this.constructor.name,
@@ -174,6 +182,60 @@ class DependencyExtractionWebpackPlugin {
174
182
  );
175
183
  }
176
184
 
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
+
177
239
  /** @param {webpack.Compilation} compilation */
178
240
  addAssets( compilation ) {
179
241
  const {
@@ -286,8 +348,11 @@ class DependencyExtractionWebpackPlugin {
286
348
 
287
349
  // Prepare to look for magic comments, in order to decide whether
288
350
  // `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' ) ) {
291
356
  chunkStaticDeps.add( 'wp-polyfill' );
292
357
  }
293
358
  };
@@ -299,7 +364,7 @@ class DependencyExtractionWebpackPlugin {
299
364
  const content = asset.source.buffer();
300
365
 
301
366
  processContentsForHash( content );
302
- processContentsForMagicComments( content );
367
+ handleMagicComments( asset.info.wpMagicComments );
303
368
  } );
304
369
 
305
370
  // Finalise hash.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/dependency-extraction-webpack-plugin",
3
- "version": "6.8.2",
3
+ "version": "6.8.3",
4
4
  "description": "Extract WordPress script dependencies from webpack bundles.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -38,5 +38,5 @@
38
38
  "publishConfig": {
39
39
  "access": "public"
40
40
  },
41
- "gitHead": "51204ac9382d0551d8fdebd3c8d4623dabfa9f3c"
41
+ "gitHead": "6187079697e13c3292eb098d6338523a6676c6e8"
42
42
  }