UNPKG

3.8 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const Dependency = require("../Dependency");
9const DependencyTemplate = require("../DependencyTemplate");
10const makeSerializable = require("../util/makeSerializable");
11const memoize = require("../util/memoize");
12
13/** @typedef {import("../ContextModule").ContextOptions} ContextOptions */
14/** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
15/** @typedef {import("../ModuleGraph")} ModuleGraph */
16/** @typedef {import("../WebpackError")} WebpackError */
17
18const getCriticalDependencyWarning = memoize(() =>
19 require("./CriticalDependencyWarning")
20);
21
22/** @typedef {ContextOptions & { request: string }} ContextDependencyOptions */
23
24const regExpToString = r => (r ? r + "" : "");
25
26class ContextDependency extends Dependency {
27 /**
28 * @param {ContextDependencyOptions} options options for the context module
29 */
30 constructor(options) {
31 super();
32
33 this.options = options;
34 this.userRequest = this.options && this.options.request;
35 /** @type {false | string} */
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 // TODO refactor this
52 this.replaces = undefined;
53 }
54
55 get category() {
56 return "commonjs";
57 }
58
59 /**
60 * @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
61 */
62 couldAffectReferencingModule() {
63 return true;
64 }
65
66 /**
67 * @returns {string | null} an identifier to merge equal requests
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 * Returns warnings
82 * @param {ModuleGraph} moduleGraph module graph
83 * @returns {WebpackError[]} warnings
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
140makeSerializable(
141 ContextDependency,
142 "webpack/lib/dependencies/ContextDependency"
143);
144
145ContextDependency.Template = DependencyTemplate;
146
147module.exports = ContextDependency;