babel-plugin-twin icon indicating copy to clipboard operation
babel-plugin-twin copied to clipboard

Check if `tw` is used before importing

Open vjpr opened this issue 4 years ago • 1 comments

I have a babel preset I use across many packages. Some use twin and others don't.

Currently, when this plugin imports twin.macro even if you are in a file or package that doesn't use it.

A workaround is to conditionally include the plugin in overrides, but this becomes an unmaintainable mess.

I wonder if it could search the AST for uses of tw...or maybe this wouldn't be very performant.


Another idea would be to use read-pkg-up to find the package.json and check if twin.macro is declared. If so, then include it. This comes with its own fs performance issues.

vjpr avatar Jun 10 '21 16:06 vjpr

Great idea - this plugin should really check before adding the imports. I'd opt for visiting the tw nodes to determine whether to add the imports or not.

Regarding performance: From what I've seen with similar checks - it shouldn't be a worry.

ben-rogerson avatar Jun 10 '21 22:06 ben-rogerson