Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect media/supports/layer from parent CSS module #17115

Merged
merged 3 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ charset = utf-8-bom

[*.md]
trim_trailing_whitespace = false

[*.snap]
trim_trailing_whitespace = false
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
40 changes: 33 additions & 7 deletions lib/CssModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const makeSerializable = require("./util/makeSerializable");
/** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
/** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */

/** @typedef {NormalModuleCreateData & { cssLayer: string|undefined|null, supports: string|undefined|null, media: string|undefined|null }} CSSModuleCreateData */
/** @typedef {NormalModuleCreateData & { cssLayer: string|undefined|null, supports: string|undefined|null, media: string|undefined|null, inheritance: Array<[string|undefined, string|undefined, string|undefined]>|null }} CSSModuleCreateData */

class CssModule extends NormalModule {
/**
Expand All @@ -27,6 +27,7 @@ class CssModule extends NormalModule {
this.cssLayer = options.cssLayer;
this.supports = options.supports;
this.media = options.media;
this.inheritance = options.inheritance;
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -47,6 +48,17 @@ class CssModule extends NormalModule {
identifier += `|${this.media}`;
}

if (this.inheritance) {
const inheritance = this.inheritance.map(
(item, index) =>
`inheritance_${index}|${item[0] || ""}|${item[1] || ""}|${
item[2] || ""
}`
);

identifier += `|${inheritance.join("|")}`;
}

return identifier;
}

Expand All @@ -57,11 +69,21 @@ class CssModule extends NormalModule {
readableIdentifier(requestShortener) {
const readableIdentifier = super.readableIdentifier(requestShortener);

return `css ${readableIdentifier}${
this.cssLayer ? ` (layer ${this.cssLayer || ""})` : ""
}${this.supports ? ` (supports ${this.supports || ""})` : ""}${
this.media ? ` (media ${this.media || ""})` : ""
}`;
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;
}

/**
Expand All @@ -77,6 +99,7 @@ class CssModule extends NormalModule {
this.cssLayer = m.cssLayer;
this.supports = m.supports;
this.media = m.media;
this.inheritance = m.inheritance;
}

/**
Expand All @@ -87,6 +110,7 @@ class CssModule extends NormalModule {
write(this.cssLayer);
write(this.supports);
write(this.media);
write(this.inheritance);
super.serialize(context);
}

Expand Down Expand Up @@ -114,7 +138,8 @@ class CssModule extends NormalModule {
resolveOptions: null,
cssLayer: null,
supports: null,
media: null
media: null,
inheritance: null
});
obj.deserialize(context);
return obj;
Expand All @@ -128,6 +153,7 @@ class CssModule extends NormalModule {
this.cssLayer = read();
this.supports = read();
this.media = read();
this.inheritance = read();
super.deserialize(context);
}
}
Expand Down
88 changes: 62 additions & 26 deletions lib/css/CssModulesPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,36 @@ class CssModulesPlugin {
// When CSS is imported from CSS there is only one dependency
const dependency = resolveData.dependencies[0];

return new CssModule({
...createData,
cssLayer: dependency.layer,
supports: dependency.supports,
media: dependency.media
});
if (dependency instanceof CssImportDependency) {
const parent =
/** @type {CssModule} */
(compilation.moduleGraph.getParentModule(dependency));

if (parent instanceof CssModule) {
let inheritance = [
[parent.cssLayer, parent.supports, parent.media]
];

if (parent.inheritance) {
inheritance.push(...parent.inheritance);
}

return new CssModule({
...createData,
cssLayer: dependency.layer,
supports: dependency.supports,
media: dependency.media,
inheritance
});
}

return new CssModule({
...createData,
cssLayer: dependency.layer,
supports: dependency.supports,
media: dependency.media
});
}
}

