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 ESM module support #3385

Closed
jfparadis opened this issue Feb 12, 2020 · 3 comments · Fixed by #3391
Closed

Native ESM module support #3385

jfparadis opened this issue Feb 12, 2020 · 3 comments · Fixed by #3391

Comments

@jfparadis
Copy link

Module support in node is still in early stages (and maybe not ready for runtime), but there are benefits to use it in a tool chain, and to begin creating packages with native support:

  • to get the native behavior of JS in tests
  • to avoid caching issues with esm transpilation

Numerous options are possible as documented here:
https://github.com/nodejs/node/blob/master/doc/api/esm.md#approach-1-use-an-es-module-wrapper

Expected Behavior

This form should be usable from a package with native ESM support:

import { rollup } from 'rollup';

As verifiable via a simple command-line test:

node  --input-type=module -e "import { rollup } from 'rollup'; console.log(typeof rollup)"
function

Or as verifiable in context of the test package provided:

git clone https://github.com/jfparadis/rollup-esm-test.git
cd rollup-esm-test
npm install

npm test

# rollup
ok 1 should be equal

1..1
# tests 1
# pass  1

# ok

Actual Behavior

Error, as verifiable via a simple command-line test:

node  --input-type=module -e "import { rollup } from 'rollup';"
import { rollup } from 'rollup';
         ^^^^^^
SyntaxError: The requested module 'rollup' does not provide an export named 'rollup'

Or as verifiable in the context of the test package provided:

git clone https://github.com/jfparadis/rollup-esm-test.git
cd rollup-esm-test
npm install
npm test
import { rollup } from 'rollup';
         ^^^^^^
SyntaxError: The requested module 'rollup' does not provide an export named 'rollup'

Workaround

The workaround is to use a wrapper or an alternate syntax to resolve the exports:

import cjsRollup from 'rollup';
const { rollup } = cjsRollup;

As verifiable via a simple command-line test:

node  --input-type=module -e "import cjsRollup from 'rollup'; const { rollup } = cjsRollup; console.log(typeof rollup)"
function

Or as verifiable the in context of the test package provided, by commenting and uncommenting a few lines in test.js.

@lukastaegert
Copy link
Member

Approach 2 https://github.com/nodejs/node/blob/master/doc/api/esm.md#approach-2-isolate-state actually sounds like a reasonable way forward. I would like to avoid importing CJS parts in the ESM parts of Rollup to provide an optimized build for JS API consumers and re-bundlers of Rollup, and I think it is reasonable to consider Rollup to be stateless enough. We could do this for Rollup 2. If it works correctly on Node 10, we are good to go.

@lukastaegert lukastaegert added this to To do in Release 2.0.0 via automation Feb 12, 2020
@lukastaegert
Copy link
Member

Ok, I really would love to support this but at the moment, acorn is being a huge pain here: acornjs/acorn#824 (comment)

Basically I need to rewrite my code several times to use named imports for the browser build but default imports for the Node builds to make sure even the ESM version of Rollup pulls in the CJS version of acorn.

@lukastaegert lukastaegert moved this from To do to In progress in Release 2.0.0 Feb 19, 2020
@lukastaegert lukastaegert moved this from In progress to Ready for merge in Release 2.0.0 Feb 19, 2020
@lukastaegert lukastaegert moved this from Ready for merge to Done in Release 2.0.0 Feb 19, 2020
@lukastaegert
Copy link
Member

Solved in rollup@2.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Release 2.0.0
  
Done
Development

Successfully merging a pull request may close this issue.

2 participants