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

Awkward ES6 imports #2582

Closed
RobertSandiford opened this issue Dec 8, 2021 · 4 comments · Fixed by #3029
Closed

Awkward ES6 imports #2582

RobertSandiford opened this issue Dec 8, 2021 · 4 comments · Fixed by #3029
Labels

Comments

@RobertSandiford
Copy link

Current behavior:

Importing createCache from @emotion/cache in an ES6 environment (type: module in package.json) does not import the function, but an object with a default property is the function

import createCache from '@emotion/cache'

const cache =  createCache({
    key: 'css',
})

Output:

TypeError: createCache is not a function

package.json

{
  "name": "emotion-cache-es6-imports",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "type": "module",
  "dependencies": {
    "@emotion/cache": "^11.6.0"
  }
}

To reproduce:

Repro repo: https://github.com/RobertSandiford/emotion-cache-es6-imports

yarn
node index

Expected behavior:

The create cache function is exported to createCache

Other notes:

When packed with webpack the above code will work.

Getting the create cache function in ES6 requires accessing the default property.

This requires code like this to work server side + client side.

import emotionCache from '@emotion/cache'
const createCache = ('default' in emotionCache ) ? emotionCache.default : emotionCache

const cache = createCache({})

It's likely that this issues exists in other places too. I had issues with @emotion/server/create-instance too. Currently I have this code

import emotionServerCreateInstance from '@emotion/server/create-instance/dist/emotion-server-create-instance.cjs.js'
const { default: createEmotionServer } = emotionServerCreateInstance

Environment information:

  • @emotion/cache version: 11.6
  • Node: v16.13.0
@Andarist
Copy link
Member

Andarist commented Dec 8, 2021

We are aware of the "issue". The way how node has implemented support for modules creates a ripple effect on the whole ecosystem - what worked for years in other tools suddenly doesn't work in node.

We'll think about solutions for this, but the problem is that it's very hard to pull off a solution that would just work for every possible combination of environments and tools. And on top of that making changes around this might require a major bump.

@RobertSandiford
Copy link
Author

RobertSandiford commented Dec 8, 2021

@Andarist

Sure. I would try to help, but I don't know the details of how node handles it.

The way that @mui handles it is with an index.js file which acts as a giant barrel file, exporting everything for ES6. CJS is handled via "main" which points to a cjs file in a subdirectory. I don't know if that's helpful to you.

@srmagura srmagura added the bug label Mar 23, 2022
@rtritto
Copy link

rtritto commented Apr 14, 2022

Any update?

@eagleera
Copy link

eagleera commented Jan 6, 2023

any update on this?

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

Successfully merging a pull request may close this issue.

5 participants