dist
5 years ago
docker
5 years ago
images
5 years ago
includes
5 years ago
languages
5 years ago
src
5 years ago
storybook
5 years ago
typings
5 years ago
vendor
5 years ago
chunk-src-version-param.js
5 years ago
license.txt
6 years ago
lint-staged.config.js
5 years ago
readme.txt
5 years ago
uninstall.php
5 years ago
unminify.js
6 years ago
woocommerce-admin.php
5 years ago
unminify.js
82 lines
| 1 | /** |
| 2 | * Repurposed UnminifiedWebpackPlugin. |
| 3 | * |
| 4 | * See: https://github.com/leftstick/unminified-webpack-plugin/blob/master/index.js |
| 5 | * |
| 6 | * Changes: |
| 7 | * 1. Remove check for UglifyJsPlugin - Terser is the successor. |
| 8 | * 2. Remove check for development mode - we always want unminified files. |
| 9 | * 3. Remove BannerPlugin support - we don't use it. |
| 10 | * 4. Remove the 'min' suffix from the chunk loaded in the new `mainEntry` option. |
| 11 | * 5. Hook into compilation later so we're running after Source Map generation. |
| 12 | */ |
| 13 | const path = require( 'path' ); |
| 14 | const ModuleFilenameHelpers = require( 'webpack/lib/ModuleFilenameHelpers' ); |
| 15 | |
| 16 | const getFileName = ( name, ext, opts ) => { |
| 17 | if ( name.match(/([-_.]min)[-_.]/ ) ) { |
| 18 | return name.replace( /[-_.]min/, '' ); |
| 19 | } |
| 20 | |
| 21 | const suffix = ( opts.postfix || 'nomin' ) + '.' + ext; |
| 22 | if ( name.match( new RegExp( '\.' + ext + '$' ) ) ) { |
| 23 | return name.replace( new RegExp( ext + '$' ), suffix ) |
| 24 | } |
| 25 | |
| 26 | return name + suffix; |
| 27 | }; |
| 28 | |
| 29 | class UnminifyWebpackPlugin { |
| 30 | constructor( opts ) { |
| 31 | const options = opts || {}; |
| 32 | |
| 33 | this.options = { |
| 34 | test: options.test || /\.(js|css)($|\?)/i, |
| 35 | mainEntry: options.mainEntry || false, |
| 36 | }; |
| 37 | } |
| 38 | |
| 39 | apply( compiler ) { |
| 40 | // Hook after asset optimization if we're using a devtool (source map). |
| 41 | // @todo: Update to afterFinishAssets for Webpack 5.x? |
| 42 | const compilationHook = compiler.options.devtool ? 'afterOptimizeAssets' : 'additionalAssets'; |
| 43 | |
| 44 | compiler.hooks.compilation.tap( 'UnminifyWebpackPlugin', ( compilation ) => { |
| 45 | compilation.hooks[ compilationHook ].tap( 'UnminifyWebpackPlugin', () => { |
| 46 | const files = [ |
| 47 | ...compilation.additionalChunkAssets |
| 48 | ]; |
| 49 | |
| 50 | compilation.chunks.forEach( chunk => files.push( ...chunk.files ) ); |
| 51 | |
| 52 | const finalFiles = files.filter( ModuleFilenameHelpers.matchObject.bind( null, this.options ) ); |
| 53 | |
| 54 | finalFiles.forEach( ( minified ) => { |
| 55 | const asset = compilation.assets[ minified ]; |
| 56 | let source = asset.source(); |
| 57 | const ext = path.extname( minified ).substr( 1 ); |
| 58 | const unminified = getFileName( minified, ext, this.options ); |
| 59 | |
| 60 | // Remove the ".min" suffix from the lazy loaded chunk filenames. |
| 61 | if ( this.options.mainEntry && minified === this.options.mainEntry ) { |
| 62 | // See: https://github.com/webpack/webpack/blob/v4.43.0/lib/web/JsonpMainTemplatePlugin.js#L129 |
| 63 | // NOTE: This will break with Webpack 5.x! |
| 64 | source = source.replace( / \+ "\.min\.js"$/m, ' + ".js"' ); |
| 65 | } |
| 66 | |
| 67 | compilation.assets[ unminified ] = { |
| 68 | source: () => { |
| 69 | return source; |
| 70 | }, |
| 71 | size: () => { |
| 72 | return source.length; |
| 73 | } |
| 74 | }; |
| 75 | } ); |
| 76 | } ); |
| 77 | } ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | module.exports = UnminifyWebpackPlugin; |
| 82 |