@@ -782,8 +782,7 @@ describe('HtmlWebpackPlugin', () => {
782
782
apply : function ( compiler ) {
783
783
compiler . plugin ( 'compilation' , compilation => {
784
784
HtmlWebpackPlugin . getHooks ( compilation ) . alterAssetTags . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
785
- expect ( typeof object . body ) . toBe ( 'object' ) ;
786
- expect ( typeof object . head ) . toBe ( 'object' ) ;
785
+ expect ( Object . keys ( object . assetTags ) ) . toEqual ( [ 'scripts' , 'styles' , 'meta' ] ) ;
787
786
eventFired = true ;
788
787
callback ( ) ;
789
788
} ) ;
@@ -817,9 +816,9 @@ describe('HtmlWebpackPlugin', () => {
817
816
apply : function ( compiler ) {
818
817
compiler . plugin ( 'compilation' , compilation => {
819
818
HtmlWebpackPlugin . getHooks ( compilation ) . alterAssetTags . tapAsync ( 'HtmlWebpackPluginTest' , ( pluginArgs , callback ) => {
820
- pluginArgs . body = pluginArgs . body . map ( tag => {
819
+ pluginArgs . assetTags . scripts = pluginArgs . assetTags . scripts . map ( tag => {
821
820
if ( tag . tagName === 'script' ) {
822
- tag . attributes . async = true ;
821
+ tag . attributes . specialAttribute = true ;
823
822
}
824
823
return tag ;
825
824
} ) ;
@@ -842,7 +841,7 @@ describe('HtmlWebpackPlugin', () => {
842
841
examplePlugin
843
842
]
844
843
} ,
845
- [ / < b o d y > [ \s ] * < s c r i p t s r c = " a p p _ b u n d l e .j s " a s y n c > < \/ s c r i p t > [ \s ] * < \/ b o d y > / ] ,
844
+ [ / < b o d y > [ \s ] * < s c r i p t s r c = " a p p _ b u n d l e .j s " s p e c i a l A t t r i b u t e > < \/ s c r i p t > [ \s ] * < \/ b o d y > / ] ,
846
845
null , done , false , false ) ;
847
846
} ) ;
848
847
@@ -851,7 +850,7 @@ describe('HtmlWebpackPlugin', () => {
851
850
apply : function ( compiler ) {
852
851
compiler . plugin ( 'compilation' , compilation => {
853
852
HtmlWebpackPlugin . getHooks ( compilation ) . alterAssetTags . tapAsync ( 'HtmlWebpackPluginTest' , ( pluginArgs , callback ) => {
854
- pluginArgs . body = pluginArgs . body . map ( tag => {
853
+ pluginArgs . assetTags . scripts = pluginArgs . assetTags . scripts . map ( tag => {
855
854
if ( tag . tagName === 'script' ) {
856
855
tag . attributes . async = false ;
857
856
}
@@ -880,12 +879,12 @@ describe('HtmlWebpackPlugin', () => {
880
879
null , done , false , false ) ;
881
880
} ) ;
882
881
883
- it ( 'fires the html-webpack-plugin-before-html-processing event' , done => {
882
+ it ( 'fires the html-webpack-plugin-after-template-execution event' , done => {
884
883
let eventFired = false ;
885
884
const examplePlugin = {
886
885
apply : function ( compiler ) {
887
886
compiler . plugin ( 'compilation' , compilation => {
888
- HtmlWebpackPlugin . getHooks ( compilation ) . beforeHtmlProcessing . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
887
+ HtmlWebpackPlugin . getHooks ( compilation ) . afterTemplateExecution . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
889
888
eventFired = true ;
890
889
callback ( ) ;
891
890
} ) ;
@@ -914,12 +913,12 @@ describe('HtmlWebpackPlugin', () => {
914
913
shouldExpectWarnings ) ;
915
914
} ) ;
916
915
917
- it ( 'fires the html-webpack-plugin-after-html-processing event' , done => {
916
+ it ( 'fires the html-webpack-plugin-before-emit event' , done => {
918
917
let eventFired = false ;
919
918
const examplePlugin = {
920
919
apply : function ( compiler ) {
921
920
compiler . plugin ( 'compilation' , compilation => {
922
- HtmlWebpackPlugin . getHooks ( compilation ) . afterHtmlProcessing . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
921
+ HtmlWebpackPlugin . getHooks ( compilation ) . beforeEmit . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
923
922
eventFired = true ;
924
923
callback ( ) ;
925
924
} ) ;
@@ -978,12 +977,12 @@ describe('HtmlWebpackPlugin', () => {
978
977
} ) ;
979
978
} ) ;
980
979
981
- it ( 'allows to modify the html during html-webpack-plugin-after-html-processing event' , done => {
980
+ it ( 'allows to modify the html during html-webpack-plugin-before-emit event' , done => {
982
981
let eventFired = false ;
983
982
const examplePlugin = {
984
983
apply : function ( compiler ) {
985
984
compiler . plugin ( 'compilation' , compilation => {
986
- HtmlWebpackPlugin . getHooks ( compilation ) . afterHtmlProcessing . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
985
+ HtmlWebpackPlugin . getHooks ( compilation ) . beforeEmit . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
987
986
eventFired = true ;
988
987
object . html += 'Injected by plugin' ;
989
988
callback ( ) ;
@@ -1013,13 +1012,50 @@ describe('HtmlWebpackPlugin', () => {
1013
1012
shouldExpectWarnings ) ;
1014
1013
} ) ;
1015
1014
1016
- it ( 'allows to modify sequentially the html during html-webpack-plugin-after-html-processing event by edit the given arguments object' , done => {
1015
+ it ( 'allows to access all hooks from within a plugin' , done => {
1016
+ let hookNames ;
1017
+ const examplePlugin = {
1018
+ apply : function ( compiler ) {
1019
+ compiler . plugin ( 'compilation' , compilation => {
1020
+ hookNames = Object . keys ( HtmlWebpackPlugin . getHooks ( compilation ) ) . sort ( ) ;
1021
+ } ) ;
1022
+ }
1023
+ } ;
1024
+
1025
+ const shouldExpectWarnings = webpackMajorVersion < 4 ;
1026
+ testHtmlPlugin ( {
1027
+ mode : 'production' ,
1028
+ entry : {
1029
+ app : path . join ( __dirname , 'fixtures/index.js' )
1030
+ } ,
1031
+ output : {
1032
+ path : OUTPUT_DIR ,
1033
+ filename : '[name]_bundle.js'
1034
+ } ,
1035
+ plugins : [
1036
+ new HtmlWebpackPlugin ( ) ,
1037
+ examplePlugin
1038
+ ]
1039
+ } , [ ] , null , ( ) => {
1040
+ expect ( hookNames ) . toEqual ( [
1041
+ 'afterEmit' ,
1042
+ 'afterTemplateExecution' ,
1043
+ 'alterAssetTagGroups' ,
1044
+ 'alterAssetTags' ,
1045
+ 'beforeAssetTagGeneration' ,
1046
+ 'beforeEmit' ] ) ;
1047
+ done ( ) ;
1048
+ } , false ,
1049
+ shouldExpectWarnings ) ;
1050
+ } ) ;
1051
+
1052
+ it ( 'allows to modify sequentially the html during html-webpack-plugin-before-emit event by edit the given arguments object' , done => {
1017
1053
let eventFiredForFirstPlugin = false ;
1018
1054
let eventFiredForSecondPlugin = false ;
1019
1055
const examplePlugin = {
1020
1056
apply : function ( compiler ) {
1021
1057
compiler . plugin ( 'compilation' , compilation => {
1022
- HtmlWebpackPlugin . getHooks ( compilation ) . afterHtmlProcessing . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1058
+ HtmlWebpackPlugin . getHooks ( compilation ) . beforeEmit . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1023
1059
eventFiredForFirstPlugin = true ;
1024
1060
object . html += 'Injected by first plugin' ;
1025
1061
callback ( null , object ) ;
@@ -1030,7 +1066,7 @@ describe('HtmlWebpackPlugin', () => {
1030
1066
const secondExamplePlugin = {
1031
1067
apply : function ( compiler ) {
1032
1068
compiler . plugin ( 'compilation' , compilation => {
1033
- HtmlWebpackPlugin . getHooks ( compilation ) . afterHtmlProcessing . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1069
+ HtmlWebpackPlugin . getHooks ( compilation ) . beforeEmit . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1034
1070
eventFiredForSecondPlugin = true ;
1035
1071
object . html += ' Injected by second plugin' ;
1036
1072
callback ( null ) ;
@@ -1062,13 +1098,13 @@ describe('HtmlWebpackPlugin', () => {
1062
1098
shouldExpectWarnings ) ;
1063
1099
} ) ;
1064
1100
1065
- it ( 'allows to modify sequentially the html during html-webpack-plugin-after-html-processing event either by edit the given arguments object or by return a new object in the callback' , done => {
1101
+ it ( 'allows to modify sequentially the html during html-webpack-plugin-before-emit event either by edit the given arguments object or by return a new object in the callback' , done => {
1066
1102
let eventFiredForFirstPlugin = false ;
1067
1103
let eventFiredForSecondPlugin = false ;
1068
1104
const examplePlugin = {
1069
1105
apply : function ( compiler ) {
1070
1106
compiler . plugin ( 'compilation' , compilation => {
1071
- HtmlWebpackPlugin . getHooks ( compilation ) . afterHtmlProcessing . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1107
+ HtmlWebpackPlugin . getHooks ( compilation ) . beforeEmit . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1072
1108
eventFiredForFirstPlugin = true ;
1073
1109
const result = _ . extend ( object , {
1074
1110
html : object . html + 'Injected by first plugin'
@@ -1081,7 +1117,7 @@ describe('HtmlWebpackPlugin', () => {
1081
1117
const secondExamplePlugin = {
1082
1118
apply : function ( compiler ) {
1083
1119
compiler . plugin ( 'compilation' , compilation => {
1084
- HtmlWebpackPlugin . getHooks ( compilation ) . afterHtmlProcessing . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1120
+ HtmlWebpackPlugin . getHooks ( compilation ) . beforeEmit . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1085
1121
eventFiredForSecondPlugin = true ;
1086
1122
object . html += ' Injected by second plugin' ;
1087
1123
callback ( null ) ;
@@ -1113,13 +1149,13 @@ describe('HtmlWebpackPlugin', () => {
1113
1149
shouldExpectWarnings ) ;
1114
1150
} ) ;
1115
1151
1116
- it ( 'allows to modify sequentially the html during html-webpack-plugin-after-html-processing event by return a new object in the callback' , done => {
1152
+ it ( 'allows to modify sequentially the html during html-webpack-plugin-before-emit event by return a new object in the callback' , done => {
1117
1153
let eventFiredForFirstPlugin = false ;
1118
1154
let eventFiredForSecondPlugin = false ;
1119
1155
const examplePlugin = {
1120
1156
apply : function ( compiler ) {
1121
1157
compiler . plugin ( 'compilation' , compilation => {
1122
- HtmlWebpackPlugin . getHooks ( compilation ) . afterHtmlProcessing . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1158
+ HtmlWebpackPlugin . getHooks ( compilation ) . beforeEmit . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1123
1159
eventFiredForFirstPlugin = true ;
1124
1160
const result = _ . extend ( object , {
1125
1161
html : object . html + 'Injected by first plugin'
@@ -1132,7 +1168,7 @@ describe('HtmlWebpackPlugin', () => {
1132
1168
const secondExamplePlugin = {
1133
1169
apply : function ( compiler ) {
1134
1170
compiler . plugin ( 'compilation' , compilation => {
1135
- HtmlWebpackPlugin . getHooks ( compilation ) . afterHtmlProcessing . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1171
+ HtmlWebpackPlugin . getHooks ( compilation ) . beforeEmit . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1136
1172
eventFiredForSecondPlugin = true ;
1137
1173
const result = _ . extend ( object , {
1138
1174
html : object . html + ' Injected by second plugin'
@@ -1164,14 +1200,14 @@ describe('HtmlWebpackPlugin', () => {
1164
1200
} ) ;
1165
1201
} ) ;
1166
1202
1167
- it ( 'allows to modify the html during html-webpack-plugin-before-html-processing event' , done => {
1203
+ it ( 'allows to modify the html during html-webpack-plugin-after-template-execution event' , done => {
1168
1204
let eventFired = false ;
1169
1205
const examplePlugin = {
1170
1206
apply : function ( compiler ) {
1171
1207
compiler . plugin ( 'compilation' , compilation => {
1172
- HtmlWebpackPlugin . getHooks ( compilation ) . beforeHtmlProcessing . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1208
+ HtmlWebpackPlugin . getHooks ( compilation ) . afterTemplateExecution . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1173
1209
eventFired = true ;
1174
- object . assets . js . push ( { path : 'funky-script.js' } ) ;
1210
+ object . bodyTags . push ( HtmlWebpackPlugin . createHtmlTagObject ( 'script' , { src : 'funky-script.js' } ) ) ;
1175
1211
object . html += 'Injected by plugin' ;
1176
1212
callback ( ) ;
1177
1213
} ) ;
@@ -1200,12 +1236,12 @@ describe('HtmlWebpackPlugin', () => {
1200
1236
shouldExpectWarnings ) ;
1201
1237
} ) ;
1202
1238
1203
- it ( 'allows to modify the html during html-webpack-plugin-before-html -generation event' , done => {
1239
+ it ( 'allows to modify the html during html-webpack-plugin-before-asset-tag -generation event' , done => {
1204
1240
let eventFired = false ;
1205
1241
const examplePlugin = {
1206
1242
apply : function ( compiler ) {
1207
1243
compiler . plugin ( 'compilation' , compilation => {
1208
- HtmlWebpackPlugin . getHooks ( compilation ) . beforeHtmlGeneration . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1244
+ HtmlWebpackPlugin . getHooks ( compilation ) . beforeAssetTagGeneration . tapAsync ( 'HtmlWebpackPluginTest' , ( object , callback ) => {
1209
1245
eventFired = true ;
1210
1246
object . assets . js . push ( { path : 'funky-script.js' } ) ;
1211
1247
callback ( ) ;
@@ -1237,10 +1273,11 @@ describe('HtmlWebpackPlugin', () => {
1237
1273
1238
1274
it ( 'fires the events in the correct order' , done => {
1239
1275
const hookCallOrder = [
1240
- 'beforeHtmlGeneration' ,
1241
- 'beforeHtmlProcessing' ,
1276
+ 'beforeAssetTagGeneration' ,
1242
1277
'alterAssetTags' ,
1243
- 'afterHtmlProcessing' ,
1278
+ 'alterAssetTagGroups' ,
1279
+ 'afterTemplateExecution' ,
1280
+ 'beforeEmit' ,
1244
1281
'afterEmit'
1245
1282
] ;
1246
1283
let eventsFired = [ ] ;
0 commit comments