WXL
4 天以前 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
 
"use strict";
 
const Dependency = require("../Dependency");
const DependencyTemplate = require("../DependencyTemplate");
const makeSerializable = require("../util/makeSerializable");
const memoize = require("../util/memoize");
 
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
/** @typedef {import("../ContextModule").ContextOptions} ContextOptions */
/** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../WebpackError")} WebpackError */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
 
const getCriticalDependencyWarning = memoize(() =>
    require("./CriticalDependencyWarning")
);
 
/** @typedef {ContextOptions & { request: string }} ContextDependencyOptions */
 
/** @typedef {{ value: string, range: Range }[]} Replaces */
 
/**
 * Returns stringified regexp.
 * @param {RegExp | false | null | undefined} r regexp
 * @returns {string} stringified regexp
 */
const regExpToString = (r) => (r ? String(r) : "");
 
class ContextDependency extends Dependency {
    /**
     * Creates an instance of ContextDependency.
     * @param {ContextDependencyOptions} options options for the context module
     * @param {string=} context request context
     */
    constructor(options, context) {
        super();
 
        this.options = options;
        this.userRequest = this.options && this.options.request;
        /** @type {false | undefined | string} */
        this.critical = false;
        this.hadGlobalOrStickyRegExp = false;
 
        if (
            this.options &&
            this.options.regExp &&
            (this.options.regExp.global || this.options.regExp.sticky)
        ) {
            this.options = { ...this.options, regExp: null };
            this.hadGlobalOrStickyRegExp = true;
        }
 
        /** @type {string | undefined} */
        this.request = undefined;
        /** @type {Range | undefined} */
        this.range = undefined;
        /** @type {Range | undefined} */
        this.valueRange = undefined;
        /** @type {boolean | string | undefined} */
        this.inShorthand = undefined;
        /** @type {Replaces | undefined} */
        this.replaces = undefined;
        this._requestContext = context;
    }
 
    /**
     * Returns a request context.
     * @returns {string | undefined} a request context
     */
    getContext() {
        return this._requestContext;
    }
 
    get category() {
        return "commonjs";
    }
 
    /**
     * Could affect referencing module.
     * @returns {boolean | TRANSITIVE} true, when changes to the referenced module could affect the referencing module; TRANSITIVE, when changes to the referenced module could affect referencing modules of the referencing module
     */
    couldAffectReferencingModule() {
        return true;
    }
 
    /**
     * Returns an identifier to merge equal requests.
     * @returns {string | null} an identifier to merge equal requests
     */
    getResourceIdentifier() {
        return (
            `context${this._requestContext || ""}|ctx request${
                this.options.request
            } ${this.options.recursive} ` +
            `${regExpToString(this.options.regExp)} ${regExpToString(
                this.options.include
            )} ${regExpToString(this.options.exclude)} ` +
            `${this.options.mode} ${this.options.chunkName} ` +
            `${JSON.stringify(this.options.groupOptions)}` +
            `${
                this.options.referencedExports
                    ? ` ${JSON.stringify(this.options.referencedExports)}`
                    : ""
            }`
        );
    }
 
    /**
     * Returns warnings.
     * @param {ModuleGraph} moduleGraph module graph
     * @returns {WebpackError[] | null | undefined} warnings
     */
    getWarnings(moduleGraph) {
        let warnings = super.getWarnings(moduleGraph);
 
        if (this.critical) {
            if (!warnings) warnings = [];
            const CriticalDependencyWarning = getCriticalDependencyWarning();
            warnings.push(new CriticalDependencyWarning(this.critical));
        }
 
        if (this.hadGlobalOrStickyRegExp) {
            if (!warnings) warnings = [];
            const CriticalDependencyWarning = getCriticalDependencyWarning();
            warnings.push(
                new CriticalDependencyWarning(
                    "Contexts can't use RegExps with the 'g' or 'y' flags."
                )
            );
        }
 
        return warnings;
    }
 
    /**
     * Serializes this instance into the provided serializer context.
     * @param {ObjectSerializerContext} context context
     */
    serialize(context) {
        const { write } = context;
 
        write(this.options);
        write(this.userRequest);
        write(this.critical);
        write(this.hadGlobalOrStickyRegExp);
        write(this.request);
        write(this._requestContext);
        write(this.range);
        write(this.valueRange);
        write(this.replaces);
 
        super.serialize(context);
    }
 
    /**
     * Restores this instance from the provided deserializer context.
     * @param {ObjectDeserializerContext} context context
     */
    deserialize(context) {
        const { read } = context;
 
        this.options = read();
        this.userRequest = read();
        this.critical = read();
        this.hadGlobalOrStickyRegExp = read();
        this.request = read();
        this._requestContext = read();
        this.range = read();
        this.valueRange = read();
        this.replaces = read();
 
        super.deserialize(context);
    }
}
 
makeSerializable(
    ContextDependency,
    "webpack/lib/dependencies/ContextDependency"
);
 
ContextDependency.Template = DependencyTemplate;
 
module.exports = ContextDependency;