ContextDependency.js
4.0 KB
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
/*
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("../ContextModule").ContextOptions} ContextOptions */
/** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../WebpackError")} WebpackError */
const getCriticalDependencyWarning = memoize(() =>
require("./CriticalDependencyWarning")
);
/** @typedef {ContextOptions & { request: string }} ContextDependencyOptions */
const regExpToString = r => (r ? r + "" : "");
class ContextDependency extends Dependency {
/**
* @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 | string} */
this.critical = false;
this.hadGlobalOrStickyRegExp = false;
if (
this.options &&
(this.options.regExp.global || this.options.regExp.sticky)
) {
this.options = { ...this.options, regExp: null };
this.hadGlobalOrStickyRegExp = true;
}
this.request = undefined;
this.range = undefined;
this.valueRange = undefined;
this.inShorthand = undefined;
// TODO refactor this
this.replaces = undefined;
this._requestContext = context;
}
/**
* @returns {string | undefined} a request context
*/
getContext() {
return this._requestContext;
}
get category() {
return "commonjs";
}
/**
* @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 {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)}`
);
}
/**
* Returns warnings
* @param {ModuleGraph} moduleGraph module graph
* @returns {WebpackError[]} 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;
}
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.prepend);
write(this.replaces);
super.serialize(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.prepend = read();
this.replaces = read();
super.deserialize(context);
}
}
makeSerializable(
ContextDependency,
"webpack/lib/dependencies/ContextDependency"
);
ContextDependency.Template = DependencyTemplate;
module.exports = ContextDependency;