webpack/lib/dependencies/ContextElementDependency.js

147 lines
3.9 KiB
JavaScript
Raw Normal View History

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
"use strict";
2018-07-30 23:08:51 +08:00
2020-06-10 19:31:01 +08:00
const Dependency = require("../Dependency");
const makeSerializable = require("../util/makeSerializable");
const ModuleDependency = require("./ModuleDependency");
2013-01-31 01:49:25 +08:00
2024-09-04 15:12:06 +08:00
/** @typedef {import("../ContextModule")} ContextModule */
2025-09-11 08:10:10 +08:00
/** @typedef {import("../Dependency").RawReferencedExports} RawReferencedExports */
/** @typedef {import("../Dependency").ReferencedExports} ReferencedExports */
/** @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 */
/** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
2020-06-10 19:31:01 +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
2025-09-11 08:10:10 +08:00
* @param {RawReferencedExports | 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
) {
super(request);
2018-02-25 09:00:20 +08:00
if (userRequest) {
this.userRequest = userRequest;
}
2024-03-16 00:59:30 +08:00
this._typePrefix = typePrefix;
this._category = category;
this.referencedExports = referencedExports;
this._context = context || undefined;
this.attributes = attributes;
}
get type() {
if (this._typePrefix) {
return `${this._typePrefix} context element`;
}
return "context element";
}
2020-06-10 19:31:01 +08:00
get category() {
return this._category;
}
/**
* @returns {string | null} an identifier to merge equal requests
*/
getResourceIdentifier() {
let str = super.getResourceIdentifier();
if (this.attributes) {
str += `|importAttributes${JSON.stringify(this.attributes)}`;
}
return str;
}
2020-06-10 19:31:01 +08:00
/**
* Returns list of exports referenced by this dependency
* @param {ModuleGraph} moduleGraph module graph
* @param {RuntimeSpec} runtime the runtime for which the module is analysed
2025-09-11 08:10:10 +08:00
* @returns {ReferencedExports} referenced exports
2020-06-10 19:31:01 +08:00
*/
getReferencedExports(moduleGraph, runtime) {
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) {
const { write } = context;
write(this._typePrefix);
write(this._category);
write(this.referencedExports);
write(this.attributes);
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) {
const { read } = context;
this._typePrefix = read();
this._category = read();
this.referencedExports = read();
this.attributes = read();
2020-06-10 19:31:01 +08:00
super.deserialize(context);
}
2013-01-31 01:49:25 +08:00
}
makeSerializable(
ContextElementDependency,
"webpack/lib/dependencies/ContextElementDependency"
);
module.exports = ContextElementDependency;