webpack/lib/CssModule.js

124 lines
3.0 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-25 20:49:16 +08:00
class CssModule extends NormalModule {
2023-04-25 21:59:17 +08:00
/**
* @param {NormalModuleCreateData & { cssLayer: string|undefined|null, supports: string|undefined|null, media: string|undefined|null }} options options object
*/
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-25 21:59:17 +08:00
this.csslayer = options.cssLayer;
this.supports = options.supports;
this.media = options.media;
2023-04-25 20:49:16 +08:00
}
/**
* @returns {string} a unique identifier of the module
*/
identifier() {
let identifier = super.identifier();
if (this.csslayer) {
identifier += `|${this.csslayer}`;
}
if (this.supports) {
identifier += `|${this.supports}`;
}
if (this.media) {
identifier += `|${this.media}`;
}
return identifier;
}
/**
* @param {RequestShortener} requestShortener the request shortener
* @returns {string} a user readable identifier of the module
*/
readableIdentifier(requestShortener) {
const readableIdentifier = super.readableIdentifier(requestShortener);
return `css ${readableIdentifier}${
2023-04-25 21:13:39 +08:00
this.csslayer ? ` (layer ${this.csslayer || ""})` : ""
}${this.supports ? ` (supports ${this.supports || ""})` : ""}${
this.media ? ` (media ${this.media || ""})` : ""
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);
this.csslayer = m.csslayer;
this.supports = m.supports;
this.media = m.media;
}
2023-04-25 21:13:39 +08:00
serialize(context) {
const { write } = context;
write(this.csslayer);
write(this.supports);
write(this.media);
super.serialize(context);
}
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,
media: null
2023-04-25 20:49:16 +08:00
});
obj.deserialize(context);
return obj;
}
deserialize(context) {
const { read } = context;
this.csslayer = read();
this.supports = read();
this.media = read();
super.deserialize(context);
}
}
2023-04-25 21:13:39 +08:00
makeSerializable(CssModule, "webpack/lib/CssModule");
module.exports = CssModule;