Skip to content

Commit cceffda

Browse files
Fix the case when the styles are defined below the component and the … (#1829)
* Fix the case when the styles are defined below the component and the component uses xcss prop * Changeset --------- Co-authored-by: Liam Ma <[email protected]>
1 parent 28bf423 commit cceffda

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

.changeset/wild-numbers-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compiled/babel-plugin': patch
3+
---
4+
5+
Resolve an issue where cssMap was being defined after its consumer, which relies on the xcss prop.

packages/babel-plugin/src/css-map/__tests__/index.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,37 @@ describe('css map basic functionality', () => {
3636
]);
3737
});
3838

39+
it('should transform css map when the styles are defined below the component and the component uses xcss prop', () => {
40+
const actual = transform(
41+
`
42+
import { cssMap } from '@compiled/react';
43+
44+
<Box xcss={styles.danger} />
45+
46+
const styles = cssMap(${styles});
47+
`,
48+
{ pretty: true }
49+
);
50+
51+
expect(actual).toMatchInlineSnapshot(`
52+
"import * as React from "react";
53+
import { ax, ix, CC, CS } from "@compiled/react/runtime";
54+
const _4 = "._bfhkbf54{background-color:green}";
55+
const _3 = "._syazbf54{color:green}";
56+
const _2 = "._bfhk5scu{background-color:red}";
57+
const _ = "._syaz5scu{color:red}";
58+
<CC>
59+
<CS>{[_, _2, _3, _4]}</CS>
60+
{<Box xcss={styles.danger} />}
61+
</CC>;
62+
const styles = {
63+
danger: "_syaz5scu _bfhk5scu",
64+
success: "_syazbf54 _bfhkbf54",
65+
};
66+
"
67+
`);
68+
});
69+
3970
it('should transform css map even when the styles are defined below the component', () => {
4071
const actual = transform(`
4172
import { cssMap } from '@compiled/react';

packages/babel-plugin/src/utils/css-builders.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ const extractObjectExpression = (node: t.ObjectExpression, meta: Metadata): CSSO
698698
*
699699
* @returns {Boolean} Whether the cache was generated
700700
*/
701-
const generateCacheForCSSMap = (node: t.Identifier, meta: Metadata): void => {
701+
export const generateCacheForCSSMap = (node: t.Identifier, meta: Metadata): void => {
702702
if (meta.state.cssMap[node.name] || meta.state.ignoreMemberExpressions[node.name]) {
703703
return;
704704
}

packages/babel-plugin/src/xcss-prop/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as t from '@babel/types';
44
import type { Metadata } from '../types';
55
import { buildCodeFrameError, getPathOfNode } from '../utils/ast';
66
import { compiledTemplate } from '../utils/build-compiled-component';
7-
import { buildCss } from '../utils/css-builders';
7+
import { buildCss, generateCacheForCSSMap } from '../utils/css-builders';
88
import { transformCssItems } from '../utils/transform-css-items';
99

1010
function getJsxAttributeExpressionContainer(path?: NodePath<t.JSXAttribute>) {
@@ -29,13 +29,15 @@ function staticObjectInvariant(expression: t.ObjectExpression, meta: Metadata) {
2929
);
3030
}
3131

32-
function collectPathMemberExpressionIdentifiers(propPath: NodePath<t.JSXAttribute>): string[] {
33-
const identifiers: string[] = [];
32+
function collectPathMemberExpressionIdentifiers(
33+
propPath: NodePath<t.JSXAttribute>
34+
): t.Identifier[] {
35+
const identifiers: t.Identifier[] = [];
3436

3537
propPath.traverse({
3638
MemberExpression(node) {
3739
if (node.node.object.type === 'Identifier') {
38-
identifiers.push(node.node.object.name);
40+
identifiers.push(node.node.object);
3941
}
4042
},
4143
});
@@ -112,7 +114,13 @@ export const visitXcssPropPath = (path: NodePath<t.JSXOpeningElement>, meta: Met
112114
// 1. Dot notation, such as "styles.text"
113115
// 2. Bracket notation, such as "styles[appearance]"
114116
const identifiers = collectPathMemberExpressionIdentifiers(propPath);
115-
const sheets = collectPassStyles(meta, identifiers);
117+
identifiers.forEach((identifier) => {
118+
generateCacheForCSSMap(identifier, meta);
119+
});
120+
const sheets = collectPassStyles(
121+
meta,
122+
identifiers.map((identifier) => identifier.name)
123+
);
116124

117125
if (sheets.length === 0) {
118126
// No sheets were extracted — bail out from the transform.

0 commit comments

Comments
 (0)