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

Bug w/ named exports #1469

Closed
o-alexandrov opened this issue Jul 24, 2021 · 5 comments
Closed

Bug w/ named exports #1469

o-alexandrov opened this issue Jul 24, 2021 · 5 comments

Comments

@o-alexandrov
Copy link

Please find a reproduction of this bug here:

The complete code sample there is 2 files:

// named-exports.js
export const a = "a";
export const b = "b";
// index.js
import * as namedExports from "./named-exports";

console.log(namedExports);
console.log(namedExports.default);

There are two npm scripts: nodejs & esbuild.
When you run npm run nodejs, you'd see the following output in the console:

[Module] { a: 'a', b: 'b' }     // namedExports
undefined                           // namedExports.default

whereas when you run npm run esbuild, the CLI output is:

  • note there's the unexpected default value
{ default: { a: [Getter], b: [Getter] }, a: [Getter], b: [Getter] }
{ a: [Getter], b: [Getter] }

As you could see above, esbuild provides a different output than plain Node.js either with esm to transpile JS, or by running against .mjs files.

Originally posted by @o-alexandrov in #532 (comment)

@hyrious
Copy link

hyrious commented Jul 24, 2021

This is because your code is actually transformed to CJS format independently by esbuild-register.
The __toModule helper method in the result makes the unknown format named-exports.js have a default prop. You may ask esbuild-register to do something like esm register to transform your code to esm and load them.

You may search the source code to see why it happens.

In fact, if you can bundle them all and run instead of let esbuild-register transform each file, the result will be the same as npm run nodejs. I've written another tool to do such thing.

@o-alexandrov
Copy link
Author

o-alexandrov commented Jul 24, 2021

@hyrious thank you, yes, you are correct; the problem is with esbuild-register.

  • I should have checked the esbuild --bundle output, added an additional npm script to the reproduction

I tried the tool you created @hyrious/esbuild-dev and it doesn't have this issue. I'll be using it, thank you very much!

On a side note, imho, the only missing feature in the tool you created is it needs a predefined script (with --cjs flag enabled) to be used as a module for node.js preloading

  • node -r ...otherPreloadedModules -r @hyrious/esbuild-dev src/example.js would fail in the linked repro

@hyrious
Copy link

hyrious commented Jul 24, 2021

On a side note, imho, the only missing feature in the tool you created is it needs a predefined script (with --cjs flag enabled) to be used as a module for node.js preloading

The -r usage is what registers (esbuild-register, esm, ...) expect. However, I don't want to use a deprecated feature as esbuild-dev's readme said.

Maybe you will be interested in creating such register by yourself.

@hyrious
Copy link

hyrious commented Jul 24, 2021

@o-alexandrov I found another transformer loads esm, maybe you could give it a try: https://github.com/antfu/esbuild-node-loader

@o-alexandrov
Copy link
Author

@hyrious thank you, I need the preloaded module to return cjs, so esbuild-node-loader doesn't work for me

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

No branches or pull requests

2 participants