| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const InitFragment = require("../InitFragment");
|
|---|
| 9 | const RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 10 | const Template = require("../Template");
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("webpack-sources").Source} Source */
|
|---|
| 13 | /** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
|---|
| 14 |
|
|---|
| 15 | /** @typedef {Map<string, string>} Dependencies */
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * Represents AwaitDependenciesInitFragment.
|
|---|
| 19 | * @extends {InitFragment<GenerateContext>}
|
|---|
| 20 | */
|
|---|
| 21 | class AwaitDependenciesInitFragment extends InitFragment {
|
|---|
| 22 | /**
|
|---|
| 23 | * Creates an instance of AwaitDependenciesInitFragment.
|
|---|
| 24 | * @param {Dependencies} dependencies maps an import var to an async module that needs to be awaited
|
|---|
| 25 | */
|
|---|
| 26 | constructor(dependencies) {
|
|---|
| 27 | super(
|
|---|
| 28 | undefined,
|
|---|
| 29 | InitFragment.STAGE_ASYNC_DEPENDENCIES,
|
|---|
| 30 | 0,
|
|---|
| 31 | "await-dependencies"
|
|---|
| 32 | );
|
|---|
| 33 | /** @type {Dependencies} */
|
|---|
| 34 | this.dependencies = dependencies;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Merges another await-dependencies fragment into this fragment.
|
|---|
| 39 | * @param {AwaitDependenciesInitFragment} other other AwaitDependenciesInitFragment
|
|---|
| 40 | * @returns {AwaitDependenciesInitFragment} AwaitDependenciesInitFragment
|
|---|
| 41 | */
|
|---|
| 42 | merge(other) {
|
|---|
| 43 | const dependencies = new Map(other.dependencies);
|
|---|
| 44 | for (const [key, value] of this.dependencies) {
|
|---|
| 45 | dependencies.set(key, value);
|
|---|
| 46 | }
|
|---|
| 47 | return new AwaitDependenciesInitFragment(dependencies);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Returns the source code that will be included as initialization code.
|
|---|
| 52 | * @param {GenerateContext} context context
|
|---|
| 53 | * @returns {string | Source | undefined} the source code that will be included as initialization code
|
|---|
| 54 | */
|
|---|
| 55 | getContent({ runtimeRequirements, runtimeTemplate }) {
|
|---|
| 56 | runtimeRequirements.add(RuntimeGlobals.module);
|
|---|
| 57 | if (this.dependencies.size === 0) {
|
|---|
| 58 | return "";
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | const importVars = [...this.dependencies.keys()];
|
|---|
| 62 | const asyncModuleValues = [...this.dependencies.values()].join(", ");
|
|---|
| 63 |
|
|---|
| 64 | const templateInput = [
|
|---|
| 65 | `var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([${asyncModuleValues}]);`
|
|---|
| 66 | ];
|
|---|
| 67 |
|
|---|
| 68 | if (
|
|---|
| 69 | this.dependencies.size === 1 ||
|
|---|
| 70 | !runtimeTemplate.supportsDestructuring()
|
|---|
| 71 | ) {
|
|---|
| 72 | templateInput.push(
|
|---|
| 73 | "var __webpack_async_dependencies_result__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);"
|
|---|
| 74 | );
|
|---|
| 75 | for (const [index, importVar] of importVars.entries()) {
|
|---|
| 76 | templateInput.push(
|
|---|
| 77 | `${importVar} = __webpack_async_dependencies_result__[${index}];`
|
|---|
| 78 | );
|
|---|
| 79 | }
|
|---|
| 80 | } else {
|
|---|
| 81 | const importVarsStr = importVars.join(", ");
|
|---|
| 82 |
|
|---|
| 83 | templateInput.push(
|
|---|
| 84 | `([${importVarsStr}] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);`
|
|---|
| 85 | );
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | templateInput.push("");
|
|---|
| 89 |
|
|---|
| 90 | return Template.asString(templateInput);
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | module.exports = AwaitDependenciesInitFragment;
|
|---|