[79a0317] | 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 | /**
|
---|
| 16 | * @extends {InitFragment<GenerateContext>}
|
---|
| 17 | */
|
---|
| 18 | class AwaitDependenciesInitFragment extends InitFragment {
|
---|
| 19 | /**
|
---|
| 20 | * @param {Set<string>} promises the promises that should be awaited
|
---|
| 21 | */
|
---|
| 22 | constructor(promises) {
|
---|
| 23 | super(
|
---|
| 24 | undefined,
|
---|
| 25 | InitFragment.STAGE_ASYNC_DEPENDENCIES,
|
---|
| 26 | 0,
|
---|
| 27 | "await-dependencies"
|
---|
| 28 | );
|
---|
| 29 | this.promises = promises;
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * @param {AwaitDependenciesInitFragment} other other AwaitDependenciesInitFragment
|
---|
| 34 | * @returns {AwaitDependenciesInitFragment} AwaitDependenciesInitFragment
|
---|
| 35 | */
|
---|
| 36 | merge(other) {
|
---|
| 37 | const promises = new Set(other.promises);
|
---|
| 38 | for (const p of this.promises) {
|
---|
| 39 | promises.add(p);
|
---|
| 40 | }
|
---|
| 41 | return new AwaitDependenciesInitFragment(promises);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /**
|
---|
| 45 | * @param {GenerateContext} context context
|
---|
| 46 | * @returns {string | Source | undefined} the source code that will be included as initialization code
|
---|
| 47 | */
|
---|
| 48 | getContent({ runtimeRequirements }) {
|
---|
| 49 | runtimeRequirements.add(RuntimeGlobals.module);
|
---|
| 50 | const promises = this.promises;
|
---|
| 51 | if (promises.size === 0) {
|
---|
| 52 | return "";
|
---|
| 53 | }
|
---|
| 54 | if (promises.size === 1) {
|
---|
| 55 | const [p] = promises;
|
---|
| 56 | return Template.asString([
|
---|
| 57 | `var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([${p}]);`,
|
---|
| 58 | `${p} = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];`,
|
---|
| 59 | ""
|
---|
| 60 | ]);
|
---|
| 61 | }
|
---|
| 62 | const sepPromises = Array.from(promises).join(", ");
|
---|
| 63 | // TODO check if destructuring is supported
|
---|
| 64 | return Template.asString([
|
---|
| 65 | `var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([${sepPromises}]);`,
|
---|
| 66 | `([${sepPromises}] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);`,
|
---|
| 67 | ""
|
---|
| 68 | ]);
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | module.exports = AwaitDependenciesInitFragment;
|
---|