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

refactor: replace import helper #726

Merged
merged 9 commits into from
Aug 5, 2021
Merged

Conversation

huozhi
Copy link
Member

@huozhi huozhi commented Aug 3, 2021

Use import expression injection to replace with @babel/helper-module-imports

Currently styled-jsx has inconsistence behavior between styled-jsx/style and styled-jsx/css imports.

addDefault helper will emit styled-jsx/style code based on babel sourceType, but resolve tags imported from styled-jsx/css aren't handled if it's in sourceType="script" mode

For example, the code below won't be transpiled

const css = require('styled-jsx/css')

exports.normal = css.resolved`p { color: blue }`
exports.resolved = css`p { color: red }`

But any <style jsx> usage in commonjs module will be transpiled and inject the imports/require.

Import declarations can be easily captured in AST traversal, but require expressions (w/ or w/o module interop) is hard to be captured. Such asstyled-jsx/css is using this way to transpile css related usages.

It's better to only support ESM transform and let other plugin for example (commonjs plugin) to handle the ESM to CJS transform.

Suggested Usage

Use ESModule syntax for jsx styles. If you want to transformed to CJS, use commonjs plugin

@huozhi huozhi changed the title enhance: replace import helper feat: replace import helper Aug 4, 2021
@huozhi huozhi changed the title feat: replace import helper refactor: replace import helper Aug 4, 2021
@huozhi huozhi marked this pull request as ready for review August 4, 2021 11:47
@huozhi huozhi requested a review from giuseppeg as a code owner August 4, 2021 11:47
src/babel.js Outdated Show resolved Hide resolved
@huozhi huozhi requested a review from giuseppeg August 5, 2021 07:19
@huozhi huozhi merged commit de67aea into vercel:master Aug 5, 2021
@huozhi huozhi deleted the replace-import-helper branch August 5, 2021 14:10
@github-actions
Copy link

github-actions bot commented Aug 5, 2021

🎉 This PR is included in version 3.4.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

TrySound pushed a commit to TrySound/styled-jsx that referenced this pull request Aug 6, 2021
* enhance: use raw imports injection

* rm cjs test

* remove import path if unused

* fix lint

* let cjs plugin handle imports

* update snapshots

* more test as example

* only inject at exit
@github-actions
Copy link

github-actions bot commented Aug 6, 2021

🎉 This PR is included in version 4.0.0-alpha.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants