| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Alexander Akait @alexander-akait
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const NormalModule = require("../NormalModule");
|
|---|
| 9 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("../Module")} Module */
|
|---|
| 12 | /** @typedef {import("../NormalModule").NormalModuleCreateData} NormalModuleCreateData */
|
|---|
| 13 | /** @typedef {import("../RequestShortener")} RequestShortener */
|
|---|
| 14 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 15 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 16 | /** @typedef {import("../../declarations/WebpackOptions").CssParserExportType} CssParserExportType */
|
|---|
| 17 |
|
|---|
| 18 | /** @typedef {string | undefined} CssLayer */
|
|---|
| 19 | /** @typedef {string | undefined} Supports */
|
|---|
| 20 | /** @typedef {string | undefined} Media */
|
|---|
| 21 | /** @typedef {[CssLayer, Supports, Media]} InheritanceItem */
|
|---|
| 22 | /** @typedef {InheritanceItem[]} Inheritance */
|
|---|
| 23 |
|
|---|
| 24 | /** @typedef {NormalModuleCreateData & { cssLayer: CssLayer, supports: Supports, media: Media, inheritance?: Inheritance, exportType?: CssParserExportType }} CssModuleCreateData */
|
|---|
| 25 |
|
|---|
| 26 | class CssModule extends NormalModule {
|
|---|
| 27 | /**
|
|---|
| 28 | * Creates an instance of CssModule.
|
|---|
| 29 | * @param {CssModuleCreateData} options options object
|
|---|
| 30 | */
|
|---|
| 31 | constructor(options) {
|
|---|
| 32 | super(options);
|
|---|
| 33 |
|
|---|
| 34 | // Avoid override `layer` for `Module` class, because it is a feature to run module in specific layer
|
|---|
| 35 | /** @type {CssModuleCreateData['cssLayer']} */
|
|---|
| 36 | this.cssLayer = options.cssLayer;
|
|---|
| 37 | /** @type {CssModuleCreateData['supports']} */
|
|---|
| 38 | this.supports = options.supports;
|
|---|
| 39 | /** @type {CssModuleCreateData['media']} */
|
|---|
| 40 | this.media = options.media;
|
|---|
| 41 | /** @type {CssModuleCreateData['inheritance']} */
|
|---|
| 42 | this.inheritance = options.inheritance;
|
|---|
| 43 | /** @type {CssModuleCreateData['exportType']} */
|
|---|
| 44 | this.exportType = options.exportType;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Returns the unique identifier used to reference this module.
|
|---|
| 49 | * @returns {string} a unique identifier of the module
|
|---|
| 50 | */
|
|---|
| 51 | identifier() {
|
|---|
| 52 | let identifier = super.identifier();
|
|---|
| 53 |
|
|---|
| 54 | if (this.cssLayer) {
|
|---|
| 55 | identifier += `|${this.cssLayer}`;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | if (this.supports) {
|
|---|
| 59 | identifier += `|${this.supports}`;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | if (this.media) {
|
|---|
| 63 | identifier += `|${this.media}`;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | if (this.inheritance) {
|
|---|
| 67 | const inheritance = this.inheritance.map(
|
|---|
| 68 | (item, index) =>
|
|---|
| 69 | `inheritance_${index}|${item[0] || ""}|${item[1] || ""}|${
|
|---|
| 70 | item[2] || ""
|
|---|
| 71 | }`
|
|---|
| 72 | );
|
|---|
| 73 |
|
|---|
| 74 | identifier += `|${inheritance.join("|")}`;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | if (this.exportType) {
|
|---|
| 78 | identifier += `|${this.exportType}`;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | // We generate extra code for HMR, so we need to invalidate the module
|
|---|
| 82 | if (this.hot) {
|
|---|
| 83 | identifier += `|${this.hot}`;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | return identifier;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * Returns a human-readable identifier for this module.
|
|---|
| 91 | * @param {RequestShortener} requestShortener the request shortener
|
|---|
| 92 | * @returns {string} a user readable identifier of the module
|
|---|
| 93 | */
|
|---|
| 94 | readableIdentifier(requestShortener) {
|
|---|
| 95 | const readableIdentifier = super.readableIdentifier(requestShortener);
|
|---|
| 96 |
|
|---|
| 97 | let identifier = `css ${readableIdentifier}`;
|
|---|
| 98 |
|
|---|
| 99 | if (this.cssLayer) {
|
|---|
| 100 | identifier += ` (layer: ${this.cssLayer})`;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | if (this.supports) {
|
|---|
| 104 | identifier += ` (supports: ${this.supports})`;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | if (this.media) {
|
|---|
| 108 | identifier += ` (media: ${this.media})`;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | if (this.exportType) {
|
|---|
| 112 | identifier += ` (exportType: ${this.exportType})`;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | return identifier;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | /**
|
|---|
| 119 | * Assuming this module is in the cache. Update the (cached) module with
|
|---|
| 120 | * the fresh module from the factory. Usually updates internal references
|
|---|
| 121 | * and properties.
|
|---|
| 122 | * @param {Module} module fresh module
|
|---|
| 123 | * @returns {void}
|
|---|
| 124 | */
|
|---|
| 125 | updateCacheModule(module) {
|
|---|
| 126 | super.updateCacheModule(module);
|
|---|
| 127 | const m = /** @type {CssModule} */ (module);
|
|---|
| 128 | this.cssLayer = m.cssLayer;
|
|---|
| 129 | this.supports = m.supports;
|
|---|
| 130 | this.media = m.media;
|
|---|
| 131 | this.inheritance = m.inheritance;
|
|---|
| 132 | this.exportType = m.exportType;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /**
|
|---|
| 136 | * Serializes this instance into the provided serializer context.
|
|---|
| 137 | * @param {ObjectSerializerContext} context context
|
|---|
| 138 | */
|
|---|
| 139 | serialize(context) {
|
|---|
| 140 | const { write } = context;
|
|---|
| 141 | write(this.cssLayer);
|
|---|
| 142 | write(this.supports);
|
|---|
| 143 | write(this.media);
|
|---|
| 144 | write(this.inheritance);
|
|---|
| 145 | write(this.exportType);
|
|---|
| 146 | super.serialize(context);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * Restores this instance from the provided deserializer context.
|
|---|
| 151 | * @param {ObjectDeserializerContext} context context
|
|---|
| 152 | * @returns {CssModule} the deserialized object
|
|---|
| 153 | */
|
|---|
| 154 | static deserialize(context) {
|
|---|
| 155 | const obj = new CssModule({
|
|---|
| 156 | // will be deserialized by Module
|
|---|
| 157 | layer: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 158 | type: "",
|
|---|
| 159 | // will be filled by updateCacheModule
|
|---|
| 160 | resource: "",
|
|---|
| 161 | context: "",
|
|---|
| 162 | request: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 163 | userRequest: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 164 | rawRequest: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 165 | loaders: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 166 | matchResource: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 167 | parser: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 168 | parserOptions: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 169 | generator: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 170 | generatorOptions: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 171 | resolveOptions: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 172 | cssLayer: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 173 | supports: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 174 | media: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 175 | inheritance: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 176 | extractSourceMap: /** @type {EXPECTED_ANY} */ (null),
|
|---|
| 177 | exportType: /** @type {EXPECTED_ANY} */ (null)
|
|---|
| 178 | });
|
|---|
| 179 | obj.deserialize(context);
|
|---|
| 180 | return obj;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | /**
|
|---|
| 184 | * Restores this instance from the provided deserializer context.
|
|---|
| 185 | * @param {ObjectDeserializerContext} context context
|
|---|
| 186 | */
|
|---|
| 187 | deserialize(context) {
|
|---|
| 188 | const { read } = context;
|
|---|
| 189 | this.cssLayer = read();
|
|---|
| 190 | this.supports = read();
|
|---|
| 191 | this.media = read();
|
|---|
| 192 | this.inheritance = read();
|
|---|
| 193 | this.exportType = read();
|
|---|
| 194 | super.deserialize(context);
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | makeSerializable(CssModule, "webpack/lib/CssModule");
|
|---|
| 199 |
|
|---|
| 200 | module.exports = CssModule;
|
|---|