[6a3a178] | 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 | class AsyncModuleRuntimeModule extends HelperRuntimeModule {
|
---|
| 12 | constructor() {
|
---|
| 13 | super("async module");
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | /**
|
---|
| 17 | * @returns {string} runtime code
|
---|
| 18 | */
|
---|
| 19 | generate() {
|
---|
| 20 | const { runtimeTemplate } = this.compilation;
|
---|
| 21 | const fn = RuntimeGlobals.asyncModule;
|
---|
| 22 | return Template.asString([
|
---|
| 23 | 'var webpackThen = typeof Symbol === "function" ? Symbol("webpack then") : "__webpack_then__";',
|
---|
| 24 | 'var webpackExports = typeof Symbol === "function" ? Symbol("webpack exports") : "__webpack_exports__";',
|
---|
| 25 | `var completeQueue = ${runtimeTemplate.basicFunction("queue", [
|
---|
| 26 | "if(queue) {",
|
---|
| 27 | Template.indent([
|
---|
| 28 | `queue.forEach(${runtimeTemplate.expressionFunction(
|
---|
| 29 | "fn.r--",
|
---|
| 30 | "fn"
|
---|
| 31 | )});`,
|
---|
| 32 | `queue.forEach(${runtimeTemplate.expressionFunction(
|
---|
| 33 | "fn.r-- ? fn.r++ : fn()",
|
---|
| 34 | "fn"
|
---|
| 35 | )});`
|
---|
| 36 | ]),
|
---|
| 37 | "}"
|
---|
| 38 | ])}`,
|
---|
| 39 | `var completeFunction = ${runtimeTemplate.expressionFunction(
|
---|
| 40 | "!--fn.r && fn()",
|
---|
| 41 | "fn"
|
---|
| 42 | )};`,
|
---|
| 43 | `var queueFunction = ${runtimeTemplate.expressionFunction(
|
---|
| 44 | "queue ? queue.push(fn) : completeFunction(fn)",
|
---|
| 45 | "queue, fn"
|
---|
| 46 | )};`,
|
---|
| 47 | `var wrapDeps = ${runtimeTemplate.returningFunction(
|
---|
| 48 | `deps.map(${runtimeTemplate.basicFunction("dep", [
|
---|
| 49 | 'if(dep !== null && typeof dep === "object") {',
|
---|
| 50 | Template.indent([
|
---|
| 51 | "if(dep[webpackThen]) return dep;",
|
---|
| 52 | "if(dep.then) {",
|
---|
| 53 | Template.indent([
|
---|
| 54 | "var queue = [];",
|
---|
| 55 | `dep.then(${runtimeTemplate.basicFunction("r", [
|
---|
| 56 | "obj[webpackExports] = r;",
|
---|
| 57 | "completeQueue(queue);",
|
---|
| 58 | "queue = 0;"
|
---|
| 59 | ])});`,
|
---|
| 60 | `var obj = {};
|
---|
| 61 | obj[webpackThen] = ${runtimeTemplate.expressionFunction(
|
---|
| 62 | "queueFunction(queue, fn), dep.catch(reject)",
|
---|
| 63 | "fn, reject"
|
---|
| 64 | )};`,
|
---|
| 65 | "return obj;"
|
---|
| 66 | ]),
|
---|
| 67 | "}"
|
---|
| 68 | ]),
|
---|
| 69 | "}",
|
---|
| 70 | `var ret = {};
|
---|
| 71 | ret[webpackThen] = ${runtimeTemplate.expressionFunction(
|
---|
| 72 | "completeFunction(fn)",
|
---|
| 73 | "fn"
|
---|
| 74 | )};
|
---|
| 75 | ret[webpackExports] = dep;
|
---|
| 76 | return ret;`
|
---|
| 77 | ])})`,
|
---|
| 78 | "deps"
|
---|
| 79 | )};`,
|
---|
| 80 | `${fn} = ${runtimeTemplate.basicFunction("module, body, hasAwait", [
|
---|
| 81 | "var queue = hasAwait && [];",
|
---|
| 82 | "var exports = module.exports;",
|
---|
| 83 | "var currentDeps;",
|
---|
| 84 | "var outerResolve;",
|
---|
| 85 | "var reject;",
|
---|
| 86 | "var isEvaluating = true;",
|
---|
| 87 | "var nested = false;",
|
---|
| 88 | `var whenAll = ${runtimeTemplate.basicFunction(
|
---|
| 89 | "deps, onResolve, onReject",
|
---|
| 90 | [
|
---|
| 91 | "if (nested) return;",
|
---|
| 92 | "nested = true;",
|
---|
| 93 | "onResolve.r += deps.length;",
|
---|
| 94 | `deps.map(${runtimeTemplate.expressionFunction(
|
---|
| 95 | "dep[webpackThen](onResolve, onReject)",
|
---|
| 96 | "dep, i"
|
---|
| 97 | )});`,
|
---|
| 98 | "nested = false;"
|
---|
| 99 | ]
|
---|
| 100 | )};`,
|
---|
| 101 | `var promise = new Promise(${runtimeTemplate.basicFunction(
|
---|
| 102 | "resolve, rej",
|
---|
| 103 | [
|
---|
| 104 | "reject = rej;",
|
---|
| 105 | `outerResolve = ${runtimeTemplate.expressionFunction(
|
---|
| 106 | "resolve(exports), completeQueue(queue), queue = 0"
|
---|
| 107 | )};`
|
---|
| 108 | ]
|
---|
| 109 | )});`,
|
---|
| 110 | "promise[webpackExports] = exports;",
|
---|
| 111 | `promise[webpackThen] = ${runtimeTemplate.basicFunction(
|
---|
| 112 | "fn, rejectFn",
|
---|
| 113 | [
|
---|
| 114 | "if (isEvaluating) { return completeFunction(fn); }",
|
---|
| 115 | "if (currentDeps) whenAll(currentDeps, fn, rejectFn);",
|
---|
| 116 | "queueFunction(queue, fn);",
|
---|
| 117 | "promise.catch(rejectFn);"
|
---|
| 118 | ]
|
---|
| 119 | )};`,
|
---|
| 120 | "module.exports = promise;",
|
---|
| 121 | `body(${runtimeTemplate.basicFunction("deps", [
|
---|
| 122 | "if(!deps) return outerResolve();",
|
---|
| 123 | "currentDeps = wrapDeps(deps);",
|
---|
| 124 | "var fn, result;",
|
---|
| 125 | `var promise = new Promise(${runtimeTemplate.basicFunction(
|
---|
| 126 | "resolve, reject",
|
---|
| 127 | [
|
---|
| 128 | `fn = ${runtimeTemplate.expressionFunction(
|
---|
| 129 | `resolve(result = currentDeps.map(${runtimeTemplate.returningFunction(
|
---|
| 130 | "d[webpackExports]",
|
---|
| 131 | "d"
|
---|
| 132 | )}))`
|
---|
| 133 | )};`,
|
---|
| 134 | "fn.r = 0;",
|
---|
| 135 | "whenAll(currentDeps, fn, reject);"
|
---|
| 136 | ]
|
---|
| 137 | )});`,
|
---|
| 138 | "return fn.r ? promise : result;"
|
---|
| 139 | ])}).then(outerResolve, reject);`,
|
---|
| 140 | "isEvaluating = false;"
|
---|
| 141 | ])};`
|
---|
| 142 | ]);
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | module.exports = AsyncModuleRuntimeModule;
|
---|