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 |
|
---|
17 | /** @typedef {string | undefined} CssLayer */
|
---|
18 | /** @typedef {string | undefined} Supports */
|
---|
19 | /** @typedef {string | undefined} Media */
|
---|
20 | /** @typedef {[CssLayer, Supports, Media]} InheritanceItem */
|
---|
21 | /** @typedef {Array<InheritanceItem>} Inheritance */
|
---|
22 |
|
---|
23 | /** @typedef {NormalModuleCreateData & { cssLayer: CssLayer, supports: Supports, media: Media, inheritance: Inheritance }} CSSModuleCreateData */
|
---|
24 |
|
---|
25 | class CssModule extends NormalModule {
|
---|
26 | /**
|
---|
27 | * @param {CSSModuleCreateData} options options object
|
---|
28 | */
|
---|
29 | constructor(options) {
|
---|
30 | super(options);
|
---|
31 |
|
---|
32 | // Avoid override `layer` for `Module` class, because it is a feature to run module in specific layer
|
---|
33 | this.cssLayer = options.cssLayer;
|
---|
34 | this.supports = options.supports;
|
---|
35 | this.media = options.media;
|
---|
36 | this.inheritance = options.inheritance;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * @returns {string} a unique identifier of the module
|
---|
41 | */
|
---|
42 | identifier() {
|
---|
43 | let identifier = super.identifier();
|
---|
44 |
|
---|
45 | if (this.cssLayer) {
|
---|
46 | identifier += `|${this.cssLayer}`;
|
---|
47 | }
|
---|
48 |
|
---|
49 | if (this.supports) {
|
---|
50 | identifier += `|${this.supports}`;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (this.media) {
|
---|
54 | identifier += `|${this.media}`;
|
---|
55 | }
|
---|
56 |
|
---|
57 | if (this.inheritance) {
|
---|
58 | const inheritance = this.inheritance.map(
|
---|
59 | (item, index) =>
|
---|
60 | `inheritance_${index}|${item[0] || ""}|${item[1] || ""}|${
|
---|
61 | item[2] || ""
|
---|
62 | }`
|
---|
63 | );
|
---|
64 |
|
---|
65 | identifier += `|${inheritance.join("|")}`;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // We generate extra code for HMR, so we need to invalidate the module
|
---|
69 | if (this.hot) {
|
---|
70 | identifier += `|${this.hot}`;
|
---|
71 | }
|
---|
72 |
|
---|
73 | return identifier;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * @param {RequestShortener} requestShortener the request shortener
|
---|
78 | * @returns {string} a user readable identifier of the module
|
---|
79 | */
|
---|
80 | readableIdentifier(requestShortener) {
|
---|
81 | const readableIdentifier = super.readableIdentifier(requestShortener);
|
---|
82 |
|
---|
83 | let identifier = `css ${readableIdentifier}`;
|
---|
84 |
|
---|
85 | if (this.cssLayer) {
|
---|
86 | identifier += ` (layer: ${this.cssLayer})`;
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (this.supports) {
|
---|
90 | identifier += ` (supports: ${this.supports})`;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (this.media) {
|
---|
94 | identifier += ` (media: ${this.media})`;
|
---|
95 | }
|
---|
96 |
|
---|
97 | return identifier;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Assuming this module is in the cache. Update the (cached) module with
|
---|
102 | * the fresh module from the factory. Usually updates internal references
|
---|
103 | * and properties.
|
---|
104 | * @param {Module} module fresh module
|
---|
105 | * @returns {void}
|
---|
106 | */
|
---|
107 | updateCacheModule(module) {
|
---|
108 | super.updateCacheModule(module);
|
---|
109 | const m = /** @type {CssModule} */ (module);
|
---|
110 | this.cssLayer = m.cssLayer;
|
---|
111 | this.supports = m.supports;
|
---|
112 | this.media = m.media;
|
---|
113 | this.inheritance = m.inheritance;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * @param {ObjectSerializerContext} context context
|
---|
118 | */
|
---|
119 | serialize(context) {
|
---|
120 | const { write } = context;
|
---|
121 | write(this.cssLayer);
|
---|
122 | write(this.supports);
|
---|
123 | write(this.media);
|
---|
124 | write(this.inheritance);
|
---|
125 | super.serialize(context);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * @param {ObjectDeserializerContext} context context
|
---|
130 | * @returns {CssModule} the deserialized object
|
---|
131 | */
|
---|
132 | static deserialize(context) {
|
---|
133 | const obj = new CssModule({
|
---|
134 | // will be deserialized by Module
|
---|
135 | layer: /** @type {EXPECTED_ANY} */ (null),
|
---|
136 | type: "",
|
---|
137 | // will be filled by updateCacheModule
|
---|
138 | resource: "",
|
---|
139 | context: "",
|
---|
140 | request: /** @type {EXPECTED_ANY} */ (null),
|
---|
141 | userRequest: /** @type {EXPECTED_ANY} */ (null),
|
---|
142 | rawRequest: /** @type {EXPECTED_ANY} */ (null),
|
---|
143 | loaders: /** @type {EXPECTED_ANY} */ (null),
|
---|
144 | matchResource: /** @type {EXPECTED_ANY} */ (null),
|
---|
145 | parser: /** @type {EXPECTED_ANY} */ (null),
|
---|
146 | parserOptions: /** @type {EXPECTED_ANY} */ (null),
|
---|
147 | generator: /** @type {EXPECTED_ANY} */ (null),
|
---|
148 | generatorOptions: /** @type {EXPECTED_ANY} */ (null),
|
---|
149 | resolveOptions: /** @type {EXPECTED_ANY} */ (null),
|
---|
150 | cssLayer: /** @type {EXPECTED_ANY} */ (null),
|
---|
151 | supports: /** @type {EXPECTED_ANY} */ (null),
|
---|
152 | media: /** @type {EXPECTED_ANY} */ (null),
|
---|
153 | inheritance: /** @type {EXPECTED_ANY} */ (null)
|
---|
154 | });
|
---|
155 | obj.deserialize(context);
|
---|
156 | return obj;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * @param {ObjectDeserializerContext} context context
|
---|
161 | * @returns {TODO} Module
|
---|
162 | */
|
---|
163 | deserialize(context) {
|
---|
164 | const { read } = context;
|
---|
165 | this.cssLayer = read();
|
---|
166 | this.supports = read();
|
---|
167 | this.media = read();
|
---|
168 | this.inheritance = read();
|
---|
169 | super.deserialize(context);
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | makeSerializable(CssModule, "webpack/lib/CssModule");
|
---|
174 |
|
---|
175 | module.exports = CssModule;
|
---|