AsyncModuleRuntimeModule.js
3.95 KB
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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
*/
"use strict";
const RuntimeGlobals = require("../RuntimeGlobals");
const Template = require("../Template");
const HelperRuntimeModule = require("./HelperRuntimeModule");
class AsyncModuleRuntimeModule extends HelperRuntimeModule {
constructor() {
super("async module");
}
/**
* @returns {string} runtime code
*/
generate() {
const { runtimeTemplate } = this.compilation;
const fn = RuntimeGlobals.asyncModule;
return Template.asString([
'var webpackQueues = typeof Symbol === "function" ? Symbol("webpack queues") : "__webpack_queues__";',
'var webpackExports = typeof Symbol === "function" ? Symbol("webpack exports") : "__webpack_exports__";',
'var webpackError = typeof Symbol === "function" ? Symbol("webpack error") : "__webpack_error__";',
`var resolveQueue = ${runtimeTemplate.basicFunction("queue", [
"if(queue && !queue.d) {",
Template.indent([
"queue.d = 1;",
`queue.forEach(${runtimeTemplate.expressionFunction(
"fn.r--",
"fn"
)});`,
`queue.forEach(${runtimeTemplate.expressionFunction(
"fn.r-- ? fn.r++ : fn()",
"fn"
)});`
]),
"}"
])}`,
`var wrapDeps = ${runtimeTemplate.returningFunction(
`deps.map(${runtimeTemplate.basicFunction("dep", [
'if(dep !== null && typeof dep === "object") {',
Template.indent([
"if(dep[webpackQueues]) return dep;",
"if(dep.then) {",
Template.indent([
"var queue = [];",
"queue.d = 0;",
`dep.then(${runtimeTemplate.basicFunction("r", [
"obj[webpackExports] = r;",
"resolveQueue(queue);"
])}, ${runtimeTemplate.basicFunction("e", [
"obj[webpackError] = e;",
"resolveQueue(queue);"
])});`,
"var obj = {};",
`obj[webpackQueues] = ${runtimeTemplate.expressionFunction(
`fn(queue)`,
"fn"
)};`,
"return obj;"
]),
"}"
]),
"}",
"var ret = {};",
`ret[webpackQueues] = ${runtimeTemplate.emptyFunction()};`,
"ret[webpackExports] = dep;",
"return ret;"
])})`,
"deps"
)};`,
`${fn} = ${runtimeTemplate.basicFunction("module, body, hasAwait", [
"var queue;",
"hasAwait && ((queue = []).d = 1);",
"var depQueues = new Set();",
"var exports = module.exports;",
"var currentDeps;",
"var outerResolve;",
"var reject;",
`var promise = new Promise(${runtimeTemplate.basicFunction(
"resolve, rej",
["reject = rej;", "outerResolve = resolve;"]
)});`,
"promise[webpackExports] = exports;",
`promise[webpackQueues] = ${runtimeTemplate.expressionFunction(
`queue && fn(queue), depQueues.forEach(fn), promise["catch"](${runtimeTemplate.emptyFunction()})`,
"fn"
)};`,
"module.exports = promise;",
`body(${runtimeTemplate.basicFunction("deps", [
"currentDeps = wrapDeps(deps);",
"var fn;",
`var getResult = ${runtimeTemplate.returningFunction(
`currentDeps.map(${runtimeTemplate.basicFunction("d", [
"if(d[webpackError]) throw d[webpackError];",
"return d[webpackExports];"
])})`
)}`,
`var promise = new Promise(${runtimeTemplate.basicFunction(
"resolve",
[
`fn = ${runtimeTemplate.expressionFunction(
"resolve(getResult)",
""
)};`,
"fn.r = 0;",
`var fnQueue = ${runtimeTemplate.expressionFunction(
"q !== queue && !depQueues.has(q) && (depQueues.add(q), q && !q.d && (fn.r++, q.push(fn)))",
"q"
)};`,
`currentDeps.map(${runtimeTemplate.expressionFunction(
"dep[webpackQueues](fnQueue)",
"dep"
)});`
]
)});`,
"return fn.r ? promise : getResult();"
])}, ${runtimeTemplate.expressionFunction(
"(err ? reject(promise[webpackError] = err) : outerResolve(exports)), resolveQueue(queue)",
"err"
)});`,
"queue && (queue.d = 0);"
])};`
]);
}
}
module.exports = AsyncModuleRuntimeModule;