webpack/lib/CssModule.js

170 lines
4.2 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Alexander Krasnoyarov @alexander-akait
*/
"use strict";
const NormalModule = require("./NormalModule");
2023-04-25 21:13:39 +08:00
const makeSerializable = require("./util/makeSerializable");
2023-04-25 20:49:16 +08:00
/** @typedef {import("./Module")} Module */
2023-04-25 21:59:17 +08:00
/** @typedef {import("./NormalModule").NormalModuleCreateData} NormalModuleCreateData */
/** @typedef {import("./RequestShortener")} RequestShortener */
2023-04-26 00:37:59 +08:00
/** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
2023-04-25 20:49:16 +08:00
2023-05-02 05:33:09 +08:00
/** @typedef {string|undefined} CssLayer */
/** @typedef {string|undefined} Supports */
/** @typedef {string|undefined} Media */
/** @typedef {[CssLayer?, Supports?, Media?]} InheritanceItem */
/** @typedef {Array<InheritanceItem>} Inheritance */
/** @typedef {NormalModuleCreateData & { cssLayer: CssLayer|null, supports: Supports|null, media: Media|null, inheritance: Inheritance|null }} CSSModuleCreateData */
2023-04-26 06:19:06 +08:00
class CssModule extends NormalModule {
2023-04-25 21:59:17 +08:00
/**
2023-04-26 06:19:06 +08:00
* @param {CSSModuleCreateData} options options object
2023-04-25 21:59:17 +08:00
*/
constructor(options) {
super(options);
2023-04-25 20:49:16 +08:00
// Avoid override `layer` for `Module` class, because it is a feature to run module in specific layer
2023-04-26 00:22:30 +08:00
this.cssLayer = options.cssLayer;
2023-04-25 21:59:17 +08:00
this.supports = options.supports;
this.media = options.media;
2023-05-01 22:46:47 +08:00
this.inheritance = options.inheritance;
2023-04-25 20:49:16 +08:00
}
/**
* @returns {string} a unique identifier of the module
*/
identifier() {
let identifier = super.identifier();
2023-04-26 00:22:30 +08:00
if (this.cssLayer) {
identifier += `|${this.cssLayer}`;
2023-04-25 20:49:16 +08:00
}
if (this.supports) {
identifier += `|${this.supports}`;
}
if (this.media) {
identifier += `|${this.media}`;
}
2023-05-01 22:46:47 +08:00
if (this.inheritance) {
const inheritance = this.inheritance.map(
(item, index) =>
`inheritance_${index}|${item[0] || ""}|${item[1] || ""}|${
item[2] || ""
}`
);
identifier += `|${inheritance.join("|")}`;
}
2023-04-25 20:49:16 +08:00
return identifier;
}
/**
* @param {RequestShortener} requestShortener the request shortener
* @returns {string} a user readable identifier of the module
*/
readableIdentifier(requestShortener) {
const readableIdentifier = super.readableIdentifier(requestShortener);
2023-05-01 22:46:47 +08:00
let identifier = `css ${readableIdentifier}`;
if (this.cssLayer) {
identifier += ` (layer: ${this.cssLayer})`;
}
if (this.supports) {
identifier += ` (supports: ${this.supports})`;
}
if (this.media) {
identifier += ` (media: ${this.media})`;
}
return identifier;
2023-04-25 20:49:16 +08:00
}
/**
* Assuming this module is in the cache. Update the (cached) module with
* the fresh module from the factory. Usually updates internal references
* and properties.
* @param {Module} module fresh module
* @returns {void}
*/
updateCacheModule(module) {
super.updateCacheModule(module);
const m = /** @type {CssModule} */ (module);
2023-04-26 00:22:30 +08:00
this.cssLayer = m.cssLayer;
2023-04-25 20:49:16 +08:00
this.supports = m.supports;
this.media = m.media;
2023-05-01 22:46:47 +08:00
this.inheritance = m.inheritance;
2023-04-25 20:49:16 +08:00
}
2023-04-26 00:37:59 +08:00
/**
* @param {ObjectSerializerContext} context context
*/
2023-04-25 21:13:39 +08:00
serialize(context) {
const { write } = context;
2023-04-26 00:22:30 +08:00
write(this.cssLayer);
2023-04-25 21:13:39 +08:00
write(this.supports);
write(this.media);
2023-05-01 22:46:47 +08:00
write(this.inheritance);
2023-04-25 21:13:39 +08:00
super.serialize(context);
}
2023-04-26 00:37:59 +08:00
/**
* @param {ObjectDeserializerContext} context context
* @returns {CssModule} the deserialized object
*/
2023-04-25 20:49:16 +08:00
static deserialize(context) {
const obj = new CssModule({
// will be deserialized by Module
layer: null,
type: "",
// will be filled by updateCacheModule
resource: "",
context: "",
request: null,
userRequest: null,
rawRequest: null,
loaders: null,
matchResource: null,
parser: null,
parserOptions: null,
generator: null,
generatorOptions: null,
2023-04-25 21:59:17 +08:00
resolveOptions: null,
cssLayer: null,
supports: null,
2023-05-01 22:46:47 +08:00
media: null,
inheritance: null
2023-04-25 20:49:16 +08:00
});
obj.deserialize(context);
return obj;
}
2023-04-26 00:37:59 +08:00
/**
* @param {ObjectDeserializerContext} context context
*/
2023-04-25 20:49:16 +08:00
deserialize(context) {
const { read } = context;
2023-04-26 00:22:30 +08:00
this.cssLayer = read();
2023-04-25 20:49:16 +08:00
this.supports = read();
this.media = read();
2023-05-01 22:46:47 +08:00
this.inheritance = read();
2023-04-25 20:49:16 +08:00
super.deserialize(context);
}
}
2023-04-25 21:13:39 +08:00
makeSerializable(CssModule, "webpack/lib/CssModule");
module.exports = CssModule;