@@ -6,7 +6,7 @@ const promisify = require('util.promisify');
6
6
7
7
// Import types
8
8
/* eslint-disable */
9
- /// <reference path="./index .d.ts" />
9
+ /// <reference path="./typings .d.ts" />
10
10
/* eslint-enable */
11
11
/** @typedef {import("webpack/lib/Compiler.js") } WebpackCompiler */
12
12
/** @typedef {import("webpack/lib/Compilation.js") } WebpackCompilation */
@@ -15,14 +15,14 @@ const vm = require('vm');
15
15
const fs = require ( 'fs' ) ;
16
16
const _ = require ( 'lodash' ) ;
17
17
const path = require ( 'path' ) ;
18
- const SyncWaterfallHook = require ( 'tapable' ) . SyncWaterfallHook ;
19
- const AsyncSeriesWaterfallHook = require ( 'tapable' ) . AsyncSeriesWaterfallHook ;
20
18
21
19
const htmlTagObjectToString = require ( './lib/html-tags' ) . htmlTagObjectToString ;
22
20
23
21
const childCompiler = require ( './lib/compiler.js' ) ;
24
22
const prettyError = require ( './lib/errors.js' ) ;
25
23
const chunkSorter = require ( './lib/chunksorter.js' ) ;
24
+ const getHtmlWebpackPluginHooks = require ( './lib/hooks.js' ) . getHtmlWebpackPluginHooks ;
25
+ const getHtmlWebpackPluginHook = require ( './lib/hooks.js' ) . getHtmlWebpackPluginHook ;
26
26
27
27
const fsStatAsync = promisify ( fs . stat ) ;
28
28
const fsReadFileAsync = promisify ( fs . readFile ) ;
@@ -86,18 +86,7 @@ class HtmlWebpackPlugin {
86
86
}
87
87
88
88
// setup hooks for third party plugins
89
- compiler . hooks . compilation . tap ( 'HtmlWebpackPluginHooks' , compilation => {
90
- // Setup the hooks only once
91
- if ( compilation . hooks . htmlWebpackPluginAlterChunks ) {
92
- return ;
93
- }
94
- compilation . hooks . htmlWebpackPluginAlterChunks = new SyncWaterfallHook ( [ 'chunks' , 'objectWithPluginRef' ] ) ;
95
- compilation . hooks . htmlWebpackPluginBeforeHtmlGeneration = new AsyncSeriesWaterfallHook ( [ 'pluginArgs' ] ) ;
96
- compilation . hooks . htmlWebpackPluginBeforeHtmlProcessing = new AsyncSeriesWaterfallHook ( [ 'pluginArgs' ] ) ;
97
- compilation . hooks . htmlWebpackPluginAlterAssetTags = new AsyncSeriesWaterfallHook ( [ 'pluginArgs' ] ) ;
98
- compilation . hooks . htmlWebpackPluginAfterHtmlProcessing = new AsyncSeriesWaterfallHook ( [ 'pluginArgs' ] ) ;
99
- compilation . hooks . htmlWebpackPluginAfterEmit = new AsyncSeriesWaterfallHook ( [ 'pluginArgs' ] ) ;
100
- } ) ;
89
+ compiler . hooks . compilation . tap ( 'HtmlWebpackPluginHooks' , getHtmlWebpackPluginHooks ) ;
101
90
102
91
compiler . hooks . make . tapAsync ( 'HtmlWebpackPlugin' , ( compilation , callback ) => {
103
92
// Compile the template (queued)
@@ -126,7 +115,6 @@ class HtmlWebpackPlugin {
126
115
* @param {() => void } callback
127
116
*/
128
117
( compilation , callback ) => {
129
- const applyPluginsAsyncWaterfall = self . applyPluginsAsyncWaterfall ( compilation ) ;
130
118
// Get all entry point names for this html file
131
119
const entryNames = Array . from ( compilation . entrypoints . keys ( ) ) ;
132
120
const filteredEntryNames = self . filterChunks ( entryNames , self . options . chunks , self . options . excludeChunks ) ;
@@ -176,7 +164,7 @@ class HtmlWebpackPlugin {
176
164
} )
177
165
// Allow plugins to make changes to the assets before invoking the template
178
166
// This only makes sense to use if `inject` is `false`
179
- . then ( compilationResult => applyPluginsAsyncWaterfall ( 'htmlWebpackPluginBeforeHtmlGeneration' , false , {
167
+ . then ( compilationResult => getHtmlWebpackPluginHook ( compilation , 'htmlWebpackPluginBeforeHtmlGeneration' ) . promise ( {
180
168
assets : assets ,
181
169
outputName : self . childCompilationOutputName ,
182
170
plugin : self
@@ -189,7 +177,7 @@ class HtmlWebpackPlugin {
189
177
// Allow plugins to change the html before assets are injected
190
178
. then ( html => {
191
179
const pluginArgs = { html : html , assets : assets , plugin : self , outputName : self . childCompilationOutputName } ;
192
- return applyPluginsAsyncWaterfall ( 'htmlWebpackPluginBeforeHtmlProcessing' , true , pluginArgs ) ;
180
+ return getHtmlWebpackPluginHook ( compilation , 'htmlWebpackPluginBeforeHtmlProcessing' ) . promise ( pluginArgs ) ;
193
181
} )
194
182
. then ( result => {
195
183
const html = result . html ;
@@ -198,7 +186,7 @@ class HtmlWebpackPlugin {
198
186
const assetTags = self . generateHtmlTagObjects ( assets ) ;
199
187
const pluginArgs = { head : assetTags . head , body : assetTags . body , plugin : self , outputName : self . childCompilationOutputName } ;
200
188
// Allow plugins to change the assetTag definitions
201
- return applyPluginsAsyncWaterfall ( 'htmlWebpackPluginAlterAssetTags' , true , pluginArgs )
189
+ return getHtmlWebpackPluginHook ( compilation , 'htmlWebpackPluginAlterAssetTags' ) . promise ( pluginArgs )
202
190
. then ( result => self . postProcessHtml ( html , assets , { body : result . body , head : result . head } )
203
191
. then ( html => _ . extend ( result , { html : html , assets : assets } ) ) ) ;
204
192
} )
@@ -207,7 +195,7 @@ class HtmlWebpackPlugin {
207
195
const html = result . html ;
208
196
const assets = result . assets ;
209
197
const pluginArgs = { html : html , assets : assets , plugin : self , outputName : self . childCompilationOutputName } ;
210
- return applyPluginsAsyncWaterfall ( 'htmlWebpackPluginAfterHtmlProcessing' , true , pluginArgs )
198
+ return getHtmlWebpackPluginHook ( compilation , 'htmlWebpackPluginAfterHtmlProcessing' ) . promise ( pluginArgs )
211
199
. then ( result => result . html ) ;
212
200
} )
213
201
. catch ( err => {
@@ -225,7 +213,7 @@ class HtmlWebpackPlugin {
225
213
size : ( ) => html . length
226
214
} ;
227
215
} )
228
- . then ( ( ) => applyPluginsAsyncWaterfall ( 'htmlWebpackPluginAfterEmit' , false , {
216
+ . then ( ( ) => getHtmlWebpackPluginHook ( compilation , 'htmlWebpackPluginAfterEmit' ) . promise ( {
229
217
html : compilation . assets [ self . childCompilationOutputName ] ,
230
218
outputName : self . childCompilationOutputName ,
231
219
plugin : self
@@ -684,30 +672,6 @@ class HtmlWebpackPlugin {
684
672
files . sort ( ) ;
685
673
return files ;
686
674
}
687
-
688
- /**
689
- * Helper to promisify compilation.applyPluginsAsyncWaterfall that returns
690
- * a function that helps to merge given plugin arguments with processed ones
691
- *
692
- * @param {WebpackCompilation } compilation
693
- *
694
- */
695
- applyPluginsAsyncWaterfall ( compilation ) {
696
- return ( eventName , requiresResult , pluginArgs ) => {
697
- if ( ! compilation . hooks [ eventName ] ) {
698
- compilation . errors . push (
699
- new Error ( 'No hook found for ' + eventName )
700
- ) ;
701
- }
702
- return compilation . hooks [ eventName ] . promise ( pluginArgs )
703
- . then ( result => {
704
- if ( requiresResult && ! result ) {
705
- throw new Error ( 'Using ' + eventName + ' did not return a result.' ) ;
706
- }
707
- return result ;
708
- } ) ;
709
- } ;
710
- }
711
675
}
712
676
713
677
/**
0 commit comments