Skip to content

Commit dbb0c77

Browse files
committed
fix: skip static optimization for member bases
1 parent f4c18e5 commit dbb0c77

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

packages/babel-plugin-kstyled/src/__tests__/transform.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ describe('babel-plugin-kstyled', () => {
138138
expect(output).not.toContain('paddingVertical');
139139
expect(output).not.toContain('paddingHorizontal');
140140
});
141+
142+
test('should skip static optimization for member expressions', () => {
143+
const input = `
144+
import { styled } from 'kstyled';
145+
import { Typography } from './typography';
146+
147+
const SectionTitle = styled(Typography.Body2)\`
148+
font-weight: 700;
149+
\`;
150+
`;
151+
152+
const output = transform(input);
153+
154+
expect(output).toContain('styled(Typography.Body2).__withStyles');
155+
expect(output).not.toContain('forwardRef');
156+
});
141157
});
142158

143159
describe('Dynamic Style Generation', () => {

packages/babel-plugin-kstyled/src/index.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -560,23 +560,29 @@ export default function babelPluginKStyled(): PluginObj<PluginState> {
560560
// generate a lightweight component directly without styled() wrapper
561561
const isStaticOnly = dynamicProps.length === 0 && !attrsMetadata;
562562

563+
const baseComponent = styledCall.arguments[0];
564+
const canOptimizeBaseComponent = t.isIdentifier(baseComponent);
565+
563566
if (opts.debug) {
564567
console.log('[babel-plugin-kstyled] Optimization check:');
565568
console.log(' isStaticOnly:', isStaticOnly);
566569
console.log(' styleSheetNode exists:', !!styleSheetNode);
567570
console.log(' opts.optimizeStatic:', opts.optimizeStatic);
568571
console.log(' dynamicProps.length:', dynamicProps.length);
569572
console.log(' attrsMetadata:', !!attrsMetadata);
573+
console.log(' canOptimizeBaseComponent:', canOptimizeBaseComponent);
570574
}
571575

572-
if (isStaticOnly && styleSheetNode && opts.optimizeStatic !== false) {
576+
if (
577+
isStaticOnly &&
578+
styleSheetNode &&
579+
opts.optimizeStatic !== false &&
580+
canOptimizeBaseComponent
581+
) {
573582
// Import forwardRef from React
574583
const program = path.findParent((p) => p.isProgram()) as NodePath<t.Program>;
575584
const forwardRefImport = addNamed(program, 'forwardRef', 'react');
576585

577-
// Get the base component from styled(Component)
578-
const baseComponent = styledCall.arguments[0];
579-
580586
// Generate ultra-optimized JSX:
581587
// forwardRef((props, ref) => (
582588
// <Component {...props} ref={ref} style={props.style ? [props.style, __ks.base] : __ks.base} />
@@ -585,15 +591,8 @@ export default function babelPluginKStyled(): PluginObj<PluginState> {
585591
// No memo: styled-components doesn't use memo either, so fair comparison
586592
// JSX spread: Let React handle prop spreading efficiently
587593

588-
// Get component name for JSX
589-
let componentName = 'View';
590-
if (t.isIdentifier(baseComponent)) {
591-
componentName = baseComponent.name;
592-
} else if (t.isMemberExpression(baseComponent)) {
593-
if (t.isIdentifier(baseComponent.property)) {
594-
componentName = baseComponent.property.name;
595-
}
596-
}
594+
// Base component is guaranteed to be an Identifier here
595+
const componentName = (baseComponent as t.Identifier).name;
597596

598597
// Create style expression: props.style ? [__ks.base, props.style] : __ks.base
599598
// IMPORTANT: Base styles first, external styles last (external overrides base)

0 commit comments

Comments
 (0)