Skip to content

Commit 7a0a171

Browse files
committed
fix: only remove tw/twStyle imports when transformations succeed
Track successful transformations instead of just import detection. Previously, imports were removed even when tw calls with interpolations or twStyle calls with dynamic values were skipped, causing runtime ReferenceError. Now hasTwImport is only set to true after successful transformation, ensuring imports are preserved when original expressions remain.
1 parent db8158f commit 7a0a171

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/babel/plugin.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ export default function reactNativeTailwindBabelPlugin(
398398
// Track the local name (could be renamed: import { tw as customTw })
399399
const localName = spec.local.name;
400400
state.twImportNames.add(localName);
401-
state.hasTwImport = true;
401+
// Don't set hasTwImport yet - only set it when we successfully transform a call
402402
}
403403
}
404404
});
@@ -443,6 +443,8 @@ export default function reactNativeTailwindBabelPlugin(
443443
path.replaceWith(
444444
t.objectExpression([t.objectProperty(t.identifier("style"), t.objectExpression([]))]),
445445
);
446+
// Mark as successfully transformed (even if empty)
447+
state.hasTwImport = true;
446448
return;
447449
}
448450

@@ -459,6 +461,9 @@ export default function reactNativeTailwindBabelPlugin(
459461
findComponentScope,
460462
t,
461463
);
464+
465+
// Mark as successfully transformed
466+
state.hasTwImport = true;
462467
},
463468

464469
// Handle twStyle('...') call expressions
@@ -502,6 +507,8 @@ export default function reactNativeTailwindBabelPlugin(
502507
if (!className) {
503508
// Replace with undefined
504509
path.replaceWith(t.identifier("undefined"));
510+
// Mark as successfully transformed (even if empty)
511+
state.hasTwImport = true;
505512
return;
506513
}
507514

@@ -518,6 +525,9 @@ export default function reactNativeTailwindBabelPlugin(
518525
findComponentScope,
519526
t,
520527
);
528+
529+
// Mark as successfully transformed
530+
state.hasTwImport = true;
521531
},
522532

523533
JSXAttribute(path, state) {

0 commit comments

Comments
 (0)