@wordpress/dependency-extraction-webpack-plugin
Advanced tools
Comparing version 6.8.2 to 6.8.3
@@ -169,2 +169,10 @@ /** | ||
stage: compiler.webpack.Compilation | ||
.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY, | ||
}, | ||
() => this.checkForMagicComments( compilation ) | ||
); | ||
compilation.hooks.processAssets.tap( | ||
{ | ||
name: this.constructor.name, | ||
stage: compiler.webpack.Compilation | ||
.PROCESS_ASSETS_STAGE_ANALYSE, | ||
@@ -178,2 +186,56 @@ }, | ||
/** | ||
* Check for magic comments before minification, so minification doesn't have to preserve them. | ||
* @param {webpack.Compilation} compilation | ||
*/ | ||
checkForMagicComments( compilation ) { | ||
// Accumulate all entrypoint chunks, some of them shared | ||
const entrypointChunks = new Set(); | ||
for ( const entrypoint of compilation.entrypoints.values() ) { | ||
for ( const chunk of entrypoint.chunks ) { | ||
entrypointChunks.add( chunk ); | ||
} | ||
} | ||
// Process each entrypoint chunk independently | ||
for ( const chunk of entrypointChunks ) { | ||
const chunkFiles = Array.from( chunk.files ); | ||
const jsExtensionRegExp = this.useModules ? /\.m?js$/i : /\.js$/i; | ||
const chunkJSFile = chunkFiles.find( ( f ) => | ||
jsExtensionRegExp.test( f ) | ||
); | ||
if ( ! chunkJSFile ) { | ||
// There's no JS file in this chunk, no work for us. Typically a `style.css` from cache group. | ||
continue; | ||
} | ||
// Prepare to look for magic comments, in order to decide whether | ||
// `wp-polyfill` is needed. | ||
const processContentsForMagicComments = ( content ) => { | ||
const magicComments = []; | ||
if ( content.includes( '/* wp:polyfill */' ) ) { | ||
magicComments.push( 'wp-polyfill' ); | ||
} | ||
return magicComments; | ||
}; | ||
// Go through the assets to process the sources. | ||
// This allows us to look for magic comments. | ||
chunkFiles.sort().forEach( ( filename ) => { | ||
const asset = compilation.getAsset( filename ); | ||
const content = asset.source.buffer(); | ||
const wpMagicComments = | ||
processContentsForMagicComments( content ); | ||
compilation.updateAsset( filename, ( v ) => v, { | ||
wpMagicComments, | ||
} ); | ||
} ); | ||
} | ||
} | ||
/** @param {webpack.Compilation} compilation */ | ||
@@ -291,4 +353,7 @@ addAssets( compilation ) { | ||
// `wp-polyfill` is needed. | ||
const processContentsForMagicComments = ( content ) => { | ||
if ( content.includes( '/* wp:polyfill */' ) ) { | ||
const handleMagicComments = ( info ) => { | ||
if ( ! info ) { | ||
return; | ||
} | ||
if ( info.includes( 'wp-polyfill' ) ) { | ||
chunkStaticDeps.add( 'wp-polyfill' ); | ||
@@ -305,3 +370,3 @@ } | ||
processContentsForHash( content ); | ||
processContentsForMagicComments( content ); | ||
handleMagicComments( asset.info.wpMagicComments ); | ||
} ); | ||
@@ -308,0 +373,0 @@ |
{ | ||
"name": "@wordpress/dependency-extraction-webpack-plugin", | ||
"version": "6.8.2", | ||
"version": "6.8.3", | ||
"description": "Extract WordPress script dependencies from webpack bundles.", | ||
@@ -41,3 +41,3 @@ "author": "The WordPress Contributors", | ||
}, | ||
"gitHead": "51204ac9382d0551d8fdebd3c8d4623dabfa9f3c" | ||
"gitHead": "6187079697e13c3292eb098d6338523a6676c6e8" | ||
} |
71661
617