@@ -9,8 +9,15 @@ const Template = require("./Template");
99class JsonpMainTemplatePlugin {
1010
1111 apply ( mainTemplate ) {
12+ function needChunkLoadingCode ( chunk ) {
13+ var otherChunksInEntry = chunk . entrypoints . some ( function ( entrypoint ) {
14+ return entrypoint . chunks . length > 1 ;
15+ } ) ;
16+ var onDemandChunks = chunk . chunks . length > 0 ;
17+ return otherChunksInEntry || onDemandChunks ;
18+ }
1219 mainTemplate . plugin ( "local-vars" , function ( source , chunk ) {
13- if ( chunk . chunks . length > 0 ) {
20+ if ( needChunkLoadingCode ( chunk ) ) {
1421 return this . asString ( [
1522 source ,
1623 "" ,
@@ -19,7 +26,9 @@ class JsonpMainTemplatePlugin {
1926 this . indent (
2027 chunk . ids . map ( id => `${ JSON . stringify ( id ) } : 0` ) . join ( ",\n" )
2128 ) ,
22- "};"
29+ "};" ,
30+ "" ,
31+ "var scheduledModules = [];"
2332 ] ) ;
2433 }
2534 return source ;
@@ -57,9 +66,13 @@ class JsonpMainTemplatePlugin {
5766 this . indent ( `script.setAttribute("nonce", ${ this . requireFn } .nc);` ) ,
5867 "}" ,
5968 `script.src = ${ this . requireFn } .p + ${ scriptSrcPath } ;` ,
60- `var timeout = setTimeout(onScriptComplete, ${ chunkLoadTimeout } );` ,
69+ "var timeout = setTimeout(function(){" ,
70+ this . indent ( [
71+ "onScriptComplete({ type: 'timeout', target: script });" ,
72+ ] ) ,
73+ `}, ${ chunkLoadTimeout } );` ,
6174 "script.onerror = script.onload = onScriptComplete;" ,
62- "function onScriptComplete() {" ,
75+ "function onScriptComplete(event ) {" ,
6376 this . indent ( [
6477 "// avoid mem leaks in IE." ,
6578 "script.onerror = script.onload = null;" ,
@@ -68,7 +81,14 @@ class JsonpMainTemplatePlugin {
6881 "if(chunk !== 0) {" ,
6982 this . indent ( [
7083 "if(chunk) {" ,
71- this . indent ( "chunk[1](new Error('Loading chunk ' + chunkId + ' failed.'));" ) ,
84+ this . indent ( [
85+ "var errorType = event && (event.type === 'load' ? 'missing' : event.type);" ,
86+ "var realSrc = event && event.target && event.target.src;" ,
87+ "var error = new Error('Loading chunk ' + chunkId + ' failed.\\n(' + errorType + ': ' + realSrc + ')');" ,
88+ "error.type = errorType;" ,
89+ "error.request = realSrc;" ,
90+ "chunk[1](error);"
91+ ] ) ,
7292 "}" ,
7393 "installedChunks[chunkId] = undefined;"
7494 ] ) ,
@@ -120,15 +140,14 @@ class JsonpMainTemplatePlugin {
120140 ] ) ;
121141 } ) ;
122142 mainTemplate . plugin ( "bootstrap" , function ( source , chunk , hash ) {
123- if ( chunk . chunks . length > 0 ) {
124- var jsonpFunction = this . outputOptions . jsonpFunction ;
143+ if ( needChunkLoadingCode ( chunk ) ) {
125144 return this . asString ( [
126145 source ,
127146 "" ,
128147 "// install a JSONP callback for chunk loading" ,
129- `var parentJsonpFunction = window[${ JSON . stringify ( jsonpFunction ) } ];` ,
130- `window[${ JSON . stringify ( jsonpFunction ) } ] = function webpackJsonpCallback(chunkIds, moreModules, executeModules) {` ,
148+ "function webpackJsonpCallback(data) {" ,
131149 this . indent ( [
150+ "var chunkIds = data[0], moreModules = data[1], executeModules = data[2];" ,
132151 "// add \"moreModules\" to the modules object," ,
133152 "// then flag all \"chunkIds\" as loaded and fire callback" ,
134153 "var moduleId, chunkId, i = 0, resolves = [], result;" ,
@@ -148,15 +167,28 @@ class JsonpMainTemplatePlugin {
148167 "}"
149168 ] ) ,
150169 "}" ,
151- "if(parentJsonpFunction) parentJsonpFunction(chunkIds, moreModules, executeModules );" ,
170+ "if(parentJsonpFunction) parentJsonpFunction(data );" ,
152171 "while(resolves.length) {" ,
153172 this . indent ( "resolves.shift()();" ) ,
154173 "}" ,
155174 this . entryPointInChildren ( chunk ) ? [
156- "if(executeModules) {" ,
175+ "scheduledModules.push.apply(scheduledModules, executeModules || []);" ,
176+ "" ,
177+ "for(i = 0; i < scheduledModules.length; i++) {" ,
157178 this . indent ( [
158- "for(i=0; i < executeModules.length; i++) {" ,
159- this . indent ( `result = ${ this . requireFn } (${ this . requireFn } .s = executeModules[i]);` ) ,
179+ "var scheduledModule = scheduledModules[i];" ,
180+ "var fullfilled = true;" ,
181+ "for(var j = 1; j < scheduledModule.length; j++) {" ,
182+ this . indent ( [
183+ "var depId = scheduledModule[j];" ,
184+ "if(installedChunks[depId] !== 0) fullfilled = false;"
185+ ] ) ,
186+ "}" ,
187+ "if(fullfilled) {" ,
188+ this . indent ( [
189+ "scheduledModules.splice(i--, 1);" ,
190+ "result = " + this . requireFn + "(" + this . requireFn + ".s = scheduledModule[0]);" ,
191+ ] ) ,
160192 "}"
161193 ] ) ,
162194 "}" ,
@@ -168,6 +200,21 @@ class JsonpMainTemplatePlugin {
168200 }
169201 return source ;
170202 } ) ;
203+ mainTemplate . plugin ( "startup" , function ( source , chunk , hash ) {
204+ if ( needChunkLoadingCode ( chunk ) ) {
205+ var jsonpFunction = this . outputOptions . jsonpFunction ;
206+ return this . asString ( [
207+ `var jsonpArray = window[${ JSON . stringify ( jsonpFunction ) } ] = window[${ JSON . stringify ( jsonpFunction ) } ] || [];` ,
208+ "var parentJsonpFunction = jsonpArray.push.bind(jsonpArray);" ,
209+ "jsonpArray.push = webpackJsonpCallback;" ,
210+ "jsonpArray = jsonpArray.slice();" ,
211+ "for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);" ,
212+ "" ,
213+ source
214+ ] ) ;
215+ }
216+ return source ;
217+ } ) ;
171218 mainTemplate . plugin ( "hot-bootstrap" , function ( source , chunk , hash ) {
172219 const hotUpdateChunkFilename = this . outputOptions . hotUpdateChunkFilename ;
173220 const hotUpdateMainFilename = this . outputOptions . hotUpdateMainFilename ;
@@ -198,7 +245,7 @@ this[${JSON.stringify(hotUpdateFunction)}] = ${runtimeSource}`;
198245 } ) ;
199246 mainTemplate . plugin ( "hash" , function ( hash ) {
200247 hash . update ( "jsonp" ) ;
201- hash . update ( "4 " ) ;
248+ hash . update ( "5 " ) ;
202249 hash . update ( `${ this . outputOptions . filename } ` ) ;
203250 hash . update ( `${ this . outputOptions . chunkFilename } ` ) ;
204251 hash . update ( `${ this . outputOptions . jsonpFunction } ` ) ;
0 commit comments