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