webpack/lib/dependencies/CssIcssExportDependency.js

160 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-12-14 23:02:26 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Ivan Kopeykin @vankop
*/
"use strict";
const { cssExportConvention } = require("../util/conventions");
2021-12-14 23:02:26 +08:00
const makeSerializable = require("../util/makeSerializable");
const NullDependency = require("./NullDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../../declarations/WebpackOptions").CssGeneratorExportsConvention} CssGeneratorExportsConvention */
/** @typedef {import("../CssModule")} CssModule */
2021-12-14 23:02:26 +08:00
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../Dependency").ExportsSpec} ExportsSpec */
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
2021-12-14 23:02:26 +08:00
/** @typedef {import("../DependencyTemplate").CssDependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../css/CssGenerator")} CssGenerator */
2023-04-12 03:22:51 +08:00
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
/** @typedef {import("../util/Hash")} Hash */
2021-12-14 23:02:26 +08:00
2024-11-07 00:11:54 +08:00
class CssIcssExportDependency extends NullDependency {
2021-12-14 23:02:26 +08:00
/**
* @param {string} name name
* @param {string} value value
*/
constructor(name, value) {
super();
this.name = name;
this.value = value;
2024-11-08 00:32:00 +08:00
this._hashUpdate = undefined;
2021-12-14 23:02:26 +08:00
}
get type() {
return "css :export";
}
/**
* @param {string} name export name
* @param {CssGeneratorExportsConvention} convention convention of the export name
* @returns {string[]} convention results
*/
getExportsConventionNames(name, convention) {
if (this._conventionNames) {
return this._conventionNames;
}
this._conventionNames = cssExportConvention(name, convention);
return this._conventionNames;
}
2021-12-14 23:02:26 +08:00
/**
* Returns the exported names
* @param {ModuleGraph} moduleGraph module graph
* @returns {ExportsSpec | undefined} export names
*/
getExports(moduleGraph) {
const module = /** @type {CssModule} */ (moduleGraph.getParentModule(this));
2025-03-07 21:12:22 +08:00
const generator = /** @type {CssGenerator} */ (module.generator);
const names = this.getExportsConventionNames(
this.name,
/** @type {CssGeneratorExportsConvention} */
(generator.convention)
);
2021-12-14 23:02:26 +08:00
return {
exports: names.map(name => ({
name,
canMangle: true
})),
2021-12-14 23:02:26 +08:00
dependencies: undefined
};
}
/**
* Update the hash
* @param {Hash} hash hash to be updated
* @param {UpdateHashContext} context context
* @returns {void}
*/
updateHash(hash, { chunkGraph }) {
2024-11-08 00:32:00 +08:00
if (this._hashUpdate === undefined) {
const module =
/** @type {CssModule} */
(chunkGraph.moduleGraph.getParentModule(this));
2025-03-07 21:12:22 +08:00
const generator = /** @type {CssGenerator} */ (module.generator);
2024-11-08 00:32:00 +08:00
const names = this.getExportsConventionNames(
this.name,
2025-03-07 21:12:22 +08:00
/** @type {CssGeneratorExportsConvention} */
(generator.convention)
2024-11-08 00:32:00 +08:00
);
this._hashUpdate = JSON.stringify(names);
}
2024-07-31 12:23:44 +08:00
hash.update("exportsConvention");
2024-11-08 00:32:00 +08:00
hash.update(this._hashUpdate);
}
2023-04-12 03:22:51 +08:00
/**
* @param {ObjectSerializerContext} context context
*/
2021-12-14 23:02:26 +08:00
serialize(context) {
const { write } = context;
write(this.name);
write(this.value);
super.serialize(context);
}
2023-04-12 03:22:51 +08:00
/**
* @param {ObjectDeserializerContext} context context
*/
2021-12-14 23:02:26 +08:00
deserialize(context) {
const { read } = context;
this.name = read();
this.value = read();
super.deserialize(context);
}
}
2024-11-07 00:11:54 +08:00
CssIcssExportDependency.Template = class CssIcssExportDependencyTemplate extends (
2021-12-14 23:02:26 +08:00
NullDependency.Template
) {
/**
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {ReplaceSource} source the current replace source which can be modified
* @param {DependencyTemplateContext} templateContext the context object
* @returns {void}
*/
2024-11-29 04:35:59 +08:00
apply(dependency, source, { cssData, module: m, runtime, moduleGraph }) {
2024-11-07 00:11:54 +08:00
const dep = /** @type {CssIcssExportDependency} */ (dependency);
const module = /** @type {CssModule} */ (m);
2025-03-07 21:12:22 +08:00
const generator = /** @type {CssGenerator} */ (module.generator);
const names = dep.getExportsConventionNames(
dep.name,
/** @type {CssGeneratorExportsConvention} */
(generator.convention)
);
2024-11-20 09:31:41 +08:00
const usedNames =
/** @type {string[]} */
(
names
.map(name =>
moduleGraph.getExportInfo(module, name).getUsedName(name, runtime)
)
.filter(Boolean)
);
2025-07-03 17:06:45 +08:00
for (const used of [...usedNames, ...names]) {
2024-11-29 04:35:59 +08:00
cssData.exports.set(used, dep.value);
}
2021-12-14 23:02:26 +08:00
}
};
makeSerializable(
2024-11-07 00:11:54 +08:00
CssIcssExportDependency,
"webpack/lib/dependencies/CssIcssExportDependency"
2021-12-14 23:02:26 +08:00
);
2024-11-07 00:11:54 +08:00
module.exports = CssIcssExportDependency;