-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate minified .min.js and unminified .js files for GB js entry po…
…ints when building (#31732) * Add .min.js suffix to index script depending on the presence of env var and enqueue .min.js files by default in `client-assets.php` * Decide what to enqueue depending on the value of the SCRIPT_DEBUG env var - If set and true, we enqueue the unminified index.js file for the given package; - If not set or set and false, we enqueue the minified (production) index.min.js file. * WIP * Save unminified .js sources before minimizer runs * No need to pass the mode anymore * Add packaged version PoC and tests * Package as internal monorepo npm, remove top-level PoC module * Add test artifacts * Use snake case for var, simplify and DRY the asset name * Hardcode index.min.js for override scripts * Always refer to index.asset.php as the asset loader scripts The WebPack build generates them using the `index.asset.php` name format. * More reliable extension replacement * Fix lint error * Fix php lint errors * Fix another linter error (prefer-alphabetical-devDependencies) * Update packages/readable-js-assets-webpack-plugin/package.json Change the plugin version to include a '-prerelease' suffix. Co-authored-by: Greg Ziółkowski <[email protected]> * Update packages/readable-js-assets-webpack-plugin/README.md Fix webpack spelling :) Co-authored-by: Greg Ziółkowski <[email protected]> * Add .nmprc and CHANGELOG.md * Restrict the compressed-size-action to *.min.js assets (and *.css) Co-authored-by: Jarda Snajdr <[email protected]> Co-authored-by: Greg Ziółkowski <[email protected]>
- Loading branch information
1 parent
c4a9a01
commit ec50dad
Showing
20 changed files
with
501 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/dependency-extraction-webpack-plugin/test/fixtures/has-extension-suffix/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { isEmpty } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { isBlobURL } from '@wordpress/blob'; | ||
|
||
isEmpty( isBlobURL( '' ) ); |
11 changes: 11 additions & 0 deletions
11
...dependency-extraction-webpack-plugin/test/fixtures/has-extension-suffix/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
const DependencyExtractionWebpackPlugin = require( '../../..' ); | ||
|
||
module.exports = { | ||
output: { | ||
filename: 'index.min.js', | ||
}, | ||
plugins: [ new DependencyExtractionWebpackPlugin() ], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<!-- Learn how to maintain this file at https://github.com/WordPress/gutenberg/tree/HEAD/packages#maintaining-changelogs. --> | ||
|
||
## Unreleased | ||
|
||
Initial release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Readable JS assets WebPack Plugin | ||
|
||
Generate a readable non-minified JS file for each `.min.js` asset. | ||
|
||
The end result is that for each JS entrypoint, we get a set of readable and non-minimized `.js` file and a minimized `.min.js`. This allows Gutenberg to follow the WordPress convention of adding a `.min.js` suffix to minimized JS files, while still providing a readable and unminized files that play well with the WordPress i18n machinery. | ||
|
||
Consult the [webpack website](https://webpack.js.org) for additional information on webpack concepts. | ||
|
||
## Installation | ||
|
||
Install the module | ||
|
||
```bash | ||
npm install @wordpress/readable-js-assets-webpack-plugin --save-dev | ||
``` | ||
|
||
**Note**: This package requires Node.js 12.0.0 or later. It also requires webpack 4.8.3 and newer. It is not compatible with older versions. | ||
|
||
## Usage | ||
|
||
### Webpack | ||
|
||
Use this plugin as you would other webpack plugins: | ||
|
||
```js | ||
// webpack.config.js | ||
const ReadableJsAssetsWebpackPlugin = require( '@wordpress/readable-js-assets-webpack-plugin' ); | ||
|
||
module.exports = { | ||
// …snip | ||
plugins: [ new ReadableJsAssetsWebpackPlugin() ], | ||
}; | ||
``` | ||
|
||
**Note:** | ||
- Multiple instances of the plugin are not supported and may produced unexpected results; | ||
- It assumes your webpack pipeline is already generating a `.min.js` JS asset file for each JS entry-point. | ||
|
||
<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const fs = require( 'fs' ); | ||
const path = require( 'path' ); | ||
|
||
class AddReadableJsAssetsWebpackPlugin { | ||
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( | ||
path.join( compilation.options.output.path, file ), | ||
source | ||
); | ||
} | ||
} | ||
apply( compiler ) { | ||
compiler.hooks.compilation.tap( | ||
this.constructor.name, | ||
( compilation ) => { | ||
compilation.hooks.additionalAssets.tap( | ||
this.constructor.name, | ||
() => this.extractUnminifiedFiles( compilation ) | ||
); | ||
} | ||
); | ||
compiler.hooks.afterEmit.tapPromise( | ||
this.constructor.name, | ||
( compilation ) => this.writeUnminifiedFiles( compilation ) | ||
); | ||
} | ||
} | ||
|
||
module.exports = AddReadableJsAssetsWebpackPlugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "@wordpress/readable-js-assets-webpack-plugin", | ||
"version": "1.0.0-prerelease", | ||
"description": "Generate a readable JS file for each JS asset.", | ||
"author": "The WordPress Contributors", | ||
"license": "GPL-2.0-or-later", | ||
"keywords": [ | ||
"wordpress", | ||
"gutenberg", | ||
"webpack", | ||
"readable", | ||
"minimizer" | ||
], | ||
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/packages/readable-js-assets-webpack-plugin/README.md", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/WordPress/gutenberg.git", | ||
"directory": "packages/readable-js-assets-webpack-plugin" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/WordPress/gutenberg/issues" | ||
}, | ||
"engines": { | ||
"node": ">=12.0" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"main": "index.js", | ||
"peerDependencies": { | ||
"webpack": "^4.8.3 || ^5.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
Oops, something went wrong.