1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
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 | /** @typedef {import("../ContextModule").ContextOptions} ContextOptions */
|
---|
14 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
15 | /** @typedef {import("../WebpackError")} WebpackError */
|
---|
16 |
|
---|
17 | const getCriticalDependencyWarning = memoize(() =>
|
---|
18 | require("./CriticalDependencyWarning")
|
---|
19 | );
|
---|
20 |
|
---|
21 | /** @typedef {ContextOptions & { request: string }} ContextDependencyOptions */
|
---|
22 |
|
---|
23 | const regExpToString = r => (r ? r + "" : "");
|
---|
24 |
|
---|
25 | class ContextDependency extends Dependency {
|
---|
26 | /**
|
---|
27 | * @param {ContextDependencyOptions} options options for the context module
|
---|
28 | */
|
---|
29 | constructor(options) {
|
---|
30 | super();
|
---|
31 |
|
---|
32 | this.options = options;
|
---|
33 | this.userRequest = this.options && this.options.request;
|
---|
34 | /** @type {false | string} */
|
---|
35 | this.critical = false;
|
---|
36 | this.hadGlobalOrStickyRegExp = false;
|
---|
37 |
|
---|
38 | if (
|
---|
39 | this.options &&
|
---|
40 | (this.options.regExp.global || this.options.regExp.sticky)
|
---|
41 | ) {
|
---|
42 | this.options = { ...this.options, regExp: null };
|
---|
43 | this.hadGlobalOrStickyRegExp = true;
|
---|
44 | }
|
---|
45 |
|
---|
46 | this.request = undefined;
|
---|
47 | this.range = undefined;
|
---|
48 | this.valueRange = undefined;
|
---|
49 | // TODO refactor this
|
---|
50 | this.replaces = undefined;
|
---|
51 | }
|
---|
52 |
|
---|
53 | get category() {
|
---|
54 | return "commonjs";
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * @returns {string | null} an identifier to merge equal requests
|
---|
59 | */
|
---|
60 | getResourceIdentifier() {
|
---|
61 | return (
|
---|
62 | `context${this.options.request} ${this.options.recursive} ` +
|
---|
63 | `${regExpToString(this.options.regExp)} ${regExpToString(
|
---|
64 | this.options.include
|
---|
65 | )} ${regExpToString(this.options.exclude)} ` +
|
---|
66 | `${this.options.mode} ${this.options.chunkName} ` +
|
---|
67 | `${JSON.stringify(this.options.groupOptions)}`
|
---|
68 | );
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Returns warnings
|
---|
73 | * @param {ModuleGraph} moduleGraph module graph
|
---|
74 | * @returns {WebpackError[]} warnings
|
---|
75 | */
|
---|
76 | getWarnings(moduleGraph) {
|
---|
77 | let warnings = super.getWarnings(moduleGraph);
|
---|
78 |
|
---|
79 | if (this.critical) {
|
---|
80 | if (!warnings) warnings = [];
|
---|
81 | const CriticalDependencyWarning = getCriticalDependencyWarning();
|
---|
82 | warnings.push(new CriticalDependencyWarning(this.critical));
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (this.hadGlobalOrStickyRegExp) {
|
---|
86 | if (!warnings) warnings = [];
|
---|
87 | const CriticalDependencyWarning = getCriticalDependencyWarning();
|
---|
88 | warnings.push(
|
---|
89 | new CriticalDependencyWarning(
|
---|
90 | "Contexts can't use RegExps with the 'g' or 'y' flags."
|
---|
91 | )
|
---|
92 | );
|
---|
93 | }
|
---|
94 |
|
---|
95 | return warnings;
|
---|
96 | }
|
---|
97 |
|
---|
98 | serialize(context) {
|
---|
99 | const { write } = context;
|
---|
100 |
|
---|
101 | write(this.options);
|
---|
102 | write(this.userRequest);
|
---|
103 | write(this.critical);
|
---|
104 | write(this.hadGlobalOrStickyRegExp);
|
---|
105 | write(this.request);
|
---|
106 | write(this.range);
|
---|
107 | write(this.valueRange);
|
---|
108 | write(this.prepend);
|
---|
109 | write(this.replaces);
|
---|
110 |
|
---|
111 | super.serialize(context);
|
---|
112 | }
|
---|
113 |
|
---|
114 | deserialize(context) {
|
---|
115 | const { read } = context;
|
---|
116 |
|
---|
117 | this.options = read();
|
---|
118 | this.userRequest = read();
|
---|
119 | this.critical = read();
|
---|
120 | this.hadGlobalOrStickyRegExp = read();
|
---|
121 | this.request = read();
|
---|
122 | this.range = read();
|
---|
123 | this.valueRange = read();
|
---|
124 | this.prepend = read();
|
---|
125 | this.replaces = read();
|
---|
126 |
|
---|
127 | super.deserialize(context);
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | makeSerializable(
|
---|
132 | ContextDependency,
|
---|
133 | "webpack/lib/dependencies/ContextDependency"
|
---|
134 | );
|
---|
135 |
|
---|
136 | ContextDependency.Template = DependencyTemplate;
|
---|
137 |
|
---|
138 | module.exports = ContextDependency;
|
---|