Commit f3f09735e68 for woocommerce
commit f3f09735e680e86210f8367dc4bb01f063e8a7ae
Author: Ján Mikláš <neosinner@gmail.com>
Date: Tue Nov 17 20:06:49 2026 +0100
Support in-place extension bundle minification (#71071)
* Support in-place bundle minification
* Add the webpack tooling changelog
diff --git a/packages/js/extension-tooling/README.md b/packages/js/extension-tooling/README.md
index 3f91f4c8427..315af5c7853 100644
--- a/packages/js/extension-tooling/README.md
+++ b/packages/js/extension-tooling/README.md
@@ -57,7 +57,7 @@ const minifyBundleCopies = require( '@woocommerce/extension-tooling/webpack-dist
```
- `includeScripts( [ [ source, destination ] ] )` expands classic scripts that use `include( 'relative/file.js' );`. It emits the regular asset and a `.min.js` copy for the caller's webpack minimizer. Limit the minimizer to `.min.js` files, for example `new TerserPlugin( { test: /\.min\.js$/ } )`, to keep the regular asset readable. Each `include()` must be on its own line; the build fails on any other `include()` line.
-- `minifyBundleCopies( [ 'frontend/*.js' ] )` emits minified copies of matching bundles after dependency metadata is generated. It skips a bundle that already has a `.min.js` asset, such as an `includeScripts` destination.
+- `minifyBundleCopies( [ 'admin/*.js' ], [ 'frontend/**/*.js' ] )` emits minified copies of bundles matching the first list after dependency metadata is generated. Match only JavaScript assets in either list. The optional second list minifies matching assets in place, including `.min.js` assets, and takes precedence when both lists match. Copy mode skips a bundle that already has a `.min.js` asset, such as an `includeScripts` destination.
## Translation checks
diff --git a/packages/js/extension-tooling/changelog/wooprd-4110-support-in-place-bundle-minification b/packages/js/extension-tooling/changelog/wooprd-4110-support-in-place-bundle-minification
new file mode 100644
index 00000000000..79cc7e8211d
--- /dev/null
+++ b/packages/js/extension-tooling/changelog/wooprd-4110-support-in-place-bundle-minification
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Support minifying webpack bundles in place.
diff --git a/packages/js/extension-tooling/src/webpack-dist-minify.js b/packages/js/extension-tooling/src/webpack-dist-minify.js
index c2696f8874b..efc6d7c8be6 100644
--- a/packages/js/extension-tooling/src/webpack-dist-minify.js
+++ b/packages/js/extension-tooling/src/webpack-dist-minify.js
@@ -1,7 +1,7 @@
const { posix } = require( 'node:path' );
const uglify = require( 'uglify-js' );
-module.exports = function minifyBundleCopies( copies ) {
+module.exports = function minifyBundleCopies( copies, inPlace = [] ) {
const name = 'MinifyBundleCopies';
return {
@@ -14,20 +14,25 @@ module.exports = function minifyBundleCopies( copies ) {
{ name, stage: Compilation.PROCESS_ASSETS_STAGE_REPORT },
() => {
for ( const asset of compilation.getAssets() ) {
- if (
- asset.name.endsWith( '.min.js' ) ||
- ! copies.some( ( pattern ) =>
+ const replace = inPlace.some( ( pattern ) =>
+ posix.matchesGlob( asset.name, pattern )
+ );
+ const copy =
+ ! asset.name.endsWith( '.min.js' ) &&
+ copies.some( ( pattern ) =>
posix.matchesGlob( asset.name, pattern )
- )
- ) {
+ );
+ if ( ! replace && ! copy ) {
continue;
}
- const destination = asset.name.replace(
- /\.js$/,
- '.min.js'
- );
+ const destination = replace
+ ? asset.name
+ : asset.name.replace( /\.js$/, '.min.js' );
// includeScripts emits its own .min.js copy; a second one would conflict.
- if ( compilation.getAsset( destination ) ) {
+ if (
+ ! replace &&
+ compilation.getAsset( destination )
+ ) {
continue;
}
const result = uglify.minify(
@@ -44,13 +49,14 @@ module.exports = function minifyBundleCopies( copies ) {
);
continue;
}
- compilation.emitAsset(
- destination,
- new sources.RawSource( result.code ),
- {
+ const output = new sources.RawSource( result.code );
+ if ( replace ) {
+ compilation.updateAsset( destination, output );
+ } else {
+ compilation.emitAsset( destination, output, {
minimized: true,
- }
- );
+ } );
+ }
}
}
);
diff --git a/packages/js/extension-tooling/test/webpack.test.js b/packages/js/extension-tooling/test/webpack.test.js
index 4ff10e0de36..dfeaea967ee 100644
--- a/packages/js/extension-tooling/test/webpack.test.js
+++ b/packages/js/extension-tooling/test/webpack.test.js
@@ -65,14 +65,14 @@ test( 'Expands classic includes and emits a minified bundle copy', async ( conte
assert.match( minified, /Hello world/ );
} );
-async function buildIncludes( context, classic, plugins = [] ) {
+async function buildIncludes( context, classic, plugins = [], entry = '' ) {
const directory = await fs.mkdtemp(
path.join( os.tmpdir(), 'woo-extension-webpack-' )
);
context.after( () => fs.rm( directory, { recursive: true, force: true } ) );
await fs.writeFile( path.join( directory, 'classic.js' ), classic );
await fs.writeFile( path.join( directory, 'part.js' ), 'const part = 1;' );
- await fs.writeFile( path.join( directory, 'entry.js' ), '' );
+ await fs.writeFile( path.join( directory, 'entry.js' ), entry );
const stats = await new Promise( ( resolve, reject ) => {
webpack(
@@ -120,6 +120,54 @@ test( 'Leaves the include copy when a bundle pattern also matches it', async ( c
assert.ok( ( await fs.readdir( directory ) ).includes( 'bundle.min.js' ) );
} );
+test( 'Minifies matching bundles in place even if they also match a copy pattern', async ( context ) => {
+ const classic = "include( 'part.js' );\nconsole.log( 'Included code' );\n";
+ const entry = "console.log( 'Hello world' );\n";
+ const original = await buildIncludes( context, classic, [], entry );
+ const { directory, stats } = await buildIncludes(
+ context,
+ classic,
+ [
+ minifyBundleCopies(
+ [ 'bundle.js' ],
+ [ 'bundle.js', 'assembled.min.js' ]
+ ),
+ ],
+ entry
+ );
+
+ assert.equal(
+ original.stats.hasErrors(),
+ false,
+ original.stats.toString( 'errors-only' )
+ );
+ assert.equal( stats.hasErrors(), false, stats.toString( 'errors-only' ) );
+ const bundle = await fs.readFile(
+ path.join( directory, 'bundle.js' ),
+ 'utf8'
+ );
+ const unminified = await fs.readFile(
+ path.join( original.directory, 'bundle.js' ),
+ 'utf8'
+ );
+ assert.ok( bundle.length < unminified.length );
+ assert.match( bundle, /Hello world/ );
+ assert.ok(
+ ! ( await fs.readdir( directory ) ).includes( 'bundle.min.js' )
+ );
+
+ const included = await fs.readFile(
+ path.join( directory, 'assembled.min.js' ),
+ 'utf8'
+ );
+ const unminifiedInclude = await fs.readFile(
+ path.join( original.directory, 'assembled.min.js' ),
+ 'utf8'
+ );
+ assert.ok( included.length < unminifiedInclude.length );
+ assert.match( included, /Included code/ );
+} );
+
test( 'Fails the build on an include line it cannot expand', async ( context ) => {
const { stats } = await buildIncludes(
context,