Skip to content

Commit ad3249b

Browse files
authored
fix(css): split @value imports on the from keyword (#22306)
* fix(css): split @value imports on the from keyword parseValueAtRuleParams located the `from` separator in `@value <names> from <source>` with `str.lastIndexOf("from")`. When the source path or an import name contained the substring `from` (e.g. `./from-colors.module.css`), the split landed inside the path, so names and source were mis-parsed and the import never resolved. Split on the first whitespace-delimited `from` keyword instead, matching postcss-modules-values semantics. Comments are already replaced with spaces before the split, so the keyword is reliably whitespace-delimited. Signed-off-by: Eshiv Pandey <[email protected]> * fix(css): restrict @value from separator to CSS whitespace JS \s also matches U+00A0 and other spaces that are valid inside an import name, which could select a from inside the name; limit both separator boundaries to the CSS whitespace set (space, tab, LF, FF, CR). Signed-off-by: Eshiv Pandey <[email protected]> --------- Signed-off-by: Eshiv Pandey <[email protected]>
1 parent 58156d6 commit ad3249b

6 files changed

Lines changed: 72 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Split `@value` imports on the `from` keyword, not a substring in the path.

‎lib/css/CssParser.js‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -564,12 +564,14 @@ const scanRuleBody = (declarations, childRules) => {
564564
const parseValueAtRuleParams = (str) => {
565565
if (VALUE_IMPORT_FORM.test(str)) {
566566
str = str.replace(CSS_COMMENT, " ").trim().replace(/;$/, "");
567-
const fromIdx = str.lastIndexOf("from");
568-
const path = str
569-
.slice(fromIdx + 5)
570-
.trim()
571-
.replace(/['"]/g, "");
572-
let content = str.slice(0, fromIdx).trim();
567+
// Split on the first `from` keyword delimited by CSS whitespace (only
568+
// ` \t\n\f\r`, not JS `\s`, which also matches U+00A0 valid in a name),
569+
// so a path or name containing the substring (`./from-colors.css`) isn't it.
570+
const fromMatch = /^([\s\S]+?)[ \t\n\f\r]+from[ \t\n\f\r]+([\s\S]+)$/i.exec(
571+
str
572+
);
573+
const path = (fromMatch ? fromMatch[2] : "").trim().replace(/['"]/g, "");
574+
let content = (fromMatch ? fromMatch[1] : str).trim();
573575

574576
if (content.startsWith("(") && content.endsWith(")")) {
575577
content = content.slice(1, -1);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@value primary: rebeccapurple;
2+
@value theme-from-base: 10px;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as style from "./style.module.css";
2+
3+
const fs = __non_webpack_require__("fs");
4+
const path = __non_webpack_require__("path");
5+
6+
it("should split `@value ... from <source>` on the keyword when the source path contains the substring `from`", () => {
7+
// Both `@value`s import from `./from-colors.module.css`; the second import
8+
// name also contains `from`. A `lastIndexOf("from")` split lands inside the
9+
// path, so the request never resolves and nothing is substituted.
10+
expect(style.a).toMatch(/a$/);
11+
12+
const cssContent = fs.readFileSync(
13+
path.join(__dirname, "bundle0.css"),
14+
"utf-8"
15+
);
16+
17+
// The imported values must be substituted into the declaration, proving the
18+
// `./from-colors.module.css` request resolved.
19+
expect(cssContent).toMatch(/color:\s*rebeccapurple/);
20+
expect(cssContent).toMatch(/padding:\s*10px/);
21+
22+
// The identifiers must not survive unsubstituted.
23+
expect(cssContent).not.toMatch(/color:\s*primary/);
24+
expect(cssContent).not.toMatch(/padding:\s*theme-from-base/);
25+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@value primary from "./from-colors.module.css";
2+
@value theme-from-base from "./from-colors.module.css";
3+
4+
.a {
5+
color: primary;
6+
padding: theme-from-base;
7+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
target: "web",
6+
mode: "development",
7+
experiments: {
8+
css: true
9+
},
10+
output: {
11+
uniqueName: "value-from-in-path"
12+
},
13+
node: {
14+
__dirname: false,
15+
__filename: false
16+
},
17+
module: {
18+
rules: [
19+
{
20+
test: /\.module\.css$/i,
21+
type: "css/module"
22+
}
23+
]
24+
}
25+
};

0 commit comments

Comments
 (0)