return new CssModule(createData);
Expand Down Expand Up @@ -450,29 +474,41 @@ class CssModulesPlugin {
codeGenResult.sources.get("css") ||
codeGenResult.sources.get("css-import");

if (module.media) {
moduleSource = new ConcatSource(
`@media ${module.media} {\n`,
new PrefixSource("\t", moduleSource),
"}"
);
}
let inheritance = [[module.cssLayer, module.supports, module.media]];

if (module.supports) {
moduleSource = new ConcatSource(
`@supports (${module.supports}) {\n`,
new PrefixSource("\t", moduleSource),
"}"
);
if (module.inheritance) {
inheritance.push(...module.inheritance);
}

// Layer can be anonymous
if (module.cssLayer !== undefined && module.cssLayer !== null) {
moduleSource = new ConcatSource(
`@layer${module.cssLayer ? ` (${module.cssLayer})` : ""} {\n`,
new PrefixSource("\t", moduleSource),
"}"
);
for (let i = 0; i < inheritance.length - 1; i++) {
const layer = inheritance[i][0];
const supports = inheritance[i][1];
const media = inheritance[i][2];

if (media) {
moduleSource = new ConcatSource(
`@media ${media} {\n`,
new PrefixSource("\t", moduleSource),
"}\n"
);
}

if (supports) {
moduleSource = new ConcatSource(
`@supports (${supports}) {\n`,
new PrefixSource("\t", moduleSource),
"}\n"
);
}

// Layer can be anonymous
if (layer !== undefined && layer !== null) {
moduleSource = new ConcatSource(
`@layer${layer ? ` ${layer}` : ""} {\n`,
new PrefixSource("\t", moduleSource),
"}\n"
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

if (moduleSource) {
Expand Down
15 changes: 2 additions & 13 deletions lib/css/CssParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,18 +197,6 @@ class CssParser extends Parser {
const isTopLevelLocal = () =>
modeData === "local" ||
(this.defaultMode === "local" && modeData === undefined);
const eatWhiteLine = (input, pos) => {
for (;;) {
const cc = input.charCodeAt(pos);
if (cc === 32 || cc === 9) {
pos++;
continue;
}
if (cc === 10) pos++;
break;
}
return pos;
};
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
const eatUntil = chars => {
const charCodes = Array.from({ length: chars.length }, (_, i) =>
chars.charCodeAt(i)
Expand Down Expand Up @@ -304,7 +292,7 @@ class CssParser extends Parser {
}
pos++;
if (pos === input.length) return pos;
pos = eatWhiteLine(input, pos);
pos = walkCssTokens.eatWhiteLine(input, pos);
return pos;
};
const eatPropertyName = eatUntil(":{};");
Expand Down Expand Up @@ -521,6 +509,7 @@ class CssParser extends Parser {
);
}
const semicolonPos = end;
end = walkCssTokens.eatWhiteLine(input, end + 1);
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
const { line: sl, column: sc } = locConverter.get(
modeData.atRuleStart
);
Expand Down
48 changes: 41 additions & 7 deletions lib/css/walkCssTokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,30 @@ const consumeSpace = (input, pos, callbacks) => {

/**
* @param {number} cc char code
* @returns {boolean} true, if cc is a whitespace
* @returns {boolean} true, if cc is a newline
*/
const _isWhiteSpace = cc => {
const _isNewline = cc => {
return (
cc === CC_LINE_FEED ||
cc === CC_CARRIAGE_RETURN ||
cc === CC_FORM_FEED ||
cc === CC_TAB ||
cc === CC_SPACE
cc === CC_LINE_FEED || cc === CC_CARRIAGE_RETURN || cc === CC_FORM_FEED
);
};

/**
* @param {number} cc char code
* @returns {boolean} true, if cc is a space (U+0009 CHARACTER TABULATION or U+0020 SPACE)
*/
const _isSpace = cc => {
return cc === CC_TAB || cc === CC_SPACE;
};

/**
* @param {number} cc char code
* @returns {boolean} true, if cc is a whitespace
*/
const _isWhiteSpace = cc => {
return _isNewline(cc) || _isSpace(cc);
};

/**
* @param {number} cc char code
* @returns {boolean} true, if cc is a start code point of an identifier
Expand Down Expand Up @@ -739,3 +751,25 @@ module.exports.eatWhitespaceAndComments = (input, pos) => {

return pos;
};

/**
* @param {string} input input
* @param {number} pos position
* @returns {number} position after whitespace
*/
module.exports.eatWhiteLine = (input, pos) => {
for (;;) {
const cc = input.charCodeAt(pos);
if (_isSpace(cc)) {
pos++;
continue;
}
if (_isNewLine(cc)) pos++;
// For `\r\n`
if (cc === CC_CARRIAGE_RETURN && input.charCodeAt(pos + 1) === CC_LINE_FEED)
pos++;
break;
}

return pos;
};