1- require ( 'require-safe' ) ( 'html-webpack-plugin' ) ;
1+ const HtmlWebpackPlugin = require ( 'html-webpack-plugin' ) ;
22
33// Create a regular expression to search for a file by chunk name & extension, including associated source maps
44function regex ( chunkName , fileExt ) {
@@ -17,6 +17,19 @@ class WebpackHashExcludePlugin {
1717 } , options ) ;
1818 }
1919
20+ apply ( compiler ) {
21+ // Remove the hash from asset file names
22+ compiler . hooks . emit . tapAsync ( 'emit' , ( compilation , callback ) => {
23+ this . removeAssetsHash ( compilation , callback ) ;
24+ } ) ;
25+ // Replace the hash value of the html static resource
26+ if ( this . options . removeHtmlHash ) {
27+ compiler . hooks . compilation . tap ( 'WebpackHashExclude' , ( compilation ) => {
28+ HtmlWebpackPlugin . getHooks ( compilation ) . alterAssetTags . tapAsync ( 'WebpackHashExclude' , ( data , callback ) => this . removeHtmlHash ( data , callback ) ) ;
29+ } ) ;
30+ }
31+ }
32+
2033 removeAssetsHash ( compilation , callback ) {
2134 const matchingChunks = compilation . chunks
2235 . filter ( ( chunk ) => this . options . excludeJs . indexOf ( chunk . name ) > - 1 || this . options . excludeCss . indexOf ( chunk . name ) > - 1 ) ;
@@ -49,49 +62,23 @@ class WebpackHashExcludePlugin {
4962 } ) ;
5063 }
5164
52- removeHtmlHash ( compilation , callback ) {
53- const chunksMapCallback = ( chunk ) => {
54- const chunkName = chunk . names [ 0 ] ;
55- const getIndex = ( item ) => item . attributes . href . indexOf ( chunkName ) > - 1 ;
56- let index ;
57-
58- if ( this . options . excludeJs . indexOf ( chunkName ) > - 1 ) {
59- index = compilation . body . findIndex ( getIndex ) ;
60- if ( index > - 1 ) {
61- compilation . body [ index ] . attributes . src = compilation . body [ index ] . attributes . src . replace ( regex ( chunkName , 'js' ) , '$1$2$3' ) ;
65+ renameAssetTags ( data , callback ) {
66+ data . assetTags . scripts . map ( ( assetTag , callback ) => {
67+ for ( let chunkName of this . options . excludeCss ) {
68+ if ( assetTag . attributes . src . search ( regex ( chunkName , 'js' ) ) > - 1 ) {
69+ assetTag . attributes . src = assetTag . attributes . src . replace ( regex ( chunkName , 'js' ) , '$1$2$3' ) ;
6270 }
6371 }
64- if ( this . options . excludeCss . indexOf ( chunkName ) > - 1 ) {
65- index = compilation . head . findIndex ( getIndex ) ;
66- if ( index > - 1 ) {
67- compilation . head [ index ] . attributes . href = compilation . head [ index ] . attributes . href . replace ( regex ( chunkName , 'css' ) , '$1$2$3' ) ;
72+ } ) ;
73+ data . assetTags . styles . map ( ( assetTag , callback ) => {
74+ for ( let chunkName of this . options . excludeCss ) {
75+ if ( assetTag . attributes . href . search ( regex ( chunkName , 'css' ) ) > - 1 ) {
76+ assetTag . attributes . href = assetTag . attributes . href . replace ( regex ( chunkName , 'css' ) , '$1$2$3' ) ;
6877 }
6978 }
70- } ;
71- compilation . chunks . map ( chunksMapCallback ) ;
72- callback ( ) ;
73- }
74-
75- apply ( compiler ) {
76- // Remove the hash from asset file names
77- compiler . hooks . emit . tapAsync ( 'emit' , ( compilation , callback ) => {
78- this . removeAssetsHash ( compilation , callback ) ;
7979 } ) ;
80- // Remove the hash value of the corresponding chunk resource file
81- compiler . hooks . thisCompilation . tap ( 'thisCompilation' , compilation => {
82- const requireEnsure = ( source ) => {
83- source . replace ( '__webpack_require__.p' , `'${ compilation . options . output . publicPath } '` )
84- } ;
85- compilation . mainTemplate . hooks . requireEnsure . tap ( 'requireEnsure' , requireEnsure ) ;
86- } ) ;
87- // Replace the hash value of the html static resource
88- if ( this . options . removeHtmlHash ) {
89- compiler . hooks . compilation . tap ( 'compilation' , ( compilation ) => {
90- compilation . hooks . htmlWebpackPluginAlterAssetTags . tapAsync ( 'compilation' , ( compilation , callback ) => {
91- this . removeHtmlHash ( compilation , callback ) ;
92- } ) ;
93- } ) ;
94- }
80+
81+ callback ( null , data ) ;
9582 }
9683}
9784
0 commit comments