@@ -8,6 +8,9 @@ const ConcatSource = require("webpack-sources").ConcatSource;
88const OriginalSource = require ( "webpack-sources" ) . OriginalSource ;
99const PrefixSource = require ( "webpack-sources" ) . PrefixSource ;
1010const Template = require ( "./Template" ) ;
11+ const SyncWaterfallHook = require ( "tapable" ) . SyncWaterfallHook ;
12+ const SyncHook = require ( "tapable" ) . SyncHook ;
13+ const SyncBailHook = require ( "tapable" ) . SyncBailHook ;
1114
1215// require function shortcuts:
1316// __webpack_require__.s = the module id of the entry point
@@ -28,6 +31,30 @@ const Template = require("./Template");
2831module . exports = class MainTemplate extends Template {
2932 constructor ( outputOptions ) {
3033 super ( outputOptions ) ;
34+ this . hooks = {
35+ modules : new SyncWaterfallHook ( [ "modules" , "chunk" , "hash" , "moduleTemplate" , "dependencyTemplates" ] ) ,
36+ moduleObj : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" , "moduleIdExpression" ] ) ,
37+ requireEnsure : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" , "chunkIdExpression" ] ) ,
38+ bootstrap : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" , "moduleTemplate" , "dependencyTemplates" ] ) ,
39+ localVars : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" ] ) ,
40+ require : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" ] ) ,
41+ requireExtensions : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" ] ) ,
42+ startup : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" ] ) ,
43+ render : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" , "moduleTemplate" , "dependencyTemplates" ] ) ,
44+ renderWithEntry : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" ] ) ,
45+ moduleRequire : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" , "moduleIdExpression" ] ) ,
46+ addModule : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" , "moduleIdExpression" , "moduleExpression" ] ) ,
47+ currentHash : new SyncWaterfallHook ( [ "source" , "requestedLength" ] ) ,
48+ assetPath : new SyncWaterfallHook ( [ "path" , "options" ] ) ,
49+ hash : new SyncHook ( [ "hash" ] ) ,
50+ hashForChunk : new SyncHook ( [ "hash" , "chunk" ] ) ,
51+ globalHashPaths : new SyncWaterfallHook ( [ "paths" ] ) ,
52+ globalHash : new SyncBailHook ( [ "chunk" , "paths" ] ) ,
53+
54+ // TODO this should be moved somewhere else
55+ // It's weird here
56+ hotBootstrap : new SyncWaterfallHook ( [ "source" , "chunk" , "hash" ] )
57+ } ;
3158 this . plugin ( "startup" , ( source , chunk , hash ) => {
3259 const buf = [ ] ;
3360 if ( chunk . entryModule ) {
@@ -44,7 +71,7 @@ module.exports = class MainTemplate extends Template {
4471 source . add ( "/************************************************************************/\n" ) ;
4572 source . add ( "/******/ (" ) ;
4673 const modules = this . renderChunkModules ( chunk , ( ) => true , moduleTemplate , dependencyTemplates , "/******/ " ) ;
47- source . add ( this . applyPluginsWaterfall ( " modules" , modules , chunk , hash , moduleTemplate , dependencyTemplates ) ) ;
74+ source . add ( this . hooks . modules . call ( modules , chunk , hash , moduleTemplate , dependencyTemplates ) ) ;
4875 source . add ( ")" ) ;
4976 return source ;
5077 } ) ;
@@ -64,7 +91,7 @@ module.exports = class MainTemplate extends Template {
6491 "}" ,
6592 "// Create a new module (and put it into the cache)" ,
6693 "var module = installedModules[moduleId] = {" ,
67- this . indent ( this . applyPluginsWaterfall ( "module-obj" , "" , chunk , hash , "moduleId" ) ) ,
94+ this . indent ( this . hooks . moduleObj . call ( "" , chunk , hash , "moduleId" ) ) ,
6895 "};" ,
6996 "" ,
7097 this . asString ( outputOptions . strictModuleExceptionHandling ? [
@@ -106,7 +133,7 @@ module.exports = class MainTemplate extends Template {
106133 buf . push ( "// The chunk loading function for additional chunks" ) ;
107134 buf . push ( `${ this . requireFn } .e = function requireEnsure(chunkId) {` ) ;
108135 buf . push ( this . indent ( "var promises = [];" ) ) ;
109- buf . push ( this . indent ( this . applyPluginsWaterfall ( "require-ensure" , "" , chunk , hash , "chunkId" ) ) ) ;
136+ buf . push ( this . indent ( this . hooks . requireEnsure . call ( "" , chunk , hash , "chunkId" ) ) ) ;
110137 buf . push ( this . indent ( "return Promise.all(promises);" ) ) ;
111138 buf . push ( "};" ) ;
112139 }
@@ -211,37 +238,37 @@ module.exports = class MainTemplate extends Template {
211238
212239 render ( hash , chunk , moduleTemplate , dependencyTemplates ) {
213240 const buf = [ ] ;
214- buf . push ( this . applyPluginsWaterfall ( " bootstrap" , "" , chunk , hash , moduleTemplate , dependencyTemplates ) ) ;
215- buf . push ( this . applyPluginsWaterfall ( "local-vars" , "" , chunk , hash ) ) ;
241+ buf . push ( this . hooks . bootstrap . call ( "" , chunk , hash , moduleTemplate , dependencyTemplates ) ) ;
242+ buf . push ( this . hooks . localVars . call ( "" , chunk , hash ) ) ;
216243 buf . push ( "" ) ;
217244 buf . push ( "// The require function" ) ;
218245 buf . push ( `function ${ this . requireFn } (moduleId) {` ) ;
219- buf . push ( this . indent ( this . applyPluginsWaterfall ( " require" , "" , chunk , hash ) ) ) ;
246+ buf . push ( this . indent ( this . hooks . require . call ( "" , chunk , hash ) ) ) ;
220247 buf . push ( "}" ) ;
221248 buf . push ( "" ) ;
222- buf . push ( this . asString ( this . applyPluginsWaterfall ( "require-extensions" , "" , chunk , hash ) ) ) ;
249+ buf . push ( this . asString ( this . hooks . requireExtensions . call ( "" , chunk , hash ) ) ) ;
223250 buf . push ( "" ) ;
224- buf . push ( this . asString ( this . applyPluginsWaterfall ( " startup" , "" , chunk , hash ) ) ) ;
225- let source = this . applyPluginsWaterfall ( " render" , new OriginalSource ( this . prefix ( buf , " \t" ) + "\n" , `webpack/bootstrap ${ hash } ` ) , chunk , hash , moduleTemplate , dependencyTemplates ) ;
251+ buf . push ( this . asString ( this . hooks . startup . call ( "" , chunk , hash ) ) ) ;
252+ let source = this . hooks . render . call ( new OriginalSource ( this . prefix ( buf , " \t" ) + "\n" , `webpack/bootstrap ${ hash } ` ) , chunk , hash , moduleTemplate , dependencyTemplates ) ;
226253 if ( chunk . hasEntryModule ( ) ) {
227- source = this . applyPluginsWaterfall ( "render-with-entry" , source , chunk , hash ) ;
254+ source = this . hooks . renderWithEntry . call ( source , chunk , hash ) ;
228255 }
229256 if ( ! source ) throw new Error ( "Compiler error: MainTemplate plugin 'render' should return something" ) ;
230257 chunk . rendered = true ;
231258 return new ConcatSource ( source , ";" ) ;
232259 }
233260
234261 renderRequireFunctionForModule ( hash , chunk , varModuleId ) {
235- return this . applyPluginsWaterfall ( "module-require" , this . requireFn , chunk , hash , varModuleId ) ;
262+ return this . hooks . moduleRequire . call ( this . requireFn , chunk , hash , varModuleId ) ;
236263 }
237264
238265 renderAddModule ( hash , chunk , varModuleId , varModule ) {
239- return this . applyPluginsWaterfall ( "add-module" , `modules[${ varModuleId } ] = ${ varModule } ;` , chunk , hash , varModuleId , varModule ) ;
266+ return this . hooks . addModule . call ( `modules[${ varModuleId } ] = ${ varModule } ;` , chunk , hash , varModuleId , varModule ) ;
240267 }
241268
242269 renderCurrentHashCode ( hash , length ) {
243270 length = length || Infinity ;
244- return this . applyPluginsWaterfall ( "current-hash" , JSON . stringify ( hash . substr ( 0 , length ) ) , length ) ;
271+ return this . hooks . currentHash . call ( JSON . stringify ( hash . substr ( 0 , length ) ) , length ) ;
245272 }
246273
247274 entryPointInChildren ( chunk ) {
@@ -259,23 +286,27 @@ module.exports = class MainTemplate extends Template {
259286 }
260287
261288 getPublicPath ( options ) {
262- return this . applyPluginsWaterfall ( "asset-path" , this . outputOptions . publicPath || "" , options ) ;
289+ return this . hooks . assetPath . call ( this . outputOptions . publicPath || "" , options ) ;
290+ }
291+
292+ getAssetPath ( path , options ) {
293+ return this . hooks . assetPath . call ( path , options ) ;
263294 }
264295
265296 updateHash ( hash ) {
266297 hash . update ( "maintemplate" ) ;
267298 hash . update ( "3" ) ;
268299 hash . update ( this . outputOptions . publicPath + "" ) ;
269- this . applyPlugins ( " hash" , hash ) ;
300+ this . hooks . hash . call ( hash ) ;
270301 }
271302
272303 updateHashForChunk ( hash , chunk ) {
273304 this . updateHash ( hash ) ;
274- this . applyPlugins ( "hash-for-chunk" , hash , chunk ) ;
305+ this . hooks . hashForChunk . call ( hash , chunk ) ;
275306 }
276307
277308 useChunkHash ( chunk ) {
278- const paths = this . applyPluginsWaterfall ( "global-hash-paths" , [ ] ) ;
279- return ! this . applyPluginsBailResult ( "global-hash" , chunk , paths ) ;
309+ const paths = this . hooks . globalHashPaths . call ( [ ] ) ;
310+ return ! this . hooks . globalHash . call ( chunk , paths ) ;
280311 }
281312} ;
0 commit comments