[79a0317] | 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 | const Template = require("../Template");
|
---|
| 10 |
|
---|
| 11 | /** @typedef {import("../Compilation")} Compilation */
|
---|
| 12 |
|
---|
| 13 | class OnChunksLoadedRuntimeModule extends RuntimeModule {
|
---|
| 14 | constructor() {
|
---|
| 15 | super("chunk loaded");
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | /**
|
---|
| 19 | * @returns {string | null} runtime code
|
---|
| 20 | */
|
---|
| 21 | generate() {
|
---|
| 22 | const compilation = /** @type {Compilation} */ (this.compilation);
|
---|
| 23 | const { runtimeTemplate } = compilation;
|
---|
| 24 | return Template.asString([
|
---|
| 25 | "var deferred = [];",
|
---|
| 26 | `${RuntimeGlobals.onChunksLoaded} = ${runtimeTemplate.basicFunction(
|
---|
| 27 | "result, chunkIds, fn, priority",
|
---|
| 28 | [
|
---|
| 29 | "if(chunkIds) {",
|
---|
| 30 | Template.indent([
|
---|
| 31 | "priority = priority || 0;",
|
---|
| 32 | "for(var i = deferred.length; i > 0 && deferred[i - 1][2] > priority; i--) deferred[i] = deferred[i - 1];",
|
---|
| 33 | "deferred[i] = [chunkIds, fn, priority];",
|
---|
| 34 | "return;"
|
---|
| 35 | ]),
|
---|
| 36 | "}",
|
---|
| 37 | "var notFulfilled = Infinity;",
|
---|
| 38 | "for (var i = 0; i < deferred.length; i++) {",
|
---|
| 39 | Template.indent([
|
---|
| 40 | runtimeTemplate.destructureArray(
|
---|
| 41 | ["chunkIds", "fn", "priority"],
|
---|
| 42 | "deferred[i]"
|
---|
| 43 | ),
|
---|
| 44 | "var fulfilled = true;",
|
---|
| 45 | "for (var j = 0; j < chunkIds.length; j++) {",
|
---|
| 46 | Template.indent([
|
---|
| 47 | `if ((priority & 1 === 0 || notFulfilled >= priority) && Object.keys(${
|
---|
| 48 | RuntimeGlobals.onChunksLoaded
|
---|
| 49 | }).every(${runtimeTemplate.returningFunction(
|
---|
| 50 | `${RuntimeGlobals.onChunksLoaded}[key](chunkIds[j])`,
|
---|
| 51 | "key"
|
---|
| 52 | )})) {`,
|
---|
| 53 | Template.indent(["chunkIds.splice(j--, 1);"]),
|
---|
| 54 | "} else {",
|
---|
| 55 | Template.indent([
|
---|
| 56 | "fulfilled = false;",
|
---|
| 57 | "if(priority < notFulfilled) notFulfilled = priority;"
|
---|
| 58 | ]),
|
---|
| 59 | "}"
|
---|
| 60 | ]),
|
---|
| 61 | "}",
|
---|
| 62 | "if(fulfilled) {",
|
---|
| 63 | Template.indent([
|
---|
| 64 | "deferred.splice(i--, 1)",
|
---|
| 65 | "var r = fn();",
|
---|
| 66 | "if (r !== undefined) result = r;"
|
---|
| 67 | ]),
|
---|
| 68 | "}"
|
---|
| 69 | ]),
|
---|
| 70 | "}",
|
---|
| 71 | "return result;"
|
---|
| 72 | ]
|
---|
| 73 | )};`
|
---|
| 74 | ]);
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | module.exports = OnChunksLoadedRuntimeModule;
|
---|