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