2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-01-08 12:03:25 +08:00
|
|
|
"use strict";
|
2018-07-23 23:33:29 +08:00
|
|
|
|
2017-01-08 12:03:25 +08:00
|
|
|
const Dependency = require("../Dependency");
|
2018-07-23 23:33:29 +08:00
|
|
|
const DependencyTemplate = require("../DependencyTemplate");
|
2018-10-11 18:47:50 +08:00
|
|
|
const makeSerializable = require("../util/makeSerializable");
|
2020-12-27 05:32:57 +08:00
|
|
|
const memoize = require("../util/memoize");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2025-09-09 23:41:52 +08:00
|
|
|
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
2019-06-14 17:44:54 +08:00
|
|
|
/** @typedef {import("../ContextModule").ContextOptions} ContextOptions */
|
2021-09-27 22:43:34 +08:00
|
|
|
/** @typedef {import("../Dependency").TRANSITIVE} TRANSITIVE */
|
2018-07-24 23:35:36 +08:00
|
|
|
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
2018-07-25 15:33:48 +08:00
|
|
|
/** @typedef {import("../WebpackError")} WebpackError */
|
2023-04-12 03:22:51 +08:00
|
|
|
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|
|
|
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
2018-07-25 15:33:48 +08:00
|
|
|
|
2020-12-27 05:32:57 +08:00
|
|
|
const getCriticalDependencyWarning = memoize(() =>
|
2020-07-06 23:07:51 +08:00
|
|
|
require("./CriticalDependencyWarning")
|
|
|
|
);
|
|
|
|
|
2019-06-14 17:44:54 +08:00
|
|
|
/** @typedef {ContextOptions & { request: string }} ContextDependencyOptions */
|
|
|
|
|
2025-09-09 23:41:52 +08:00
|
|
|
/** @typedef {{ value: string, range: Range }[]} Replaces */
|
|
|
|
|
2023-06-17 02:24:34 +08:00
|
|
|
/**
|
2025-08-22 00:14:29 +08:00
|
|
|
* @param {RegExp | false | null | undefined} r regexp
|
2023-06-17 02:24:34 +08:00
|
|
|
* @returns {string} stringified regexp
|
|
|
|
*/
|
2025-07-17 00:13:14 +08:00
|
|
|
const regExpToString = (r) => (r ? String(r) : "");
|
2017-11-19 07:22:38 +08:00
|
|
|
|
2017-01-08 12:03:25 +08:00
|
|
|
class ContextDependency extends Dependency {
|
2019-06-14 17:44:54 +08:00
|
|
|
/**
|
|
|
|
* @param {ContextDependencyOptions} options options for the context module
|
2022-03-28 23:51:48 +08:00
|
|
|
* @param {string=} context request context
|
2019-06-14 17:44:54 +08:00
|
|
|
*/
|
2022-03-28 23:51:48 +08:00
|
|
|
constructor(options, context) {
|
2017-01-08 12:03:25 +08:00
|
|
|
super();
|
2018-10-11 18:47:50 +08:00
|
|
|
|
2017-10-16 19:19:53 +08:00
|
|
|
this.options = options;
|
2018-10-18 05:10:14 +08:00
|
|
|
this.userRequest = this.options && this.options.request;
|
2023-06-17 06:33:17 +08:00
|
|
|
/** @type {false | undefined | string} */
|
2018-04-11 02:58:32 +08:00
|
|
|
this.critical = false;
|
2017-10-03 17:26:05 +08:00
|
|
|
this.hadGlobalOrStickyRegExp = false;
|
2018-10-11 18:47:50 +08:00
|
|
|
|
2018-10-18 05:10:14 +08:00
|
|
|
if (
|
|
|
|
this.options &&
|
2025-08-22 00:14:29 +08:00
|
|
|
this.options.regExp &&
|
2018-10-18 05:10:14 +08:00
|
|
|
(this.options.regExp.global || this.options.regExp.sticky)
|
|
|
|
) {
|
2019-06-19 19:16:05 +08:00
|
|
|
this.options = { ...this.options, regExp: null };
|
2017-10-03 17:26:05 +08:00
|
|
|
this.hadGlobalOrStickyRegExp = true;
|
|
|
|
}
|
2018-10-11 18:47:50 +08:00
|
|
|
|
2025-09-09 23:41:52 +08:00
|
|
|
/** @type {string | undefined} */
|
2018-07-23 23:33:29 +08:00
|
|
|
this.request = undefined;
|
2025-09-09 23:41:52 +08:00
|
|
|
/** @type {Range | undefined} */
|
2018-07-23 23:33:29 +08:00
|
|
|
this.range = undefined;
|
2025-09-09 23:41:52 +08:00
|
|
|
/** @type {Range | undefined} */
|
2018-07-23 23:33:29 +08:00
|
|
|
this.valueRange = undefined;
|
2023-06-17 06:33:17 +08:00
|
|
|
/** @type {boolean | string | undefined} */
|
2021-10-21 16:02:07 +08:00
|
|
|
this.inShorthand = undefined;
|
2025-09-09 23:41:52 +08:00
|
|
|
/** @type {Replaces | undefined} */
|
2018-07-23 23:33:29 +08:00
|
|
|
this.replaces = undefined;
|
2022-03-28 23:51:48 +08:00
|
|
|
this._requestContext = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string | undefined} a request context
|
|
|
|
*/
|
|
|
|
getContext() {
|
|
|
|
return this._requestContext;
|
2017-01-08 12:03:25 +08:00
|
|
|
}
|
|
|
|
|
2020-05-31 17:48:45 +08:00
|
|
|
get category() {
|
|
|
|
return "commonjs";
|
|
|
|
}
|
|
|
|
|
2021-09-27 22:43:34 +08:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
couldAffectReferencingModule() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:33:48 +08:00
|
|
|
/**
|
|
|
|
* @returns {string | null} an identifier to merge equal requests
|
|
|
|
*/
|
2017-11-19 07:22:38 +08:00
|
|
|
getResourceIdentifier() {
|
2018-02-25 09:00:20 +08:00
|
|
|
return (
|
2022-03-28 23:51:48 +08:00
|
|
|
`context${this._requestContext || ""}|ctx request${
|
|
|
|
this.options.request
|
|
|
|
} ${this.options.recursive} ` +
|
2018-02-25 09:00:20 +08:00
|
|
|
`${regExpToString(this.options.regExp)} ${regExpToString(
|
|
|
|
this.options.include
|
|
|
|
)} ${regExpToString(this.options.exclude)} ` +
|
2018-04-16 16:27:22 +08:00
|
|
|
`${this.options.mode} ${this.options.chunkName} ` +
|
2024-09-11 03:52:10 +08:00
|
|
|
`${JSON.stringify(this.options.groupOptions)}` +
|
|
|
|
`${
|
|
|
|
this.options.referencedExports
|
|
|
|
? ` ${JSON.stringify(this.options.referencedExports)}`
|
|
|
|
: ""
|
|
|
|
}`
|
2018-02-25 09:00:20 +08:00
|
|
|
);
|
2017-01-08 12:03:25 +08:00
|
|
|
}
|
2017-10-03 17:26:05 +08:00
|
|
|
|
2018-07-25 15:33:48 +08:00
|
|
|
/**
|
|
|
|
* Returns warnings
|
2018-07-24 23:35:36 +08:00
|
|
|
* @param {ModuleGraph} moduleGraph module graph
|
2023-06-17 02:24:34 +08:00
|
|
|
* @returns {WebpackError[] | null | undefined} warnings
|
2018-07-25 15:33:48 +08:00
|
|
|
*/
|
2018-07-24 23:35:36 +08:00
|
|
|
getWarnings(moduleGraph) {
|
2019-08-07 18:00:50 +08:00
|
|
|
let warnings = super.getWarnings(moduleGraph);
|
2018-10-11 18:47:50 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (this.critical) {
|
2019-08-07 18:00:50 +08:00
|
|
|
if (!warnings) warnings = [];
|
2020-07-06 23:07:51 +08:00
|
|
|
const CriticalDependencyWarning = getCriticalDependencyWarning();
|
2017-10-03 17:26:05 +08:00
|
|
|
warnings.push(new CriticalDependencyWarning(this.critical));
|
|
|
|
}
|
2018-10-11 18:47:50 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (this.hadGlobalOrStickyRegExp) {
|
2019-08-07 18:00:50 +08:00
|
|
|
if (!warnings) warnings = [];
|
2020-07-06 23:07:51 +08:00
|
|
|
const CriticalDependencyWarning = getCriticalDependencyWarning();
|
2018-02-25 09:00:20 +08:00
|
|
|
warnings.push(
|
|
|
|
new CriticalDependencyWarning(
|
|
|
|
"Contexts can't use RegExps with the 'g' or 'y' flags."
|
|
|
|
)
|
|
|
|
);
|
2017-10-03 17:26:05 +08:00
|
|
|
}
|
2018-10-11 18:47:50 +08:00
|
|
|
|
2017-10-03 17:26:05 +08:00
|
|
|
return warnings;
|
|
|
|
}
|
2018-10-11 18:47:50 +08:00
|
|
|
|
2023-04-12 03:22:51 +08:00
|
|
|
/**
|
|
|
|
* @param {ObjectSerializerContext} context context
|
|
|
|
*/
|
2018-10-11 18:47:50 +08:00
|
|
|
serialize(context) {
|
|
|
|
const { write } = context;
|
|
|
|
|
|
|
|
write(this.options);
|
|
|
|
write(this.userRequest);
|
|
|
|
write(this.critical);
|
|
|
|
write(this.hadGlobalOrStickyRegExp);
|
|
|
|
write(this.request);
|
2022-03-28 23:51:48 +08:00
|
|
|
write(this._requestContext);
|
2018-10-11 18:47:50 +08:00
|
|
|
write(this.range);
|
|
|
|
write(this.valueRange);
|
|
|
|
write(this.replaces);
|
|
|
|
|
|
|
|
super.serialize(context);
|
|
|
|
}
|
|
|
|
|
2023-04-12 03:22:51 +08:00
|
|
|
/**
|
|
|
|
* @param {ObjectDeserializerContext} context context
|
|
|
|
*/
|
2018-10-11 18:47:50 +08:00
|
|
|
deserialize(context) {
|
|
|
|
const { read } = context;
|
|
|
|
|
|
|
|
this.options = read();
|
|
|
|
this.userRequest = read();
|
|
|
|
this.critical = read();
|
|
|
|
this.hadGlobalOrStickyRegExp = read();
|
|
|
|
this.request = read();
|
2022-03-28 23:51:48 +08:00
|
|
|
this._requestContext = read();
|
2018-10-11 18:47:50 +08:00
|
|
|
this.range = read();
|
|
|
|
this.valueRange = read();
|
|
|
|
this.replaces = read();
|
|
|
|
|
|
|
|
super.deserialize(context);
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:47:50 +08:00
|
|
|
makeSerializable(
|
|
|
|
ContextDependency,
|
|
|
|
"webpack/lib/dependencies/ContextDependency"
|
|
|
|
);
|
|
|
|
|
2018-07-23 23:33:29 +08:00
|
|
|
ContextDependency.Template = DependencyTemplate;
|
|
|
|
|
2017-01-08 12:03:25 +08:00
|
|
|
module.exports = ContextDependency;
|