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

Entry file's export isn't exported #29

Closed
ravener opened this issue Feb 20, 2020 · 3 comments
Closed

Entry file's export isn't exported #29

ravener opened this issue Feb 20, 2020 · 3 comments

Comments

@ravener
Copy link

ravener commented Feb 20, 2020

Using ES6 Modules with main.js

import lib from "./lib.js"

console.log(lib());

export default () => "main";

lib.js

export default () => "Hello, World!";

Using command esbuild --bundle --outfile=bundle.js main.js

Then loading the bundle.js file in Node.js i see the Hello World being printed so lib.js got imported successfully however the main.js also exported a function that i cannot use from Node.js it should've been exported. Cannot reproduce this with rollup.

(Another comment: the bundle is pretty big compared to what rollup produces for the code i mentioned do we really need all that stuff?)

@ravener
Copy link
Author

ravener commented Feb 24, 2020

@evanw any ideas about this? I also can't use react with multiple files it generates broken code for the importing logic.

@evanw evanw closed this as completed in 22aae6a Apr 18, 2020
@evanw
Copy link
Owner

evanw commented Apr 18, 2020

the main.js also exported a function that i cannot use from Node.js it should've been exported.

The reason why this wasn't working before was because esbuild actually generated output in the IIFE (immediately-invoked function expression) format, not the CommonJS format that node uses, since I've been focusing on running code in the browser.

I added a new --format flag to control the output format and added support for the CommonJS output format. You can enable that with --format=cjs. However, if you want this to run in node you'll probably want to use --platform=node which automatically implies --format=cjs. Bundling with --platform=node enables referencing builtin node libraries and if you use any node modules that have both browser and node variants, switches esbuild over to bundling the node variants.

(Another comment: the bundle is pretty big compared to what rollup produces for the code i mentioned do we really need all that stuff?)

I assume you're talking about the extra module loading code esbuild sticks at the top. No, that stuff isn't necessarily needed. I'm trying to have esbuild support bundling both ES6 and CommonJS modules instead of something like Rollup, which only natively supports ES6 modules. That extra stuff is for CommonJS support.

I'm working towards a state where that extra stuff is stripped out when it's not needed. Believe me, it bugs me too. But I haven't yet done the additional work to detect when it's not needed and strip it away. I'll definitely get to this at some point.

(Mostly as a reminder for me later) I recently made some progress towards this state by rearranging how the bundle is structured so that helper code is now in multiple top-level functions instead of in a single multi-purpose function nested inside a closure. The next step is to detect which of those helper functions are unused and strip the unused ones away, which will only leave the ones that are actually needed, if any. I also need to detect if all code reachable from the entry point is valid ES6 and, if so, skip embedding the code inside a CommonJS module map.

@amatiasq-heydoc
Copy link

I tried --platform=node and didn't work but explicit --format=cjs did the job, thanks!

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

3 participants