| 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("../javascript/JavascriptParser").Range} Range */
|
|---|
| 14 | /** @typedef {import("../ContextModule").ContextOptions} ContextOptions */
|
|---|
| 15 | /** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
|
|---|
| 16 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 17 | /** @typedef {import("../errors/WebpackError")} WebpackError */
|
|---|
| 18 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 19 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 20 |
|
|---|
| 21 | const getCriticalDependencyWarning = memoize(() =>
|
|---|
| 22 | require("./CriticalDependencyWarning")
|
|---|
| 23 | );
|
|---|
| 24 |
|
|---|
| 25 | /** @typedef {ContextOptions & { request: string }} ContextDependencyOptions */
|
|---|
| 26 |
|
|---|
| 27 | /** @typedef {{ value: string, range: Range }[]} Replaces */
|
|---|
| 28 |
|
|---|
| 29 | /**
|
|---|
| 30 | * Returns stringified regexp.
|
|---|
| 31 | * @param {RegExp | false | null | undefined} r regexp
|
|---|
| 32 | * @returns {string} stringified regexp
|
|---|
| 33 | */
|
|---|
| 34 | const regExpToString = (r) => (r ? String(r) : "");
|
|---|
| 35 |
|
|---|
| 36 | class ContextDependency extends Dependency {
|
|---|
| 37 | /**
|
|---|
| 38 | * Creates an instance of ContextDependency.
|
|---|
| 39 | * @param {ContextDependencyOptions} options options for the context module
|
|---|
| 40 | * @param {string=} context request context
|
|---|
| 41 | */
|
|---|
| 42 | constructor(options, context) {
|
|---|
| 43 | super();
|
|---|
| 44 |
|
|---|
| 45 | this.options = options;
|
|---|
| 46 | this.userRequest = this.options && this.options.request;
|
|---|
| 47 | /** @type {false | undefined | string} */
|
|---|
| 48 | this.critical = false;
|
|---|
| 49 | this.hadGlobalOrStickyRegExp = false;
|
|---|
| 50 |
|
|---|
| 51 | if (
|
|---|
| 52 | this.options &&
|
|---|
| 53 | this.options.regExp &&
|
|---|
| 54 | (this.options.regExp.global || this.options.regExp.sticky)
|
|---|
| 55 | ) {
|
|---|
| 56 | this.options = { ...this.options, regExp: null };
|
|---|
| 57 | this.hadGlobalOrStickyRegExp = true;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /** @type {string | undefined} */
|
|---|
| 61 | this.request = undefined;
|
|---|
| 62 | /** @type {Range | undefined} */
|
|---|
| 63 | this.range = undefined;
|
|---|
| 64 | /** @type {Range | undefined} */
|
|---|
| 65 | this.valueRange = undefined;
|
|---|
| 66 | /** @type {boolean | string | undefined} */
|
|---|
| 67 | this.inShorthand = undefined;
|
|---|
| 68 | /** @type {Replaces | undefined} */
|
|---|
| 69 | this.replaces = undefined;
|
|---|
| 70 | this._requestContext = context;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Returns a request context.
|
|---|
| 75 | * @returns {string | undefined} a request context
|
|---|
| 76 | */
|
|---|
| 77 | getContext() {
|
|---|
| 78 | return this._requestContext;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | get category() {
|
|---|
| 82 | return "commonjs";
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | /**
|
|---|
| 86 | * Could affect referencing module.
|
|---|
| 87 | * @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
|
|---|
| 88 | */
|
|---|
| 89 | couldAffectReferencingModule() {
|
|---|
| 90 | return true;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Returns an identifier to merge equal requests.
|
|---|
| 95 | * @returns {string | null} an identifier to merge equal requests
|
|---|
| 96 | */
|
|---|
| 97 | getResourceIdentifier() {
|
|---|
| 98 | return (
|
|---|
| 99 | `context${this._requestContext || ""}|ctx request${
|
|---|
| 100 | this.options.request
|
|---|
| 101 | } ${this.options.recursive} ` +
|
|---|
| 102 | `${regExpToString(this.options.regExp)} ${regExpToString(
|
|---|
| 103 | this.options.include
|
|---|
| 104 | )} ${regExpToString(this.options.exclude)} ` +
|
|---|
| 105 | `${this.options.mode} ${this.options.chunkName} ` +
|
|---|
| 106 | `${JSON.stringify(this.options.groupOptions)}` +
|
|---|
| 107 | `${
|
|---|
| 108 | this.options.referencedExports
|
|---|
| 109 | ? ` ${JSON.stringify(this.options.referencedExports)}`
|
|---|
| 110 | : ""
|
|---|
| 111 | }`
|
|---|
| 112 | );
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /**
|
|---|
| 116 | * Returns warnings.
|
|---|
| 117 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 118 | * @returns {WebpackError[] | null | undefined} warnings
|
|---|
| 119 | */
|
|---|
| 120 | getWarnings(moduleGraph) {
|
|---|
| 121 | let warnings = super.getWarnings(moduleGraph);
|
|---|
| 122 |
|
|---|
| 123 | if (this.critical) {
|
|---|
| 124 | if (!warnings) warnings = [];
|
|---|
| 125 | const CriticalDependencyWarning = getCriticalDependencyWarning();
|
|---|
| 126 | warnings.push(new CriticalDependencyWarning(this.critical));
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | if (this.hadGlobalOrStickyRegExp) {
|
|---|
| 130 | if (!warnings) warnings = [];
|
|---|
| 131 | const CriticalDependencyWarning = getCriticalDependencyWarning();
|
|---|
| 132 | warnings.push(
|
|---|
| 133 | new CriticalDependencyWarning(
|
|---|
| 134 | "Contexts can't use RegExps with the 'g' or 'y' flags."
|
|---|
| 135 | )
|
|---|
| 136 | );
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | return warnings;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /**
|
|---|
| 143 | * Serializes this instance into the provided serializer context.
|
|---|
| 144 | * @param {ObjectSerializerContext} context context
|
|---|
| 145 | */
|
|---|
| 146 | serialize(context) {
|
|---|
| 147 | const { write } = context;
|
|---|
| 148 |
|
|---|
| 149 | write(this.options);
|
|---|
| 150 | write(this.userRequest);
|
|---|
| 151 | write(this.critical);
|
|---|
| 152 | write(this.hadGlobalOrStickyRegExp);
|
|---|
| 153 | write(this.request);
|
|---|
| 154 | write(this._requestContext);
|
|---|
| 155 | write(this.range);
|
|---|
| 156 | write(this.valueRange);
|
|---|
| 157 | write(this.replaces);
|
|---|
| 158 |
|
|---|
| 159 | super.serialize(context);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | /**
|
|---|
| 163 | * Restores this instance from the provided deserializer context.
|
|---|
| 164 | * @param {ObjectDeserializerContext} context context
|
|---|
| 165 | */
|
|---|
| 166 | deserialize(context) {
|
|---|
| 167 | const { read } = context;
|
|---|
| 168 |
|
|---|
| 169 | this.options = read();
|
|---|
| 170 | this.userRequest = read();
|
|---|
| 171 | this.critical = read();
|
|---|
| 172 | this.hadGlobalOrStickyRegExp = read();
|
|---|
| 173 | this.request = read();
|
|---|
| 174 | this._requestContext = read();
|
|---|
| 175 | this.range = read();
|
|---|
| 176 | this.valueRange = read();
|
|---|
| 177 | this.replaces = read();
|
|---|
| 178 |
|
|---|
| 179 | super.deserialize(context);
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | makeSerializable(
|
|---|
| 184 | ContextDependency,
|
|---|
| 185 | "webpack/lib/dependencies/ContextDependency"
|
|---|
| 186 | );
|
|---|
| 187 |
|
|---|
| 188 | ContextDependency.Template = DependencyTemplate;
|
|---|
| 189 |
|
|---|
| 190 | module.exports = ContextDependency;
|
|---|