Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate minified .min.js and unminified .js files for GB js entry points when building #31732

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ea484fe
Add .min.js suffix to index script depending on the presence of env v…
fullofcaffeine May 12, 2021
7d8c554
Decide what to enqueue depending on the value of the SCRIPT_DEBUG env…
fullofcaffeine May 12, 2021
7049cfa
WIP
fullofcaffeine May 20, 2021
0344474
Save unminified .js sources before minimizer runs
fullofcaffeine May 20, 2021
b793205
No need to pass the mode anymore
fullofcaffeine May 20, 2021
9333972
Add packaged version PoC and tests
fullofcaffeine May 21, 2021
11e6ee7
Package as internal monorepo npm, remove top-level PoC module
fullofcaffeine May 22, 2021
2f71f41
Merge branch 'trunk' into try/minified-and-unminified-entries-webpack…
fullofcaffeine May 22, 2021
fc04306
Add test artifacts
fullofcaffeine May 22, 2021
5276fe0
Use snake case for var, simplify and DRY the asset name
fullofcaffeine May 26, 2021
8e689a2
Hardcode index.min.js for override scripts
fullofcaffeine Jun 3, 2021
0db666d
Always refer to index.asset.php as the asset loader scripts
fullofcaffeine Jun 3, 2021
6bcdfc2
More reliable extension replacement
jsnajdr Jun 3, 2021
424e656
Fix lint error
fullofcaffeine Jun 4, 2021
ab42326
Fix php lint errors
fullofcaffeine Jun 4, 2021
27bb663
Fix another linter error (prefer-alphabetical-devDependencies)
fullofcaffeine Jun 4, 2021
e7df95e
Update packages/readable-js-assets-webpack-plugin/package.json
fullofcaffeine Jun 9, 2021
9ae5416
Update packages/readable-js-assets-webpack-plugin/README.md
fullofcaffeine Jun 9, 2021
e72e2fc
Add .nmprc and CHANGELOG.md
fullofcaffeine Jun 9, 2021
15fc75c
Restrict the compressed-size-action to *.min.js assets (and *.css)
fullofcaffeine Jun 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Save unminified .js sources before minimizer runs
  • Loading branch information
fullofcaffeine committed May 20, 2021
commit 0344474f8cc19498f42dba59bd0e32e2c81f15cb
65 changes: 24 additions & 41 deletions add-readable-js-assets-webpack-plugin.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,36 @@
/**
* External dependencies
*/
const TerserPlugin = require( 'terser-webpack-plugin' );
const fs = require( 'fs' );

//@todo Name needs to change because this also minimizes the files
class AddReadableJsAssetsWebpackPlugin {
constructor( { mode } ) {
this.terserPlugin = new TerserPlugin( {
test: /\.min\.js$/,
cache: true,
parallel: true,
sourceMap: mode !== 'production',
terserOptions: {
output: {
comments: /translators:/i,
},
compress: {
passes: 2,
},
mangle: {
reserved: [ '__', '_n', '_nx', '_x' ],
},
},
extractComments: false,
extractUnminifiedFiles( compilation ) {
const files = compilation.chunks.flatMap( ( chunk ) => chunk.files );
compilation.unminifiedAssets = files.map( ( file ) => {
const asset = compilation.assets[ file ];
const unminifiedFile = file.replace( /\.min\.js$/, '.js' );
return [ unminifiedFile, asset.source() ];
} );
}
async writeUnminifiedFiles( compilation ) {
for ( const [ file, source ] of compilation.unminifiedAssets ) {
await fs.promises.writeFile( file, source );
}
}
apply( compiler ) {
// For some reason, webpack still includes the terser minifier even after I
// removed it from the main webpack.config.js, not sure where it's
// coming from, so as a temporary hack until I find out, I forcedly remove it
compiler.options.optimization.minimizer = [];
compiler.hooks.emit.tap( this.constructor.name, ( compilation ) => {
const readableJsAssets = Object.keys( compilation.assets )
.filter( ( assetPath ) => assetPath.match( /\.min.js$/ ) )
.reduce( ( acc, assetPath ) => {
acc[ assetPath.replace( /\.min/, '' ) ] =
compilation.assets[ assetPath ];
return acc;
}, {} );

if ( compiler.options.optimization.minimize ) {
// This doesn't work :(. I guess because we're already too late
// in the hooks flow and terser doesn't have a chance to when it
// sets up its hook "taps".
this.terserPlugin.apply( compiler );
compiler.hooks.compilation.tap(
this.constructor.name,
( compilation ) => {
compilation.hooks.additionalAssets.tap(
this.constructor.name,
() => this.extractUnminifiedFiles( compilation )
);
}

compilation.assets = { ...compilation.assets, ...readableJsAssets };
} );
);
compiler.hooks.afterEmit.tapPromise(
this.constructor.name,
( compilation ) => this.writeUnminifiedFiles( compilation )
);
}
}

Expand Down
27 changes: 25 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const { BundleAnalyzerPlugin } = require( 'webpack-bundle-analyzer' );
const { DefinePlugin } = require( 'webpack' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const TerserPlugin = require( 'terser-webpack-plugin' );
const postcss = require( 'postcss' );
const { escapeRegExp, compact } = require( 'lodash' );
const { sep } = require( 'path' );
Expand All @@ -15,6 +16,11 @@ const fastGlob = require( 'fast-glob' );
const CustomTemplatedPathPlugin = require( '@wordpress/custom-templated-path-webpack-plugin' );
const LibraryExportDefaultPlugin = require( '@wordpress/library-export-default-webpack-plugin' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );

/**
* Internal dependencies
*/
const AddReadableJsAssetsWebpackPlugin = require( './add-readable-js-assets-webpack-plugin' );
const {
camelCaseDash,
} = require( '@wordpress/dependency-extraction-webpack-plugin/lib/util' );
Expand All @@ -23,7 +29,6 @@ const {
* Internal dependencies
*/
const { dependencies } = require( './package' );
const AddReadableJsAssetsWebpackPlugin = require( './add-readable-js-assets-webpack-plugin' );

const {
NODE_ENV: mode = 'development',
Expand Down Expand Up @@ -111,6 +116,25 @@ module.exports = {
// Only concatenate modules in production, when not analyzing bundles.
concatenateModules:
mode === 'production' && ! process.env.WP_BUNDLE_ANALYZER,
minimizer: [
new TerserPlugin( {
cache: true,
parallel: true,
sourceMap: mode !== 'production',
terserOptions: {
output: {
comments: /translators:/i,
},
compress: {
passes: 2,
},
mangle: {
reserved: [ '__', '_n', '_nx', '_x' ],
},
},
extractComments: false,
} ),
],
},
mode,
entry: createEntrypoints(),
Expand All @@ -120,7 +144,6 @@ module.exports = {
const { chunk } = data;
const { entryModule } = chunk;
const { rawRequest } = entryModule;

/*
* If the file being built is a Core Block's frontend file,
* we build it in the block's directory.
Expand Down