@@ -28,6 +28,10 @@ class JsonpMainTemplatePlugin {
2828 }
2929 return false ;
3030 } ;
31+ const needPrefetchingCode = chunk => {
32+ const allPrefetchChunks = chunk . getChildIdsByOrdersMap ( true ) . prefetch ;
33+ return allPrefetchChunks && Object . keys ( allPrefetchChunks ) . length ;
34+ } ;
3135 // TODO refactor this
3236 if ( ! mainTemplate . hooks . jsonpScript ) {
3337 mainTemplate . hooks . jsonpScript = new SyncWaterfallHook ( [
@@ -330,6 +334,7 @@ class JsonpMainTemplatePlugin {
330334 ( source , chunk , hash ) => {
331335 if ( needChunkLoadingCode ( chunk ) ) {
332336 const withDefer = needEntryDeferringCode ( chunk ) ;
337+ const withPrefetch = needPrefetchingCode ( chunk ) ;
333338 return Template . asString ( [
334339 source ,
335340 "" ,
@@ -339,7 +344,7 @@ class JsonpMainTemplatePlugin {
339344 "var chunkIds = data[0];" ,
340345 "var moreModules = data[1];" ,
341346 withDefer ? "var executeModules = data[2];" : "" ,
342- "var prefetchChunks = data[3] || []" ,
347+ ... ( withPrefetch ? [ "var prefetchChunks = data[3] || []" ] : [ ] ) ,
343348 '// add "moreModules" to the modules object,' ,
344349 '// then flag all "chunkIds" as loaded and fire callback' ,
345350 "var moduleId, chunkId, i = 0, resolves = [];" ,
@@ -380,20 +385,23 @@ class JsonpMainTemplatePlugin {
380385 "return checkDeferredModules();"
381386 ] )
382387 : "" ,
383- "" ,
384- "// chunk prefetching for javascript" ,
385- "var head = document.getElementsByTagName('head')[0];" ,
386- "prefetchChunks.forEach(function(chunkId) {" ,
387- Template . indent ( [
388- "if(installedChunks[chunkId] === undefined) {" ,
389- Template . indent ( [
390- "installedChunks[chunkId] = null;" ,
391- mainTemplate . hooks . linkPrefetch . call ( "" , chunk , hash ) ,
392- "head.appendChild(link);"
393- ] ) ,
394- "}"
395- ] ) ,
396- "});"
388+ ...( withPrefetch
389+ ? [
390+ "// chunk prefetching for javascript" ,
391+ "var head = document.getElementsByTagName('head')[0];" ,
392+ "prefetchChunks.forEach(function(chunkId) {" ,
393+ Template . indent ( [
394+ "if(installedChunks[chunkId] === undefined) {" ,
395+ Template . indent ( [
396+ "installedChunks[chunkId] = null;" ,
397+ mainTemplate . hooks . linkPrefetch . call ( "" , chunk , hash ) ,
398+ "head.appendChild(link);"
399+ ] ) ,
400+ "}"
401+ ] ) ,
402+ "});"
403+ ]
404+ : [ ] )
397405 ] ) ,
398406 "};" ,
399407 withDefer
@@ -458,14 +466,17 @@ class JsonpMainTemplatePlugin {
458466 mainTemplate . hooks . beforeStartup . tap (
459467 "JsonpMainTemplatePlugin" ,
460468 ( source , chunk , hash ) => {
461- if ( needChunkLoadingCode ( chunk ) ) {
469+ const prefetchChunks = chunk . getChildIdsByOrders ( ) . prefetch ;
470+ if (
471+ needChunkLoadingCode ( chunk ) &&
472+ prefetchChunks &&
473+ prefetchChunks . length
474+ ) {
462475 return Template . asString ( [
463476 source ,
464477 `webpackJsonpCallback([${ JSON . stringify (
465478 chunk . ids
466- ) } , {}, null, ${ JSON . stringify (
467- chunk . getChildIdsByOrders ( ) . prefetch
468- ) } ])`
479+ ) } , {}, null, ${ JSON . stringify ( prefetchChunks ) } ])`
469480 ] ) ;
470481 }
471482 return source ;
0 commit comments