Bez popisu

unminify.js 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. const getFileName = ( name, ext, opts ) => {
  16. if ( name.match(/([-_.]min)[-_.]/ ) ) {
  17. return name.replace( /[-_.]min/, '' );
  18. }
  19. const suffix = ( opts.postfix || 'nomin' ) + '.' + ext;
  20. if ( name.match( new RegExp( '\.' + ext + '$' ) ) ) {
  21. return name.replace( new RegExp( ext + '$' ), suffix )
  22. }
  23. return name + suffix;
  24. };
  25. class UnminifyWebpackPlugin {
  26. constructor( opts ) {
  27. const options = opts || {};
  28. this.options = {
  29. test: options.test || /\.(js|css)($|\?)/i,
  30. mainEntry: options.mainEntry || false,
  31. };
  32. }
  33. apply( compiler ) {
  34. // Hook after asset optimization if we're using a devtool (source map).
  35. // @todo: Update to afterFinishAssets for Webpack 5.x?
  36. const compilationHook = compiler.options.devtool ? 'afterOptimizeAssets' : 'additionalAssets';
  37. compiler.hooks.compilation.tap( 'UnminifyWebpackPlugin', ( compilation ) => {
  38. compilation.hooks[ compilationHook ].tap( 'UnminifyWebpackPlugin', () => {
  39. const files = [
  40. ...compilation.additionalChunkAssets
  41. ];
  42. compilation.chunks.forEach( chunk => files.push( ...chunk.files ) );
  43. const finalFiles = files.filter( ModuleFilenameHelpers.matchObject.bind( null, this.options ) );
  44. finalFiles.forEach( ( minified ) => {
  45. const asset = compilation.assets[ minified ];
  46. let source = asset.source();
  47. const ext = path.extname( minified ).substr( 1 );
  48. const unminified = getFileName( minified, ext, this.options );
  49. // Remove the ".min" suffix from the lazy loaded chunk filenames.
  50. if ( this.options.mainEntry && minified === this.options.mainEntry ) {
  51. // See: https://github.com/webpack/webpack/blob/v4.43.0/lib/web/JsonpMainTemplatePlugin.js#L129
  52. // NOTE: This will break with Webpack 5.x!
  53. source = source.replace( / \+ "\.min\.js"$/m, ' + ".js"' );
  54. }
  55. compilation.assets[ unminified ] = {
  56. source: () => {
  57. return source;
  58. },
  59. size: () => {
  60. return source.length;
  61. }
  62. };
  63. } );
  64. } );
  65. } );
  66. }
  67. }
  68. module.exports = UnminifyWebpackPlugin;