[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | "use strict";
|
---|
| 6 |
|
---|
| 7 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 8 | const Template = require("../Template");
|
---|
| 9 | const HelperRuntimeModule = require("./HelperRuntimeModule");
|
---|
| 10 |
|
---|
| 11 | /** @typedef {import("../Compilation")} Compilation */
|
---|
| 12 |
|
---|
| 13 | class AsyncModuleRuntimeModule extends HelperRuntimeModule {
|
---|
| 14 | constructor() {
|
---|
| 15 | super("async module");
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * @returns {string | null} runtime code
|
---|
| 20 | */
|
---|
| 21 | generate() {
|
---|
| 22 | const compilation = /** @type {Compilation} */ (this.compilation);
|
---|
| 23 | const { runtimeTemplate } = compilation;
|
---|
| 24 | const fn = RuntimeGlobals.asyncModule;
|
---|
| 25 | return Template.asString([
|
---|
| 26 | 'var webpackQueues = typeof Symbol === "function" ? Symbol("webpack queues") : "__webpack_queues__";',
|
---|
| 27 | `var webpackExports = typeof Symbol === "function" ? Symbol("webpack exports") : "${RuntimeGlobals.exports}";`,
|
---|
| 28 | 'var webpackError = typeof Symbol === "function" ? Symbol("webpack error") : "__webpack_error__";',
|
---|
| 29 | `var resolveQueue = ${runtimeTemplate.basicFunction("queue", [
|
---|
| 30 | "if(queue && queue.d < 1) {",
|
---|
| 31 | Template.indent([
|
---|
| 32 | "queue.d = 1;",
|
---|
| 33 | `queue.forEach(${runtimeTemplate.expressionFunction(
|
---|
| 34 | "fn.r--",
|
---|
| 35 | "fn"
|
---|
| 36 | )});`,
|
---|
| 37 | `queue.forEach(${runtimeTemplate.expressionFunction(
|
---|
| 38 | "fn.r-- ? fn.r++ : fn()",
|
---|
| 39 | "fn"
|
---|
| 40 | )});`
|
---|
| 41 | ]),
|
---|
| 42 | "}"
|
---|
| 43 | ])}`,
|
---|
| 44 | `var wrapDeps = ${runtimeTemplate.returningFunction(
|
---|
| 45 | `deps.map(${runtimeTemplate.basicFunction("dep", [
|
---|
| 46 | 'if(dep !== null && typeof dep === "object") {',
|
---|
| 47 | Template.indent([
|
---|
| 48 | "if(dep[webpackQueues]) return dep;",
|
---|
| 49 | "if(dep.then) {",
|
---|
| 50 | Template.indent([
|
---|
| 51 | "var queue = [];",
|
---|
| 52 | "queue.d = 0;",
|
---|
| 53 | `dep.then(${runtimeTemplate.basicFunction("r", [
|
---|
| 54 | "obj[webpackExports] = r;",
|
---|
| 55 | "resolveQueue(queue);"
|
---|
| 56 | ])}, ${runtimeTemplate.basicFunction("e", [
|
---|
| 57 | "obj[webpackError] = e;",
|
---|
| 58 | "resolveQueue(queue);"
|
---|
| 59 | ])});`,
|
---|
| 60 | "var obj = {};",
|
---|
| 61 | `obj[webpackQueues] = ${runtimeTemplate.expressionFunction(
|
---|
| 62 | "fn(queue)",
|
---|
| 63 | "fn"
|
---|
| 64 | )};`,
|
---|
| 65 | "return obj;"
|
---|
| 66 | ]),
|
---|
| 67 | "}"
|
---|
| 68 | ]),
|
---|
| 69 | "}",
|
---|
| 70 | "var ret = {};",
|
---|
| 71 | `ret[webpackQueues] = ${runtimeTemplate.emptyFunction()};`,
|
---|
| 72 | "ret[webpackExports] = dep;",
|
---|
| 73 | "return ret;"
|
---|
| 74 | ])})`,
|
---|
| 75 | "deps"
|
---|
| 76 | )};`,
|
---|
| 77 | `${fn} = ${runtimeTemplate.basicFunction("module, body, hasAwait", [
|
---|
| 78 | "var queue;",
|
---|
| 79 | "hasAwait && ((queue = []).d = -1);",
|
---|
| 80 | "var depQueues = new Set();",
|
---|
| 81 | "var exports = module.exports;",
|
---|
| 82 | "var currentDeps;",
|
---|
| 83 | "var outerResolve;",
|
---|
| 84 | "var reject;",
|
---|
| 85 | `var promise = new Promise(${runtimeTemplate.basicFunction(
|
---|
| 86 | "resolve, rej",
|
---|
| 87 | ["reject = rej;", "outerResolve = resolve;"]
|
---|
| 88 | )});`,
|
---|
| 89 | "promise[webpackExports] = exports;",
|
---|
| 90 | `promise[webpackQueues] = ${runtimeTemplate.expressionFunction(
|
---|
| 91 | `queue && fn(queue), depQueues.forEach(fn), promise["catch"](${runtimeTemplate.emptyFunction()})`,
|
---|
| 92 | "fn"
|
---|
| 93 | )};`,
|
---|
| 94 | "module.exports = promise;",
|
---|
| 95 | `body(${runtimeTemplate.basicFunction("deps", [
|
---|
| 96 | "currentDeps = wrapDeps(deps);",
|
---|
| 97 | "var fn;",
|
---|
| 98 | `var getResult = ${runtimeTemplate.returningFunction(
|
---|
| 99 | `currentDeps.map(${runtimeTemplate.basicFunction("d", [
|
---|
| 100 | "if(d[webpackError]) throw d[webpackError];",
|
---|
| 101 | "return d[webpackExports];"
|
---|
| 102 | ])})`
|
---|
| 103 | )}`,
|
---|
| 104 | `var promise = new Promise(${runtimeTemplate.basicFunction(
|
---|
| 105 | "resolve",
|
---|
| 106 | [
|
---|
| 107 | `fn = ${runtimeTemplate.expressionFunction(
|
---|
| 108 | "resolve(getResult)",
|
---|
| 109 | ""
|
---|
| 110 | )};`,
|
---|
| 111 | "fn.r = 0;",
|
---|
| 112 | `var fnQueue = ${runtimeTemplate.expressionFunction(
|
---|
| 113 | "q !== queue && !depQueues.has(q) && (depQueues.add(q), q && !q.d && (fn.r++, q.push(fn)))",
|
---|
| 114 | "q"
|
---|
| 115 | )};`,
|
---|
| 116 | `currentDeps.map(${runtimeTemplate.expressionFunction(
|
---|
| 117 | "dep[webpackQueues](fnQueue)",
|
---|
| 118 | "dep"
|
---|
| 119 | )});`
|
---|
| 120 | ]
|
---|
| 121 | )});`,
|
---|
| 122 | "return fn.r ? promise : getResult();"
|
---|
| 123 | ])}, ${runtimeTemplate.expressionFunction(
|
---|
| 124 | "(err ? reject(promise[webpackError] = err) : outerResolve(exports)), resolveQueue(queue)",
|
---|
| 125 | "err"
|
---|
| 126 | )});`,
|
---|
| 127 | "queue && queue.d < 0 && (queue.d = 0);"
|
---|
| 128 | ])};`
|
---|
| 129 | ]);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | module.exports = AsyncModuleRuntimeModule;
|
---|