webpack/lib/dependencies/CssImportDependency.js

110 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-12-01 20:27:00 +08:00
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Ivan Kopeykin @vankop
*/
"use strict";
const makeSerializable = require("../util/makeSerializable");
const ModuleDependency = require("./ModuleDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
2023-05-22 04:31:30 +08:00
/** @typedef {import("../css/CssParser").Range} Range */
2023-04-26 00:37:59 +08:00
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
2021-12-01 20:27:00 +08:00
class CssImportDependency extends ModuleDependency {
/**
2023-04-26 06:19:06 +08:00
* Example of dependency:
* \@import url("landscape.css") layer(forms) screen and (orientation: landscape) screen and (orientation: landscape);
2021-12-01 20:27:00 +08:00
* @param {string} request request
2023-05-22 04:31:30 +08:00
* @param {Range} range range of the argument
2023-04-25 09:17:51 +08:00
* @param {string | undefined} layer layer
2021-12-01 20:27:00 +08:00
* @param {string | undefined} supports list of supports conditions
* @param {string | undefined} media list of media conditions
*/
2023-04-25 09:17:51 +08:00
constructor(request, range, layer, supports, media) {
2021-12-01 20:27:00 +08:00
super(request);
this.range = range;
2023-04-25 09:17:51 +08:00
this.layer = layer;
2021-12-01 20:27:00 +08:00
this.supports = supports;
this.media = media;
}
get type() {
return "css @import";
}
get category() {
2023-04-26 00:43:45 +08:00
return "css-import";
2021-12-01 20:27:00 +08:00
}
2023-04-25 21:13:39 +08:00
/**
* @returns {string | null} an identifier to merge equal requests
*/
2023-04-25 09:33:47 +08:00
getResourceIdentifier() {
2023-04-25 21:13:39 +08:00
let str = `context${this._context || ""}|module${this.request}`;
if (this.layer) {
str += `|layer${this.layer}`;
}
if (this.supports) {
str += `|supports${this.supports}`;
}
if (this.media) {
str += `|media${this.media}`;
}
return str;
2023-04-25 09:33:47 +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;
write(this.layer);
write(this.supports);
write(this.media);
super.serialize(context);
}
2023-04-26 00:37:59 +08:00
/**
* @param {ObjectDeserializerContext} context context
*/
2023-04-25 21:13:39 +08:00
deserialize(context) {
const { read } = context;
this.layer = read();
this.supports = read();
this.media = read();
super.deserialize(context);
}
2021-12-01 20:27:00 +08:00
}
CssImportDependency.Template = class CssImportDependencyTemplate extends (
ModuleDependency.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}
*/
apply(dependency, source, templateContext) {
const dep = /** @type {CssImportDependency} */ (dependency);
source.replace(dep.range[0], dep.range[1] - 1, "");
}
};
makeSerializable(
CssImportDependency,
"webpack/lib/dependencies/CssImportDependency"
);
module.exports = CssImportDependency;