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-06 01:58:10 +08:00
|
|
|
"use strict";
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2020-06-10 19:31:01 +08:00
|
|
|
const Dependency = require("../Dependency");
|
2018-10-11 18:56:26 +08:00
|
|
|
const makeSerializable = require("../util/makeSerializable");
|
2017-01-06 01:58:10 +08:00
|
|
|
const ModuleDependency = require("./ModuleDependency");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2024-09-04 15:12:06 +08:00
|
|
|
/** @typedef {import("../ContextModule")} ContextModule */
|
2020-06-10 19:31:01 +08:00
|
|
|
/** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
2024-09-04 15:03:38 +08:00
|
|
|
/** @typedef {import("../Module")} Module */
|
2024-09-04 15:12:06 +08:00
|
|
|
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
2024-06-11 00:21:03 +08:00
|
|
|
/** @typedef {import("../javascript/JavascriptParser").ImportAttributes} ImportAttributes */
|
2023-04-12 03:22:51 +08:00
|
|
|
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|
|
|
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
2020-07-28 00:09:48 +08:00
|
|
|
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
2020-06-10 19:31:01 +08:00
|
|
|
|
2017-01-06 01:58:10 +08:00
|
|
|
class ContextElementDependency extends ModuleDependency {
|
2022-03-25 21:28:18 +08:00
|
|
|
/**
|
|
|
|
* @param {string} request request
|
2025-04-26 01:43:01 +08:00
|
|
|
* @param {string | undefined} userRequest user request
|
2024-08-07 23:22:25 +08:00
|
|
|
* @param {string | undefined} typePrefix type prefix
|
2022-03-25 21:28:18 +08:00
|
|
|
* @param {string} category category
|
2024-08-07 23:22:25 +08:00
|
|
|
* @param {(string[][] | null)=} referencedExports referenced exports
|
2022-03-25 21:28:18 +08:00
|
|
|
* @param {string=} context context
|
2024-06-11 00:21:03 +08:00
|
|
|
* @param {ImportAttributes=} attributes import assertions
|
2022-03-25 21:28:18 +08:00
|
|
|
*/
|
|
|
|
constructor(
|
|
|
|
request,
|
|
|
|
userRequest,
|
|
|
|
typePrefix,
|
|
|
|
category,
|
|
|
|
referencedExports,
|
2024-03-16 00:59:30 +08:00
|
|
|
context,
|
|
|
|
attributes
|
2022-03-25 21:28:18 +08:00
|
|
|
) {
|
2017-01-06 01:58:10 +08:00
|
|
|
super(request);
|
2020-06-10 19:31:01 +08:00
|
|
|
this.referencedExports = referencedExports;
|
2021-05-31 19:44:09 +08:00
|
|
|
this._typePrefix = typePrefix;
|
2020-06-18 05:03:02 +08:00
|
|
|
this._category = category;
|
2022-03-25 21:28:18 +08:00
|
|
|
this._context = context || undefined;
|
2018-10-11 18:56:26 +08:00
|
|
|
|
2018-02-25 09:00:20 +08:00
|
|
|
if (userRequest) {
|
2017-01-06 01:58:10 +08:00
|
|
|
this.userRequest = userRequest;
|
|
|
|
}
|
2024-03-16 00:59:30 +08:00
|
|
|
|
|
|
|
this.assertions = attributes;
|
2017-01-06 01:58:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
get type() {
|
2021-05-31 19:44:09 +08:00
|
|
|
if (this._typePrefix) {
|
|
|
|
return `${this._typePrefix} context element`;
|
|
|
|
}
|
|
|
|
|
2017-01-06 01:58:10 +08:00
|
|
|
return "context element";
|
2016-01-07 02:56:17 +08:00
|
|
|
}
|
2020-06-10 19:31:01 +08:00
|
|
|
|
2020-06-18 05:03:02 +08:00
|
|
|
get category() {
|
|
|
|
return this._category;
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:31:01 +08:00
|
|
|
/**
|
|
|
|
* Returns list of exports referenced by this dependency
|
|
|
|
* @param {ModuleGraph} moduleGraph module graph
|
2020-07-28 00:09:48 +08:00
|
|
|
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
2020-06-10 19:31:01 +08:00
|
|
|
* @returns {(string[] | ReferencedExport)[]} referenced exports
|
|
|
|
*/
|
2020-07-28 00:09:48 +08:00
|
|
|
getReferencedExports(moduleGraph, runtime) {
|
2024-09-04 15:03:38 +08:00
|
|
|
if (!this.referencedExports) return Dependency.EXPORTS_OBJECT_REFERENCED;
|
|
|
|
const refs = [];
|
|
|
|
for (const referencedExport of this.referencedExports) {
|
|
|
|
if (
|
|
|
|
this._typePrefix === "import()" &&
|
|
|
|
referencedExport[0] === "default"
|
|
|
|
) {
|
|
|
|
const selfModule =
|
|
|
|
/** @type {ContextModule} */
|
|
|
|
(moduleGraph.getParentModule(this));
|
|
|
|
const importedModule =
|
|
|
|
/** @type {Module} */
|
|
|
|
(moduleGraph.getModule(this));
|
|
|
|
const exportsType = importedModule.getExportsType(
|
|
|
|
moduleGraph,
|
|
|
|
selfModule.options.namespaceObject === "strict"
|
|
|
|
);
|
|
|
|
if (
|
|
|
|
exportsType === "default-only" ||
|
|
|
|
exportsType === "default-with-named"
|
|
|
|
) {
|
|
|
|
return Dependency.EXPORTS_OBJECT_REFERENCED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
refs.push({
|
|
|
|
name: referencedExport,
|
|
|
|
canMangle: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return refs;
|
2020-06-10 19:31:01 +08:00
|
|
|
}
|
|
|
|
|
2023-04-12 03:22:51 +08:00
|
|
|
/**
|
|
|
|
* @param {ObjectSerializerContext} context context
|
|
|
|
*/
|
2020-06-10 19:31:01 +08:00
|
|
|
serialize(context) {
|
2022-02-09 20:32:10 +08:00
|
|
|
const { write } = context;
|
|
|
|
write(this._typePrefix);
|
|
|
|
write(this._category);
|
|
|
|
write(this.referencedExports);
|
2024-03-16 00:59:30 +08:00
|
|
|
write(this.assertions);
|
2020-06-10 19:31:01 +08:00
|
|
|
super.serialize(context);
|
|
|
|
}
|
|
|
|
|
2023-04-12 03:22:51 +08:00
|
|
|
/**
|
|
|
|
* @param {ObjectDeserializerContext} context context
|
|
|
|
*/
|
2020-06-10 19:31:01 +08:00
|
|
|
deserialize(context) {
|
2022-02-09 20:32:10 +08:00
|
|
|
const { read } = context;
|
|
|
|
this._typePrefix = read();
|
|
|
|
this._category = read();
|
|
|
|
this.referencedExports = read();
|
2024-03-16 00:59:30 +08:00
|
|
|
this.assertions = read();
|
2020-06-10 19:31:01 +08:00
|
|
|
super.deserialize(context);
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:56:26 +08:00
|
|
|
makeSerializable(
|
|
|
|
ContextElementDependency,
|
|
|
|
"webpack/lib/dependencies/ContextElementDependency"
|
|
|
|
);
|
|
|
|
|
2017-01-06 01:58:10 +08:00
|
|
|
module.exports = ContextElementDependency;
|