-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-template-require-ensure.js
143 lines (140 loc) · 6.58 KB
/
main-template-require-ensure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/** @module scriptfiber/plugins/scriptfiber-runtime-webpack-plugin/main-template-require-ensure */
import {Template} from 'webpack';
/**
* @description Scriptfiber JSONP Chunk Loading script generator.
* @param {Object<string,*>} mainTemplate - Main template object.
* @returns {string} Generated script.
*/
function mainTemplateRequireEnsure(mainTemplate) {
return Template.asString([
'// SCRIPTFIBER JSONP CHUNK LOADING FOR JAVASCRIPT',
'',
'var installedChunkData = installedChunks[chunkId];',
'',
'if (installedChunkData !== 0) { // 0 MEANS "ALREADY INSTALLED"',
Template.indent([
'// A PROMISE MEANS "CURRENTLY LOADING"',
'if (installedChunkData) {',
Template.indent(['promises.push(installedChunkData[2]);']),
'} else {',
Template.indent([
'// SETUP PROMISE IN CHUNK CACHE',
'var promise = new Promise(function(resolve, reject) {',
Template.indent([
'installedChunkData = installedChunks[chunkId] = [resolve, reject];'
]),
'});',
'promises.push(installedChunkData[2] = promise);',
'',
'// START CHUNK LOADING',
'',
'// *** NOTE: "jsonpScript" HOOK CALL HAS BEEN REMOVED ***',
'',
'var eventObj = {',
Template.indent([
'cancelable: true,',
'detail: {',
Template.indent([
'src: jsonpScriptSrc(chunkId),',
'callback: function callback(src) {',
Template.indent([
'var script = document.createElement("script");',
mainTemplate.outputOptions.jsonpScriptType
? Template.asString([
'',
`script.type = ${JSON.stringify(
mainTemplate.outputOptions
.jsonpScriptType
)};`,
''
])
: '',
'script.charset = "utf-8";',
`script.timeout = ${
mainTemplate.outputOptions.chunkLoadTimeout /
1000
};`,
'',
`if (${mainTemplate.requireFn}.nc) {`,
Template.indent([
`script.setAttribute("nonce", ${mainTemplate.requireFn}.nc);`
]),
'}',
'',
'script.src = src;',
mainTemplate.outputOptions.crossOriginLoading
? Template.asString([
'',
'if (script.src.indexOf(window.location.origin + "/") !== 0) {',
Template.indent([
`script.crossOrigin = ${JSON.stringify(
mainTemplate.outputOptions
.crossOriginLoading
)};`
]),
'}',
''
])
: '',
'// CREATE ERROR BEFORE STACK UNWOUND TO GET USEFUL STACKTRACE LATER',
'var error = new Error();',
'',
'var onScriptComplete = function (e) {',
Template.indent([
'// AVOID MEM LEAKS IN IE',
'script.onerror = script.onload = null;',
'',
'clearTimeout(timeout);',
'',
'var chunk = installedChunks[chunkId];',
'',
'if(chunk !== 0) {',
Template.indent([
'if(chunk) {',
Template.indent([
'var errorType = e && (e.type === "load" ? "missing" : e.type);',
'var realSrc = e && e.target && e.target.src;',
'error.message = "Loading chunk " + chunkId + " failed.\\n(" + errorType + ": " + realSrc + ")";',
'error.name = "ChunkLoadError";',
'error.type = errorType;',
'error.request = realSrc;',
'chunk[1](error);'
]),
'}',
'',
'installedChunks[chunkId] = undefined;'
]),
'}'
]),
'};',
'',
'var timeout = setTimeout(function(){',
Template.indent([
'onScriptComplete({ type: "timeout", target: script });'
]),
`}, ${mainTemplate.outputOptions.chunkLoadTimeout});`,
'',
'script.onerror = script.onload = onScriptComplete;',
'',
'document.head.appendChild(script);'
]),
'}'
]),
'}'
]),
'};',
'',
'var event = new CustomEvent("webpackModuleRequest", eventObj);',
'',
'if (window.dispatchEvent(event)) {',
Template.indent([
'eventObj.detail.callback(eventObj.detail.src);'
]),
'}'
]),
'}'
]),
'}'
]);
}
export {mainTemplateRequireEnsure};