Skip to content
Merged
Prev Previous commit
Next Next commit
Switch to using magic comments
  • Loading branch information
sgomes committed Sep 18, 2024
commit 9d549933e320e9f70d8647a417b11ad895b2151c
33 changes: 17 additions & 16 deletions packages/babel-preset-default/replace-polyfills.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@ function replacePolyfills() {
this.hasAddedPolyfills = false;
},
visitor: {
Program: {
exit( path ) {
if ( this.hasAddedPolyfills ) {
// Add magic comment to top of file.
path.addComment(
'leading',
' wordpress: needs wp-polyfill '
);
}
},
},
// Handle `import` syntax.
ImportDeclaration( path ) {
const source = path?.node?.source;
const name = source?.value || '';

// Look for imports from `core-js`.
if ( name.startsWith( 'core-js/' ) ) {
if ( this.hasAddedPolyfills ) {
// Remove duplicate import.
path.remove();
} else {
// Replace with `@wordpress/polyfill`.
source.value = '@wordpress/polyfill';
this.hasAddedPolyfills = true;
}
// Remove import.
path.remove();
this.hasAddedPolyfills = true;
}
},

Expand All @@ -43,14 +49,9 @@ function replacePolyfills() {
arg.type === 'StringLiteral' &&
arg.value.startsWith( 'core-js/' )
) {
if ( this.hasAddedPolyfills ) {
// Remove duplicate import.
path.remove();
} else {
// Replace with `@wordpress/polyfill`.
arg.value = '@wordpress/polyfill';
this.hasAddedPolyfills = true;
}
// Remove import.
path.remove();
this.hasAddedPolyfills = true;
}
},
},
Expand Down
38 changes: 31 additions & 7 deletions packages/dependency-extraction-webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class DependencyExtractionWebpackPlugin {
}
}

// Go through the assets and hash the sources. We can't just use
// Prepare to hash the sources. We can't just use
// `chunk.contentHash` because that's not updated when
// assets are minified. In practice the hash is updated by
// `RealContentHashPlugin` after minification, but it only modifies
Expand All @@ -288,12 +288,36 @@ class DependencyExtractionWebpackPlugin {
const { hashFunction, hashDigest, hashDigestLength } =
compilation.outputOptions;

const contentHash = chunkFiles
.sort()
.reduce( ( hash, filename ) => {
const asset = compilation.getAsset( filename );
return hash.update( asset.source.buffer() );
}, createHash( hashFunction ) )
const hashBuilder = createHash( hashFunction );

const processContentsForHash = ( content ) => {
hashBuilder.update( content );
};

// Prepare to look for magic comments, in order to decide whether
// `wp-polyfill` is needed.
const processContentsForMagicComments = ( content ) => {
if (
content
.toString()
.includes( '/* wordpress: needs wp-polyfill */' )
) {
chunkStaticDeps.add( 'wp-polyfill' );
}
};

// Go through the assets to process the sources.
// This allows us to generate hashes, as well as look for magic comments.
chunkFiles.sort().forEach( ( filename ) => {
const asset = compilation.getAsset( filename );
const content = asset.source.buffer();

processContentsForHash( content );
processContentsForMagicComments( content );
} );

// Finalise hash.
const contentHash = hashBuilder
.digest( hashDigest )
.slice( 0, hashDigestLength );

Expand Down
1 change: 0 additions & 1 deletion packages/dependency-extraction-webpack-plugin/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ function defaultRequestToExternalModule( request ) {
switch ( request ) {
case '@wordpress/interactivity-router':
case '@wordpress/a11y':
case '@wordpress/polyfill':
return `import ${ request }`;
}

Expand Down
2 changes: 1 addition & 1 deletion tools/webpack/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const baseConfig = {
parallel: true,
terserOptions: {
output: {
comments: /translators:/i,
comments: /(translators|wordpress):/i,
},
compress: {
passes: 2,
Expand Down