From 17c450e7eb45764c8ea5039ebe7e12b20c09299f Mon Sep 17 00:00:00 2001 From: Mark Lawlor Date: Tue, 31 May 2022 10:32:44 +1000 Subject: [PATCH] fix: always try to extract styles --- __tests__/styled/__snapshots__/props.tsx.snap | 16 +++++++++++ __tests__/styled/props.tsx | 27 ++++++++++++++++++- src/babel/root-visitor.ts | 7 ----- src/babel/visitor.ts | 6 ++--- 4 files changed, 44 insertions(+), 12 deletions(-) diff --git a/__tests__/styled/__snapshots__/props.tsx.snap b/__tests__/styled/__snapshots__/props.tsx.snap index 86c9e3a..78774d0 100644 --- a/__tests__/styled/__snapshots__/props.tsx.snap +++ b/__tests__/styled/__snapshots__/props.tsx.snap @@ -30,6 +30,22 @@ exports[`Styled - Base Class Name can set base classNames 1`] = ` /> `; +exports[`Styled - Default props can render with default props 1`] = ` + +`; + exports[`Styled - Props can style custom props 1`] = ` Array [ { expect(tree).toMatchSnapshot(); }); }); + +describe("Styled - Default props", () => { + const StyledText = styled(Text, { + baseClassName: "p-4", + }); + StyledText.defaultProps = { + accessibilityRole: "header", + }; + + test("can render with default props", () => { + const tree = render( + + + + ).toJSON(); + + expect(tree).toMatchSnapshot(); + }); +}); diff --git a/src/babel/root-visitor.ts b/src/babel/root-visitor.ts index 867dcbc..ca5ac43 100644 --- a/src/babel/root-visitor.ts +++ b/src/babel/root-visitor.ts @@ -70,7 +70,6 @@ export default function rootVisitor( mode: "compileAndTransform", blockModuleTransform: [], hasStyledComponentImport: false, - hasClassNames: false, canCompile, canTransform, ...state, @@ -92,16 +91,10 @@ export default function rootVisitor( hasStyleSheetImport, hasProvider, hasStyledComponentImport, - hasClassNames, hmr, } = visitorState; if (hmr) { - // There are no classNames so skip this file - if (!hasClassNames) { - return; - } - /** * Override tailwind to only process the classnames in this file */ diff --git a/src/babel/visitor.ts b/src/babel/visitor.ts index 9d87dc7..98fe31f 100644 --- a/src/babel/visitor.ts +++ b/src/babel/visitor.ts @@ -20,7 +20,6 @@ export interface VisitorState cwd: string; allowRelativeModules: AllowPathOptions; blockList: Set; - hasClassNames: boolean; hasStyledComponentImport: boolean; hasProvider: boolean; hasStyleSheetImport: boolean; @@ -63,9 +62,8 @@ export const visitor: Visitor = { return; } - if (someAttributes(path, ["className", "tw", "fill", "stroke"])) { - if (canTransform) toStyledComponent(path); - state.hasClassNames = true; + if (someAttributes(path, ["className", "tw"]) && canTransform) { + toStyledComponent(path); } }, };