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

Support esm on node with conditional exports #3366

Closed

Conversation

sebamarynissen
Copy link

Since 13.6 node is able to load esm without the --experimental-modules flag, and since 13.7 node supports conditional exports unflagged as well.

While node can import cjs code from esm code just fine, currently only

import d3 from 'd3';

is possible. If you try

import { line } from 'd3';

you get

import { line } from 'd3';
         ^^^^
SyntaxError: The requested module 'd3' does not provide an export named 'line'

because Node doesn't support named exports from cjs modules.

I modified the build config to build an esm file for node as well and made sure node can find it using the conditional exports.

I am aware that the node version of d3 simply exports the individual d3-* modules whereas my build fully includes them in the code. I think that eventually the conditional exports should be used in each of the d3-* modules. I just wanted to add this as a PoC however to open up the discussion without going through all the work of implementing the conditional exports in each of the submodules.

@mbostock
Copy link
Member

mbostock commented Feb 4, 2020

Neat! The future is here. I’ll need to do some testing but this looks good at first glance.

@sebamarynissen
Copy link
Author

In the meantime it was discovered that we can silence the warning that conditional exports gives for require, see mrdoob/three.js#18498 (comment) and chaijs/chai#1317 (comment). I'll push a new version.

@kane2266

This comment was marked as spam.

@curran
Copy link
Contributor

curran commented Aug 27, 2020

Curious what remains to be done here?

@mbostock
Copy link
Member

Do we want to add type: module here, too? And can we use the existing ES module entry point for environments that support it?

  "main": "./dist/d3.node.js",
  "exports": {
    "import": "./index.js",
    "require": "./dist/d3.node.js"
  },
  "type": "module"

@sebamarynissen
Copy link
Author

sebamarynissen commented Aug 28, 2020

From memory, I think this would work except when you use named imports in versions of node that don't support conditional exports, but do support ES modules with the --experimental-modules flag. If I'm not mistaken node would in that case try to import the cjs build as ES module (as it ignores the exports field and hence uses the main field), which will result in an error.

Personally I think this acceptable though as it should work in all node versions where ES modules are unflagged.

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

Successfully merging this pull request may close these issues.

4 participants