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 RuntimeModule = require("../RuntimeModule");
|
---|
9 |
|
---|
10 | /** @typedef {import("../Compilation")} Compilation */
|
---|
11 | /** @typedef {import("../MainTemplate")} MainTemplate */
|
---|
12 |
|
---|
13 | class StartupEntrypointRuntimeModule extends RuntimeModule {
|
---|
14 | /**
|
---|
15 | * @param {boolean} asyncChunkLoading use async chunk loading
|
---|
16 | */
|
---|
17 | constructor(asyncChunkLoading) {
|
---|
18 | super("startup entrypoint");
|
---|
19 | this.asyncChunkLoading = asyncChunkLoading;
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * @returns {string | null} runtime code
|
---|
24 | */
|
---|
25 | generate() {
|
---|
26 | const compilation = /** @type {Compilation} */ (this.compilation);
|
---|
27 | const { runtimeTemplate } = compilation;
|
---|
28 | return `${
|
---|
29 | RuntimeGlobals.startupEntrypoint
|
---|
30 | } = ${runtimeTemplate.basicFunction("result, chunkIds, fn", [
|
---|
31 | "// arguments: chunkIds, moduleId are deprecated",
|
---|
32 | "var moduleId = chunkIds;",
|
---|
33 | `if(!fn) chunkIds = result, fn = ${runtimeTemplate.returningFunction(
|
---|
34 | `${RuntimeGlobals.require}(${RuntimeGlobals.entryModuleId} = moduleId)`
|
---|
35 | )};`,
|
---|
36 | ...(this.asyncChunkLoading
|
---|
37 | ? [
|
---|
38 | `return Promise.all(chunkIds.map(${RuntimeGlobals.ensureChunk}, ${
|
---|
39 | RuntimeGlobals.require
|
---|
40 | })).then(${runtimeTemplate.basicFunction("", [
|
---|
41 | "var r = fn();",
|
---|
42 | "return r === undefined ? result : r;"
|
---|
43 | ])})`
|
---|
44 | ]
|
---|
45 | : [
|
---|
46 | `chunkIds.map(${RuntimeGlobals.ensureChunk}, ${RuntimeGlobals.require})`,
|
---|
47 | "var r = fn();",
|
---|
48 | "return r === undefined ? result : r;"
|
---|
49 | ])
|
---|
50 | ])}`;
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | module.exports = StartupEntrypointRuntimeModule;
|
---|