Skip to content

Commit e930051

Browse files
authored
fix(css): preserve invalid gradient color stops (#22417)
* fix(css): preserve invalid gradient color stops * fix(css): note safe gradient minification in changeset
1 parent bff03d7 commit e930051

8 files changed

Lines changed: 113 additions & 1 deletion

File tree

‎.changeset/022-css-extract-comments-gradients.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": minor
33
---
44

5-
Extract CSS and HTML license comments like JS, and shorten CSS gradients.
5+
Extract CSS/HTML license comments like JS and safely shorten CSS gradients.

‎lib/css/syntax-printer.js‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,6 +3255,13 @@ const _dropDefaultGradientPosition = (fn, inner) => {
32553255
if (match === null) return null;
32563256
const position = match[2].toLowerCase().replace(/\s+/g, " ");
32573257
if (!DEFAULT_GRADIENT_POSITIONS.has(position)) return null;
3258+
// Removing `at` from a color stop would make an invalid gradient valid.
3259+
if (
3260+
match[1] !== undefined &&
3261+
_splitTopLevelSpaces(match[1]).some(_isBareColor)
3262+
) {
3263+
return null;
3264+
}
32583265
const rest = inner.slice(comma + 1).trim();
32593266
return match[1] === undefined ? rest : `${match[1]},${rest}`;
32603267
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigCacheTestCases css minimize-gradient-position-validity minimize-gradient-position-validity should compile 1`] = `
4+
Array [
5+
"/*! at cannot follow a color stop; the earlier declaration must remain effective */.named{background-image:linear-gradient(red,blue);background-image:radial-gradient(red at center,blue)}",
6+
"/*! a hex color with a position is still a stop */.hex{background-image:repeating-radial-gradient(#123 10px at 50%,blue 20px)}",
7+
"/*! a color function is also a stop */.function{background-image:conic-gradient(color(display-p3 1 0 0) at center center,blue)}",
8+
"/*! a context-dependent color can carry a position */.current{background-image:repeating-conic-gradient(currentColor 10deg at 50% 50%,blue 20deg)}",
9+
"/*! a conic stop can put its angle before its color */.angle-first{background-image:conic-gradient(10deg red at center,blue)}",
10+
"/*! system colors are stops too */.system{background-image:radial-gradient(Canvas at center,blue)}",
11+
"/*! valid preludes still lose their default positions */.shape{background-image:radial-gradient(circle,red,blue)}.position-only{background-image:radial-gradient(red,blue)}.size{background-image:repeating-radial-gradient(10px 20px,red,blue 5px)}.rotation{background-image:conic-gradient(from 90deg,red,blue)}.repeating{background-image:repeating-conic-gradient(red,blue 20deg)}",
12+
]
13+
`;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2+
3+
exports[`ConfigTestCases css minimize-gradient-position-validity minimize-gradient-position-validity should compile 1`] = `
4+
Array [
5+
"/*! at cannot follow a color stop; the earlier declaration must remain effective */.named{background-image:linear-gradient(red,blue);background-image:radial-gradient(red at center,blue)}",
6+
"/*! a hex color with a position is still a stop */.hex{background-image:repeating-radial-gradient(#123 10px at 50%,blue 20px)}",
7+
"/*! a color function is also a stop */.function{background-image:conic-gradient(color(display-p3 1 0 0) at center center,blue)}",
8+
"/*! a context-dependent color can carry a position */.current{background-image:repeating-conic-gradient(currentColor 10deg at 50% 50%,blue 20deg)}",
9+
"/*! a conic stop can put its angle before its color */.angle-first{background-image:conic-gradient(10deg red at center,blue)}",
10+
"/*! system colors are stops too */.system{background-image:radial-gradient(Canvas at center,blue)}",
11+
"/*! valid preludes still lose their default positions */.shape{background-image:radial-gradient(circle,red,blue)}.position-only{background-image:radial-gradient(red,blue)}.size{background-image:repeating-radial-gradient(10px 20px,red,blue 5px)}.rotation{background-image:conic-gradient(from 90deg,red,blue)}.repeating{background-image:repeating-conic-gradient(red,blue 20deg)}",
12+
]
13+
`;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import "./style.css";
2+
3+
it("should preserve gradient validity", () => {
4+
expect(true).toBe(true);
5+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*! at cannot follow a color stop; the earlier declaration must remain effective */
2+
.named {
3+
background-image: linear-gradient(red, blue);
4+
background-image: radial-gradient(red at center, blue);
5+
}
6+
/*! a hex color with a position is still a stop */
7+
.hex {
8+
background-image: repeating-radial-gradient(#123 10px at 50%, blue 20px);
9+
}
10+
/*! a color function is also a stop */
11+
.function {
12+
background-image: conic-gradient(color(display-p3 1 0 0) at center center, blue);
13+
}
14+
/*! a context-dependent color can carry a position */
15+
.current {
16+
background-image: repeating-conic-gradient(currentColor 10deg at 50% 50%, blue 20deg);
17+
}
18+
/*! a conic stop can put its angle before its color */
19+
.angle-first {
20+
background-image: conic-gradient(10deg red at center, blue);
21+
}
22+
/*! system colors are stops too */
23+
.system {
24+
background-image: radial-gradient(Canvas at center, blue);
25+
}
26+
/*! valid preludes still lose their default positions */
27+
.shape {
28+
background-image: radial-gradient(circle at center, red, blue);
29+
}
30+
.position-only {
31+
background-image: radial-gradient(at 50% 50%, red, blue);
32+
}
33+
.size {
34+
background-image: repeating-radial-gradient(10px 20px at center center, red, blue 5px);
35+
}
36+
.rotation {
37+
background-image: conic-gradient(from 90deg at 50%, red, blue);
38+
}
39+
.repeating {
40+
background-image: repeating-conic-gradient(at center, red, blue 20deg);
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict";
2+
3+
const readMinifiedSections = require("../../../helpers/readMinifiedSections");
4+
5+
module.exports = {
6+
findBundle() {
7+
return ["bundle0.js"];
8+
},
9+
afterExecute(options) {
10+
expect(
11+
readMinifiedSections(options.output.path, "bundle0.css")
12+
).toMatchSnapshot();
13+
}
14+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
target: "web",
6+
mode: "production",
7+
output: {
8+
pathinfo: false
9+
},
10+
optimization: {
11+
minimize: {
12+
css: { extractComments: false }
13+
},
14+
minimizer: ["..."]
15+
},
16+
experiments: {
17+
css: true
18+
}
19+
};

0 commit comments

Comments
 (0)