Skip to content

css prop inline object literals not extracted from compiled (dist) files #3509

@kris-ellery

Description

@kris-ellery

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

  • macOS
  • Windows
  • Linux

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions