1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const Dependency = require("../Dependency");
|
9 | const DependencyTemplate = require("../DependencyTemplate");
|
10 | const makeSerializable = require("../util/makeSerializable");
|
11 | const memoize = require("../util/memoize");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | const getCriticalDependencyWarning = memoize(() =>
|
19 | require("./CriticalDependencyWarning")
|
20 | );
|
21 |
|
22 |
|
23 |
|
24 | const regExpToString = r => (r ? r + "" : "");
|
25 |
|
26 | class ContextDependency extends Dependency {
|
27 | |
28 |
|
29 |
|
30 | constructor(options) {
|
31 | super();
|
32 |
|
33 | this.options = options;
|
34 | this.userRequest = this.options && this.options.request;
|
35 |
|
36 | this.critical = false;
|
37 | this.hadGlobalOrStickyRegExp = false;
|
38 |
|
39 | if (
|
40 | this.options &&
|
41 | (this.options.regExp.global || this.options.regExp.sticky)
|
42 | ) {
|
43 | this.options = { ...this.options, regExp: null };
|
44 | this.hadGlobalOrStickyRegExp = true;
|
45 | }
|
46 |
|
47 | this.request = undefined;
|
48 | this.range = undefined;
|
49 | this.valueRange = undefined;
|
50 | this.inShorthand = undefined;
|
51 |
|
52 | this.replaces = undefined;
|
53 | }
|
54 |
|
55 | get category() {
|
56 | return "commonjs";
|
57 | }
|
58 |
|
59 | |
60 |
|
61 |
|
62 | couldAffectReferencingModule() {
|
63 | return true;
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
69 | getResourceIdentifier() {
|
70 | return (
|
71 | `context${this.options.request} ${this.options.recursive} ` +
|
72 | `${regExpToString(this.options.regExp)} ${regExpToString(
|
73 | this.options.include
|
74 | )} ${regExpToString(this.options.exclude)} ` +
|
75 | `${this.options.mode} ${this.options.chunkName} ` +
|
76 | `${JSON.stringify(this.options.groupOptions)}`
|
77 | );
|
78 | }
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 | getWarnings(moduleGraph) {
|
86 | let warnings = super.getWarnings(moduleGraph);
|
87 |
|
88 | if (this.critical) {
|
89 | if (!warnings) warnings = [];
|
90 | const CriticalDependencyWarning = getCriticalDependencyWarning();
|
91 | warnings.push(new CriticalDependencyWarning(this.critical));
|
92 | }
|
93 |
|
94 | if (this.hadGlobalOrStickyRegExp) {
|
95 | if (!warnings) warnings = [];
|
96 | const CriticalDependencyWarning = getCriticalDependencyWarning();
|
97 | warnings.push(
|
98 | new CriticalDependencyWarning(
|
99 | "Contexts can't use RegExps with the 'g' or 'y' flags."
|
100 | )
|
101 | );
|
102 | }
|
103 |
|
104 | return warnings;
|
105 | }
|
106 |
|
107 | serialize(context) {
|
108 | const { write } = context;
|
109 |
|
110 | write(this.options);
|
111 | write(this.userRequest);
|
112 | write(this.critical);
|
113 | write(this.hadGlobalOrStickyRegExp);
|
114 | write(this.request);
|
115 | write(this.range);
|
116 | write(this.valueRange);
|
117 | write(this.prepend);
|
118 | write(this.replaces);
|
119 |
|
120 | super.serialize(context);
|
121 | }
|
122 |
|
123 | deserialize(context) {
|
124 | const { read } = context;
|
125 |
|
126 | this.options = read();
|
127 | this.userRequest = read();
|
128 | this.critical = read();
|
129 | this.hadGlobalOrStickyRegExp = read();
|
130 | this.request = read();
|
131 | this.range = read();
|
132 | this.valueRange = read();
|
133 | this.prepend = read();
|
134 | this.replaces = read();
|
135 |
|
136 | super.deserialize(context);
|
137 | }
|
138 | }
|
139 |
|
140 | makeSerializable(
|
141 | ContextDependency,
|
142 | "webpack/lib/dependencies/ContextDependency"
|
143 | );
|
144 |
|
145 | ContextDependency.Template = DependencyTemplate;
|
146 |
|
147 | module.exports = ContextDependency;
|