Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error if defined after the styled-components plugin #5

Merged
merged 2 commits into from
Jun 19, 2019

Conversation

TrevorBurnham
Copy link
Owner

When the styled-components Babel plugin runs, it replaces TaggedTemplateExpressions with CallExpressions, making this plugin a no-op. The README briefly mentions this ordering issue:

Add the plugin to your Babel configuration before the styled-components plugin…

But enforcing that directive through code is much better! 😁

Copy link

@nvanselow nvanselow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! I very much agree that codifying these types of errors is much better than only documentation. Great find on that pre API.

'`styled-components` plugin'
);
}
};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simplify this down by using find instead of forEach. A side effect of that would be a slightly optimized search as we can abort as soon as we find this plugin and it is before the styled-components plugin.

E.g., (psuedo-code ish)

let styledComponentsPluginIndex = -1;
plugins.find(([plugin], index) => {
   if(plugin.key === 'styled-components') {
     styledComponentsPluginIndex = index;
     return false;
  }

  if(plugin.pre === pre) {
    if(styledComponentsPluginIndex >= 0) {
     throw new Error('...');
   }
   
   return true;
  }
});

Although, seeing that written out, your original may be slightly more readable. It's a toss up, so either way.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, using find makes sense: 4a6a656

@TrevorBurnham TrevorBurnham merged commit 70ec8bc into master Jun 19, 2019
@TrevorBurnham TrevorBurnham deleted the throw-if-after-styled-components-plugin branch June 19, 2019 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants