[6a3a178] | 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 | * @typedef {GenerateContext} Context
|
---|
| 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 | merge(other) {
|
---|
| 33 | const promises = new Set(this.promises);
|
---|
| 34 | for (const p of other.promises) {
|
---|
| 35 | promises.add(p);
|
---|
| 36 | }
|
---|
| 37 | return new AwaitDependenciesInitFragment(promises);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * @param {Context} context context
|
---|
| 42 | * @returns {string|Source} the source code that will be included as initialization code
|
---|
| 43 | */
|
---|
| 44 | getContent({ runtimeRequirements }) {
|
---|
| 45 | runtimeRequirements.add(RuntimeGlobals.module);
|
---|
| 46 | const promises = this.promises;
|
---|
| 47 | if (promises.size === 0) {
|
---|
| 48 | return "";
|
---|
| 49 | }
|
---|
| 50 | if (promises.size === 1) {
|
---|
| 51 | for (const p of promises) {
|
---|
| 52 | return Template.asString([
|
---|
| 53 | `var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([${p}]);`,
|
---|
| 54 | `${p} = (__webpack_async_dependencies__.then ? await __webpack_async_dependencies__ : __webpack_async_dependencies__)[0];`,
|
---|
| 55 | ""
|
---|
| 56 | ]);
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | const sepPromises = Array.from(promises).join(", ");
|
---|
| 60 | // TODO check if destructuring is supported
|
---|
| 61 | return Template.asString([
|
---|
| 62 | `var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([${sepPromises}]);`,
|
---|
| 63 | `([${sepPromises}] = __webpack_async_dependencies__.then ? await __webpack_async_dependencies__ : __webpack_async_dependencies__);`,
|
---|
| 64 | ""
|
---|
| 65 | ]);
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | module.exports = AwaitDependenciesInitFragment;
|
---|