马宇豪
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
 
"use strict";
 
const { ConcatSource, PrefixSource } = require("webpack-sources");
const InitFragment = require("./InitFragment");
const Template = require("./Template");
const { mergeRuntime } = require("./util/runtime");
 
/** @typedef {import("webpack-sources").Source} Source */
/** @typedef {import("./Generator").GenerateContext} GenerateContext */
/** @typedef {import("./util/runtime").RuntimeSpec} RuntimeSpec */
 
const wrapInCondition = (condition, source) => {
    if (typeof source === "string") {
        return Template.asString([
            `if (${condition}) {`,
            Template.indent(source),
            "}",
            ""
        ]);
    } else {
        return new ConcatSource(
            `if (${condition}) {\n`,
            new PrefixSource("\t", source),
            "}\n"
        );
    }
};
 
/**
 * @typedef {GenerateContext} Context
 */
class ConditionalInitFragment extends InitFragment {
    /**
     * @param {string|Source} content the source code that will be included as initialization code
     * @param {number} stage category of initialization code (contribute to order)
     * @param {number} position position in the category (contribute to order)
     * @param {string} key unique key to avoid emitting the same initialization code twice
     * @param {RuntimeSpec | boolean} runtimeCondition in which runtime this fragment should be executed
     * @param {string|Source=} endContent the source code that will be included at the end of the module
     */
    constructor(
        content,
        stage,
        position,
        key,
        runtimeCondition = true,
        endContent
    ) {
        super(content, stage, position, key, endContent);
        this.runtimeCondition = runtimeCondition;
    }
 
    /**
     * @param {Context} context context
     * @returns {string|Source} the source code that will be included as initialization code
     */
    getContent(context) {
        if (this.runtimeCondition === false || !this.content) return "";
        if (this.runtimeCondition === true) return this.content;
        const expr = context.runtimeTemplate.runtimeConditionExpression({
            chunkGraph: context.chunkGraph,
            runtimeRequirements: context.runtimeRequirements,
            runtime: context.runtime,
            runtimeCondition: this.runtimeCondition
        });
        if (expr === "true") return this.content;
        return wrapInCondition(expr, this.content);
    }
 
    /**
     * @param {Context} context context
     * @returns {string|Source=} the source code that will be included at the end of the module
     */
    getEndContent(context) {
        if (this.runtimeCondition === false || !this.endContent) return "";
        if (this.runtimeCondition === true) return this.endContent;
        const expr = context.runtimeTemplate.runtimeConditionExpression({
            chunkGraph: context.chunkGraph,
            runtimeRequirements: context.runtimeRequirements,
            runtime: context.runtime,
            runtimeCondition: this.runtimeCondition
        });
        if (expr === "true") return this.endContent;
        return wrapInCondition(expr, this.endContent);
    }
 
    merge(other) {
        if (this.runtimeCondition === true) return this;
        if (other.runtimeCondition === true) return other;
        if (this.runtimeCondition === false) return other;
        if (other.runtimeCondition === false) return this;
        const runtimeCondition = mergeRuntime(
            this.runtimeCondition,
            other.runtimeCondition
        );
        return new ConditionalInitFragment(
            this.content,
            this.stage,
            this.position,
            this.key,
            runtimeCondition,
            this.endContent
        );
    }
}
 
module.exports = ConditionalInitFragment;