-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
ea484fe
7d8c554
7049cfa
0344474
b793205
9333972
11e6ee7
2f71f41
fc04306
5276fe0
8e689a2
0db666d
6bcdfc2
424e656
ab42326
27bb663
e7df95e
9ae5416
e72e2fc
15fc75c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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( '' ) ); |
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() ], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package-lock=false |
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. |
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> |
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 ), | ||
fullofcaffeine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; |
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll need to update the plugin for webpack 5. Instead of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, not sure I understood. Should we do this now or only when we migrate to v5? AFAIK we're still using v4.x, right? |
||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could hardcode
index.asset.php
here, but this approach required less changes, and I didn't want to change more than I needed in this context, at least not as part of this PR. Let me know what you think :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are asset files that are not
index
, e.g.,frontend.asset.php
for blocks. These are not used in this particular loop but who knows, someday they could. Being flexible sounds OK.