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

fix: Support ESM in Node.js through conditional exports #3768

Conversation

steve-taylor
Copy link

This is an attempt to fix #3601. This fix uses conditional exports and is specifically to fix how Node.js, not bundlers, import styled components.

I'm leaving it as a draft for now, because it's not fully tested due to @emotion/is-prop-valid possibly suffering from the same issue and needing the same fix:

TypeError: i is not a function
    at file:///path/to/repo/node_modules/styled-components/dist/styled-components.esm.mjs:1:20617
    at x (file:///path/to/repo/node_modules/styled-components/dist/styled-components.esm.mjs:1:20841)
    at Wc (/path/to/repo/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:68:44)
    at Zc (/path/to/repo/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:73:362)
    at Z (/path/to/repo/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:76:89)
    at $c (/path/to/repo/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:78:98)
    at bd (/path/to/repo/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:77:404)
    at Z (/path/to/repo/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:76:217)
    at $c (/path/to/repo/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:78:98)
    at Zc (/path/to/repo/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:71:145)

i is the default export from @emotion/is-prop-valid.

Similarly, in my (legacy) v5 backport, I had this issue with @emotion/stylis:

TypeError: h is not a constructor
    at ae (file:///path/to/repo/node_modules/styled-components/dist/styled-components.esm.mjs:1:12214)
    at file:///path/to/repo/node_modules/styled-components/dist/styled-components.esm.mjs:1:13205
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:437:15)

h is the default export from @emotion/stylis.

When I get some time later in the week, I'll work my way through the affected Emotion packages and, if that ultimately fixes the problem, create additional PRs to support this one.

@steve-taylor
Copy link
Author

This is a deep rabbit hole. My Emotion PR was summarily closed in favour of a larger competing PR that needs some work and relies on a Preconstruct PR.

@wight554
Copy link

This is a deep rabbit hole. My Emotion PR was summarily closed in favour of a larger competing PR that needs some work and relies on a Preconstruct PR.

https://github.com/sheremet-va/dual-packaging might be helpful

@patsplat
Copy link

It appears that the larger competing PR was closed, recreated, and has now merged.

@patsplat
Copy link

I am another user impacted by this bug.

How can I help test this fix? What steps should I take to do so?

@dexbol
Copy link

dexbol commented Nov 15, 2022

Hope this PR would merge to v6.

@quantizor
Copy link
Contributor

Added "exports" to package json in the most recent beta that should help with this

@quantizor quantizor closed this Mar 2, 2023
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.

Provide better ESM support for importing styled in node
5 participants