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

@emotion/babel-plugin is broken under module imports #2973

Closed
otakustay opened this issue Jan 22, 2023 · 0 comments · Fixed by #3029
Closed

@emotion/babel-plugin is broken under module imports #2973

otakustay opened this issue Jan 22, 2023 · 0 comments · Fixed by #3029

Comments

@otakustay
Copy link
Contributor

otakustay commented Jan 22, 2023

Current behavior:

When an ESM compatible module resolver loads @emotion/babel-plugin, it resolves to emotion-babel-plugin.esm.js, which fails to use createMacro function because it is undefined in ESM imports.

TypeError: createMacro is not a function
 ❯ createTransformerMacro node_modules/@emotion/babel-plugin/dist/emotion-babel-plugin.esm.js:754:38
    752|
    753|     if (/\/macro$/.test(source)) {
    754|       path.get('source').replaceWith(babel.types.stringLiteral(source.replace(/\/macro$/, '')));
       |                                      ^
    755|     }
    756|
 ❯ node_modules/@emotion/babel-plugin/dist/emotion-babel-plugin.esm.js:1111:17
 ❯ index.test.js:3:31

To reproduce:

  1. Clone https://github.com/otakustay/issues: git clone git@github.com:otakustay/issues.git
  2. Switch to emotion-babel-plugin-macro branch: git checkout emotion-babel-plugin-macro
  3. Install dependencies: npm install
  4. Run test: vitest run

Vitest (vite as its core) will recognize module field in package.json of @emotion/babel-plugin and resolves to
emotion-babel-plugin.esm.js, in this case import { createMacro } from 'babel-plugin-macros'; will results undefined, this should be something like this:

import macros from 'babel-plugin-macros';
const {createMacro} = macros.default;

Expected behavior:

The test should pass

Environment information:

  • react version: No react is required in this case
  • @emotion/react version: 11.10.5
@otakustay otakustay changed the title @emotion/babel-plugin is broken under ESM environment @emotion/babel-plugin is broken under module imports Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant