@wordpress/dependency-extraction-webpack-plugin 6.8.1 → 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 +109 -8
  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.
@@ -369,6 +434,9 @@ class DependencyExtractionWebpackPlugin {
369
434
  }
370
435
  }
371
436
 
437
+ static #staticDepsCurrent = new WeakSet();
438
+ static #staticDepsCache = new WeakMap();
439
+
372
440
  /**
373
441
  * Can we trace a line of static dependencies from an entry to a module
374
442
  *
@@ -378,6 +446,20 @@ class DependencyExtractionWebpackPlugin {
378
446
  * @return {boolean} True if there is a static import path to the root
379
447
  */
380
448
  static hasStaticDependencyPathToRoot( compilation, block ) {
449
+ if ( DependencyExtractionWebpackPlugin.#staticDepsCache.has( block ) ) {
450
+ return DependencyExtractionWebpackPlugin.#staticDepsCache.get(
451
+ block
452
+ );
453
+ }
454
+
455
+ if (
456
+ DependencyExtractionWebpackPlugin.#staticDepsCurrent.has( block )
457
+ ) {
458
+ return false;
459
+ }
460
+
461
+ DependencyExtractionWebpackPlugin.#staticDepsCurrent.add( block );
462
+
381
463
  const incomingConnections = [
382
464
  ...compilation.moduleGraph.getIncomingConnections( block ),
383
465
  ].filter(
@@ -391,6 +473,13 @@ class DependencyExtractionWebpackPlugin {
391
473
  // If we don't have non-entry, non-library incoming connections,
392
474
  // we've reached a root of
393
475
  if ( ! incomingConnections.length ) {
476
+ DependencyExtractionWebpackPlugin.#staticDepsCache.set(
477
+ block,
478
+ true
479
+ );
480
+ DependencyExtractionWebpackPlugin.#staticDepsCurrent.delete(
481
+ block
482
+ );
394
483
  return true;
395
484
  }
396
485
 
@@ -409,16 +498,28 @@ class DependencyExtractionWebpackPlugin {
409
498
 
410
499
  // All the dependencies were Async, the module was reached via a dynamic import
411
500
  if ( ! staticDependentModules.length ) {
501
+ DependencyExtractionWebpackPlugin.#staticDepsCache.set(
502
+ block,
503
+ false
504
+ );
505
+ DependencyExtractionWebpackPlugin.#staticDepsCurrent.delete(
506
+ block
507
+ );
412
508
  return false;
413
509
  }
414
510
 
415
511
  // Continue to explore any static dependencies
416
- return staticDependentModules.some( ( parentStaticDependentModule ) =>
417
- DependencyExtractionWebpackPlugin.hasStaticDependencyPathToRoot(
418
- compilation,
419
- parentStaticDependentModule
420
- )
512
+ const result = staticDependentModules.some(
513
+ ( parentStaticDependentModule ) =>
514
+ DependencyExtractionWebpackPlugin.hasStaticDependencyPathToRoot(
515
+ compilation,
516
+ parentStaticDependentModule
517
+ )
421
518
  );
519
+
520
+ DependencyExtractionWebpackPlugin.#staticDepsCache.set( block, result );
521
+ DependencyExtractionWebpackPlugin.#staticDepsCurrent.delete( block );
522
+ return result;
422
523
  }
423
524
  }
424
525
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/dependency-extraction-webpack-plugin",
3
- "version": "6.8.1",
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": "cf707c1f25a2716e310cc8f9afcc8554405c79ac"
41
+ "gitHead": "6187079697e13c3292eb098d6338523a6676c6e8"
42
42
  }