Description
Panda fails to extract styles when a css prop is passed an inline object literal in compiled (dist) files. The same pattern is extracted correctly from source files.
This was discovered while using Panda to tree-shake CSS across both local source files and compiled library dist files (e.g. component libraries from a design system).
When JSX is compiled, <Box css={{ color: 'RebeccaPurple' }} /> becomes a jsx() function call. Panda's static analysis appears to handle the JSX form correctly, but does not recognize the inline object literal in the compiled form.
| Pattern |
Source |
Dist |
Inline object literal css={{ color: '...' }} |
✅ Extracted |
❌ Not extracted |
Pre-defined variable css={styles} |
✅ Extracted |
✅ Extracted |
Spread of pre-defined variable css={{ ...spreadStyles }} |
✅ Extracted |
✅ Extracted |
Link to Reproduction
https://stackblitz.com/edit/vitejs-vite-4pyiuwzk?file=src%2FApp.tsx&terminal=dev
Steps to reproduce
Source file (works):
import type { ReactNode } from 'react';
import { css, cx } from '@/styled-system/css';
import type { SystemStyleObject } from '@/styled-system/types';
interface BoxProps {
children?: ReactNode;
css?: SystemStyleObject;
}
const Box = (props: BoxProps) => {
const { css: cssProp, children } = props;
return <div className={cx(css(cssProp))}>{children}</div>;
};
const styles = css.raw({
color: 'white',
backgroundColor: 'AntiqueWhite',
});
const spreadStyles = css.raw({
color: 'white',
backgroundColor: 'SteelBlue',
});
export const CustomComponent = () => {
return (
<>
{/* Panda CAN extract from source, but CANNOT from dist */}
<Box css={{ color: 'black', backgroundColor: 'RebeccaPurple' }}>Box</Box>
{/* Panda CAN extract from source and dist */}
<Box css={styles}>Box</Box>
<Box css={{ ...spreadStyles }}>Box</Box>
</>
);
};
Compiled dist file (broken):
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { css, cx } from "@/styled-system/css";
const Box = (props)=>{
const { css: cssProp, children } = props;
return /*#__PURE__*/ jsx("div", {
className: cx(css(cssProp)),
children: children
});
};
const styles = css.raw({
color: 'white',
backgroundColor: 'AntiqueWhite'
});
const spreadStyles = css.raw({
color: 'white',
backgroundColor: 'SteelBlue'
});
const CustomComponent = ()=>{
return /*#__PURE__*/ jsxs(Fragment, {
children: [
/*#__PURE__*/ jsx(Box, {
css: {
color: 'black',
backgroundColor: 'RebeccaPurple'
},
children: "Box"
}),
/*#__PURE__*/ jsx(Box, {
css: styles,
children: "Box"
}),
/*#__PURE__*/ jsx(Box, {
css: {
...spreadStyles
},
children: "Box"
})
]
});
};
export { CustomComponent };
Panda should extract styles from inline object literals passed to the css prop regardless of whether the file is a source .tsx file or a compiled .js dist file.
JS Framework
React (TS)
Panda CSS Version
1.8.2
Browser
No response
Operating System
Additional Information
No response
Description
Panda fails to extract styles when a
cssprop is passed an inline object literal in compiled (dist) files. The same pattern is extracted correctly from source files.This was discovered while using Panda to tree-shake CSS across both local source files and compiled library dist files (e.g. component libraries from a design system).
When JSX is compiled,
<Box css={{ color: 'RebeccaPurple' }} />becomes ajsx()function call. Panda's static analysis appears to handle the JSX form correctly, but does not recognize the inline object literal in the compiled form.css={{ color: '...' }}css={styles}css={{ ...spreadStyles }}Link to Reproduction
https://stackblitz.com/edit/vitejs-vite-4pyiuwzk?file=src%2FApp.tsx&terminal=dev
Steps to reproduce
Source file (works):
Compiled dist file (broken):
Panda should extract styles from inline object literals passed to the
cssprop regardless of whether the file is a source.tsxfile or a compiled.jsdist file.JS Framework
React (TS)
Panda CSS Version
1.8.2
Browser
No response
Operating System
Additional Information
No response