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