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

Native ES modules #1040

Closed
wants to merge 5 commits into from
Closed

Native ES modules #1040

wants to merge 5 commits into from

Conversation

matt-allan
Copy link
Contributor

@matt-allan matt-allan commented Jun 28, 2021

This PR converts the Radiance repo over to native ES modules. Every supported version of Node supports native ES modules so we don't need to use Common JS anymore.

By default every .js file will now be parsed as an ES module. If you need the file to be parsed as a CommonJS module (i.e. because you're using a package that only supports CJS) you can use the .cjs extension.

We can't drop the CommonJS bundle yet because the @emotion/styled package is not fully setup for ES modules yet and breaks when you try to import @emotion/styled/base. You can repro it with this script:

import { Button } from './dist/bundle-es/index.js';

console.log(Button);
$ node testing.js
node:internal/process/esm_loader:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '/Users/matt/code/radiance-ui/node_modules/@emotion/styled/base' is not supported resolving ES modules imported from /Users/matt/code/radiance-ui/dist/bundle-es/shared-components/typography/index.js
Did you mean to import @emotion/styled/base/dist/emotion-styled-base.cjs.js?

In the interim if you need to use Radiance components from node you still have to use Common JS:

const radiance = require('./dist/bundle.cjs');

const { Button } = radiance;

console.log(Button);
$ node testing.cjs
[Function: Button] {
  Container: <ref *1> [Function (anonymous)] {
    displayName: 'ButtonContainer',
    defaultProps: undefined,
    __emotion_real: [Circular *1],
    __emotion_base: 'div',
    __emotion_styles: [
      'label:ButtonContainer;',
   // ...

Bundlewatch can't load native ES modules and we don't need this
to be a JS file, so converting it to JSON allows us to switch to
native ES modules.
This will allow us to drop CommonJS builds and use the same ES module
build in Node, the browser, etc. Node.JS 10 was the last version that
didn't support ES modules and it's not obsolete.
We don't need this anymore since Node can load the ES build.
@matt-allan
Copy link
Contributor Author

Opened emotion-js/emotion#2413 for the Emotion issue. I don't think we can fix it on our end.

@emotion/styled isn't compatible with native ES modules yet. We will
need to continue supporting CommonJS until it's possible to load
Radiance components in Node with native ES modules. This doesn't
preclude us from using native ES modules ourselves.

See emotion-js/emotion#2413.
@matt-allan
Copy link
Contributor Author

Going to close this until we are unblocked by Emotion.

@matt-allan matt-allan closed this Jun 28, 2021
@matt-allan matt-allan deleted the es-modules branch June 28, 2021 19:47
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

1 participant