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