Skip to content

Commit c84bfc9

Browse files
fix(emotion): fix autoLabel not adding label to css tagged template class names (#597)
Fix `autoLabel` option not working for `css` tagged template literals. The label was added without the `label:` CSS property prefix, so emotion's runtime did not recognize it and omitted it from the class name. Fixes #571 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: Donny/강동윤 <kdy1@users.noreply.github.com>
1 parent 27bcf2e commit c84bfc9

23 files changed

Lines changed: 116 additions & 32 deletions

packages/emotion/__tests__/__snapshots__/wasm.test.ts.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`Should add label to css tagged template when autoLabel is 'always' 1`] = `
4+
"import { css } from '@emotion/css';
5+
export var testCls = /*#__PURE__*/ css("font-size:100px;font-weight:bold;", "label:testCls");
6+
export var anotherStyle = /*#__PURE__*/ css("color:red;", "label:anotherStyle");
7+
"
8+
`;
9+
310
exports[`Should transform emotion css correctly 1`] = `
411
"function _tagged_template_literal(strings, raw) {
512
if (!raw) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { css } from '@emotion/css';
2+
3+
export const testCls = css`
4+
font-size: 100px;
5+
font-weight: bold;
6+
`;
7+
8+
export const anotherStyle = css`
9+
color: red;
10+
`;

packages/emotion/__tests__/wasm.test.ts

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import url from "node:url";
55
import { type Options, transform } from "@swc/core";
66

77
const pluginName = "swc_plugin_emotion.wasm";
8+
const pluginPath = path.join(
9+
path.dirname(url.fileURLToPath(import.meta.url)),
10+
"..",
11+
pluginName,
12+
);
813

914
const options: Options = {
1015
jsc: {
@@ -14,11 +19,7 @@ const options: Options = {
1419
experimental: {
1520
plugins: [
1621
[
17-
path.join(
18-
path.dirname(url.fileURLToPath(import.meta.url)),
19-
"..",
20-
pluginName,
21-
),
22+
pluginPath,
2223
{},
2324
],
2425
],
@@ -39,3 +40,69 @@ test("Should transform emotion css correctly", async () => {
3940
const output = await transform(code, options);
4041
expect(output.code).toMatchSnapshot();
4142
});
43+
44+
test("Should add label to css tagged template when autoLabel is 'always'", async () => {
45+
const code = await fs.readFile(
46+
path.resolve(
47+
url.fileURLToPath(import.meta.url),
48+
"..",
49+
"fixtures",
50+
"auto-label-input.js",
51+
),
52+
"utf-8",
53+
);
54+
const output = await transform(code, {
55+
jsc: {
56+
parser: {
57+
syntax: "ecmascript",
58+
},
59+
experimental: {
60+
plugins: [
61+
[
62+
pluginPath,
63+
{
64+
autoLabel: "always",
65+
sourceMap: false,
66+
},
67+
],
68+
],
69+
},
70+
},
71+
});
72+
expect(output.code).toMatchSnapshot();
73+
// Verify labels are added with the correct "label:" prefix
74+
expect(output.code).toContain('"label:testCls"');
75+
expect(output.code).toContain('"label:anotherStyle"');
76+
});
77+
78+
test("Should not add label to css tagged template when autoLabel is 'never'", async () => {
79+
const code = await fs.readFile(
80+
path.resolve(
81+
url.fileURLToPath(import.meta.url),
82+
"..",
83+
"fixtures",
84+
"auto-label-input.js",
85+
),
86+
"utf-8",
87+
);
88+
const output = await transform(code, {
89+
jsc: {
90+
parser: {
91+
syntax: "ecmascript",
92+
},
93+
experimental: {
94+
plugins: [
95+
[
96+
pluginPath,
97+
{
98+
autoLabel: "never",
99+
sourceMap: false,
100+
},
101+
],
102+
],
103+
},
104+
},
105+
});
106+
expect(output.code).not.toContain('"label:testCls"');
107+
expect(output.code).not.toContain('"label:anotherStyle"');
108+
});

packages/emotion/transform/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ impl<C: Comments> Fold for EmotionTransformer<'_, C> {
727727
if !self.in_jsx_element {
728728
self.comments.add_pure_comment(i.span.lo());
729729
if self.options.auto_label.unwrap_or(false) {
730-
args.push(self.create_label(false).as_arg());
730+
args.push(self.create_label(true).as_arg());
731731
}
732732
if let Some(cm) = self.create_sourcemap(tagged_tpl.span.lo()) {
733733
args.push(cm.as_arg());

packages/emotion/transform/tests/fixture/compress/output.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const Animated = /*#__PURE__*/ styled("div", {
1010
target: "e1xgaop21",
1111
label: "Animated"
1212
})("& code{background-color:linen;}animation:", ({ animation })=>animation, " 0.2s infinite ease-in-out alternate;", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5wdXQudHMiLCJzb3VyY2VzIjpbImlucHV0LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IGNzcyB9IGZyb20gXCJAZW1vdGlvbi9yZWFjdFwiO1xuaW1wb3J0IHN0eWxlZCBmcm9tIFwiQGVtb3Rpb24vc3R5bGVkXCI7XG5cbmNvbnN0IHVuaXROb3JtYWwgPSBcIjFyZW1cIjtcbmNvbnN0IHVuaXRMYXJnZSA9IFwiMnJlbVwiO1xuXG5jb25zdCBFeGFtcGxlID0gc3R5bGVkLmRpdmBcbiAgbWFyZ2luOiAke3VuaXROb3JtYWx9ICR7dW5pdExhcmdlfTtcbmA7XG5cbmV4cG9ydCBjb25zdCBBbmltYXRlZCA9IHN0eWxlZC5kaXZgXG4gICYgY29kZSB7XG4gICAgYmFja2dyb3VuZC1jb2xvcjogbGluZW47XG4gIH1cbiAgYW5pbWF0aW9uOiAkeyh7IGFuaW1hdGlvbiB9KSA9PiBhbmltYXRpb259IDAuMnMgaW5maW5pdGUgZWFzZS1pbi1vdXQgYWx0ZXJuYXRlO1xuYDtcblxuY29uc3Qgc2hhZG93Qm9yZGVyID0gKHsgd2lkdGggPSBcIjFweFwiLCBjb2xvciB9KSA9PiBjc3NgXG4gIGJveC1zaGFkb3c6IGluc2V0IDBweCAwcHggMHB4ICR7d2lkdGh9ICR7Y29sb3J9O1xuYDtcblxuY29uc3QgU3R5bGVkSW5wdXQgPSBzdHlsZWQuaW5wdXRgXG4gICR7c2hhZG93Qm9yZGVyKHsgY29sb3I6IFwicmVkXCIsIHdpZHRoOiBcIjRweFwiIH0pfVxuYDtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFVd0IifQ== */");
13-
const shadowBorder = ({ width = "1px", color })=>/*#__PURE__*/ css("box-shadow:inset 0px 0px 0px ", width, " ", color, ";", "shadowBorder", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5wdXQudHMiLCJzb3VyY2VzIjpbImlucHV0LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IGNzcyB9IGZyb20gXCJAZW1vdGlvbi9yZWFjdFwiO1xuaW1wb3J0IHN0eWxlZCBmcm9tIFwiQGVtb3Rpb24vc3R5bGVkXCI7XG5cbmNvbnN0IHVuaXROb3JtYWwgPSBcIjFyZW1cIjtcbmNvbnN0IHVuaXRMYXJnZSA9IFwiMnJlbVwiO1xuXG5jb25zdCBFeGFtcGxlID0gc3R5bGVkLmRpdmBcbiAgbWFyZ2luOiAke3VuaXROb3JtYWx9ICR7dW5pdExhcmdlfTtcbmA7XG5cbmV4cG9ydCBjb25zdCBBbmltYXRlZCA9IHN0eWxlZC5kaXZgXG4gICYgY29kZSB7XG4gICAgYmFja2dyb3VuZC1jb2xvcjogbGluZW47XG4gIH1cbiAgYW5pbWF0aW9uOiAkeyh7IGFuaW1hdGlvbiB9KSA9PiBhbmltYXRpb259IDAuMnMgaW5maW5pdGUgZWFzZS1pbi1vdXQgYWx0ZXJuYXRlO1xuYDtcblxuY29uc3Qgc2hhZG93Qm9yZGVyID0gKHsgd2lkdGggPSBcIjFweFwiLCBjb2xvciB9KSA9PiBjc3NgXG4gIGJveC1zaGFkb3c6IGluc2V0IDBweCAwcHggMHB4ICR7d2lkdGh9ICR7Y29sb3J9O1xuYDtcblxuY29uc3QgU3R5bGVkSW5wdXQgPSBzdHlsZWQuaW5wdXRgXG4gICR7c2hhZG93Qm9yZGVyKHsgY29sb3I6IFwicmVkXCIsIHdpZHRoOiBcIjRweFwiIH0pfVxuYDtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFpQm1EIn0= */");
13+
const shadowBorder = ({ width = "1px", color })=>/*#__PURE__*/ css("box-shadow:inset 0px 0px 0px ", width, " ", color, ";", "label:shadowBorder", "/*# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5wdXQudHMiLCJzb3VyY2VzIjpbImlucHV0LnRzIl0sInNvdXJjZXNDb250ZW50IjpbImltcG9ydCB7IGNzcyB9IGZyb20gXCJAZW1vdGlvbi9yZWFjdFwiO1xuaW1wb3J0IHN0eWxlZCBmcm9tIFwiQGVtb3Rpb24vc3R5bGVkXCI7XG5cbmNvbnN0IHVuaXROb3JtYWwgPSBcIjFyZW1cIjtcbmNvbnN0IHVuaXRMYXJnZSA9IFwiMnJlbVwiO1xuXG5jb25zdCBFeGFtcGxlID0gc3R5bGVkLmRpdmBcbiAgbWFyZ2luOiAke3VuaXROb3JtYWx9ICR7dW5pdExhcmdlfTtcbmA7XG5cbmV4cG9ydCBjb25zdCBBbmltYXRlZCA9IHN0eWxlZC5kaXZgXG4gICYgY29kZSB7XG4gICAgYmFja2dyb3VuZC1jb2xvcjogbGluZW47XG4gIH1cbiAgYW5pbWF0aW9uOiAkeyh7IGFuaW1hdGlvbiB9KSA9PiBhbmltYXRpb259IDAuMnMgaW5maW5pdGUgZWFzZS1pbi1vdXQgYWx0ZXJuYXRlO1xuYDtcblxuY29uc3Qgc2hhZG93Qm9yZGVyID0gKHsgd2lkdGggPSBcIjFweFwiLCBjb2xvciB9KSA9PiBjc3NgXG4gIGJveC1zaGFkb3c6IGluc2V0IDBweCAwcHggMHB4ICR7d2lkdGh9ICR7Y29sb3J9O1xuYDtcblxuY29uc3QgU3R5bGVkSW5wdXQgPSBzdHlsZWQuaW5wdXRgXG4gICR7c2hhZG93Qm9yZGVyKHsgY29sb3I6IFwicmVkXCIsIHdpZHRoOiBcIjRweFwiIH0pfVxuYDtcbiJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFpQm1EIn0= */");
1414
const StyledInput = /*#__PURE__*/ styled("input", {
1515
target: "e1xgaop22",
1616
label: "StyledInput"

0 commit comments

Comments
 (0)