@@ -95,14 +95,17 @@ function emitMinimalWrapper(args: {
9595 return result ;
9696}
9797
98+ type ExportInfo = { exportName : string ; isDefault : boolean } ;
99+
98100export function emitWrappers ( args : {
99101 root : Collection < any > ;
100102 j : any ;
101103 styledDecls : StyledDecl [ ] ;
102104 wrapperNames : Set < string > ;
103105 patternProp : ( keyName : string , valueId ?: any ) => any ;
106+ exportedComponents : Map < string , ExportInfo > ;
104107} ) : void {
105- const { root, j, styledDecls, wrapperNames, patternProp } = args ;
108+ const { root, j, styledDecls, wrapperNames, patternProp, exportedComponents } = args ;
106109
107110 const wrapperDecls = styledDecls . filter ( ( d ) => d . needsWrapperComponent ) ;
108111 if ( wrapperDecls . length === 0 ) {
@@ -886,6 +889,105 @@ export function emitWrappers(args: {
886889 ) ;
887890 }
888891
892+ // Simple exported styled components (styled.div without special features)
893+ // These are exported components that need wrapper generation to maintain exports.
894+ const simpleExportedIntrinsicWrappers = wrapperDecls . filter ( ( d ) => {
895+ if ( d . base . kind !== "intrinsic" ) {
896+ return false ;
897+ }
898+ // Skip if already handled by other wrapper categories
899+ if ( d . withConfig ?. displayName || d . withConfig ?. componentId ) {
900+ return false ;
901+ }
902+ if ( d . shouldForwardProp ) {
903+ return false ;
904+ }
905+ if ( d . enumVariant ) {
906+ return false ;
907+ }
908+ if ( d . siblingWrapper ) {
909+ return false ;
910+ }
911+ if ( d . attrWrapper ) {
912+ return false ;
913+ }
914+ const tagName = d . base . tagName ;
915+ // Skip specialized wrapper categories
916+ if ( tagName === "button" && wrapperNames . has ( d . localName ) ) {
917+ return false ;
918+ }
919+ if ( tagName === "input" || tagName === "a" ) {
920+ return false ;
921+ }
922+ return true ;
923+ } ) ;
924+
925+ for ( const d of simpleExportedIntrinsicWrappers ) {
926+ if ( d . base . kind !== "intrinsic" ) {
927+ continue ;
928+ }
929+ const tagName = d . base . tagName ;
930+ const styleArgs : any [ ] = [
931+ ...( d . extendsStyleKey
932+ ? [ j . memberExpression ( j . identifier ( "styles" ) , j . identifier ( d . extendsStyleKey ) ) ]
933+ : [ ] ) ,
934+ j . memberExpression ( j . identifier ( "styles" ) , j . identifier ( d . styleKey ) ) ,
935+ ] ;
936+
937+ emitted . push (
938+ ...emitMinimalWrapper ( {
939+ j,
940+ localName : d . localName ,
941+ tagName,
942+ styleArgs,
943+ destructureProps : [ ] ,
944+ displayName : undefined ,
945+ patternProp,
946+ } ) ,
947+ ) ;
948+ }
949+
950+ // Component wrappers (styled(Component)) - these wrap another component
951+ const componentWrappers = wrapperDecls . filter ( ( d ) => d . base . kind === "component" ) ;
952+
953+ for ( const d of componentWrappers ) {
954+ if ( d . base . kind !== "component" ) {
955+ continue ;
956+ }
957+ const wrappedComponent = d . base . ident ;
958+ const styleArgs : any [ ] = [
959+ ...( d . extendsStyleKey
960+ ? [ j . memberExpression ( j . identifier ( "styles" ) , j . identifier ( d . extendsStyleKey ) ) ]
961+ : [ ] ) ,
962+ j . memberExpression ( j . identifier ( "styles" ) , j . identifier ( d . styleKey ) ) ,
963+ ] ;
964+
965+ const propsId = j . identifier ( "props" ) ;
966+ const stylexPropsCall = j . callExpression (
967+ j . memberExpression ( j . identifier ( "stylex" ) , j . identifier ( "props" ) ) ,
968+ styleArgs ,
969+ ) ;
970+
971+ // Create: <WrappedComponent {...props} {...stylex.props(styles.key)} />
972+ const jsx = j . jsxElement (
973+ j . jsxOpeningElement (
974+ j . jsxIdentifier ( wrappedComponent ) ,
975+ [ j . jsxSpreadAttribute ( propsId ) , j . jsxSpreadAttribute ( stylexPropsCall ) ] ,
976+ true ,
977+ ) ,
978+ null ,
979+ [ ] ,
980+ ) ;
981+
982+ emitted . push (
983+ j . functionDeclaration (
984+ j . identifier ( d . localName ) ,
985+ [ propsId ] ,
986+ j . blockStatement ( [ j . returnStatement ( jsx as any ) ] ) ,
987+ ) ,
988+ ) ;
989+ }
990+
889991 if ( emitted . length > 0 ) {
890992 // Re-order emitted wrapper nodes to match `wrapperDecls` source order.
891993 const groups = new Map < string , any [ ] > ( ) ;
@@ -950,6 +1052,27 @@ export function emitWrappers(args: {
9501052 }
9511053 ordered . push ( ...restNodes ) ;
9521054
1055+ // Wrap function declarations in export statements for exported components
1056+ const wrappedOrdered = ordered . map ( ( node ) => {
1057+ if ( node ?. type !== "FunctionDeclaration" ) {
1058+ return node ;
1059+ }
1060+ const fnName = node . id ?. name ;
1061+ if ( ! fnName ) {
1062+ return node ;
1063+ }
1064+ const exportInfo = exportedComponents . get ( fnName ) ;
1065+ if ( ! exportInfo ) {
1066+ return node ;
1067+ }
1068+ if ( exportInfo . isDefault ) {
1069+ // Create: export default function X(...) { ... }
1070+ return j . exportDefaultDeclaration ( node ) ;
1071+ }
1072+ // Create: export function X(...) { ... }
1073+ return j . exportNamedDeclaration ( node , [ ] , null ) ;
1074+ } ) ;
1075+
9531076 root
9541077 . find ( j . VariableDeclaration )
9551078 . filter ( ( p : any ) =>
@@ -958,7 +1081,7 @@ export function emitWrappers(args: {
9581081 ) ,
9591082 )
9601083 . at ( 0 )
961- . insertAfter ( ordered ) ;
1084+ . insertAfter ( wrappedOrdered ) ;
9621085 }
9631086
9641087 if ( forceReactImport ) {
0 commit comments