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

Create CJS build output, or expect adapters to bundle the ESM themselves? #503

Closed
Rich-Harris opened this issue Mar 12, 2021 · 5 comments
Closed
Labels
adapters - general Support for functionality general to all adapters

Comments

@Rich-Harris
Copy link
Member

From #485 (comment):

I almost wonder if adapters should be responsible for bundling the outputted ESM (this would also solve the question of what needs to be considered external during build, so that adapters that target lambda can create a single CJS file.

Alternatively maybe Vite could output both ESM and CJS from build.

tl;dr most lambda platforms seem to expect CJS, even though Node itself runs ESM natively. The best outcome would probably be if those adapters had access to a single CJS file with no external dependencies (since the platform's bundlers don't always work as you'd hope), though the possibility of native modules in the dep graph makes this a bit of a challenge. So exporting CJS from Vite alongside the ESM is probably the easiest solution, even if it's a bit wasteful.

We could eliminate the waste if the adapter was able to declare its requirements to build.

@GrygrFlzr
Copy link
Member

Specifically for Vercel:

Currently [non-lambda] ESM is compiled to CJS (import changed to require) but we plan to add support for ESM proper once Node.js 10 goes EOL
We don't support ESM in functions at this time
We will soon though, I'll let you know once we do

Node 10 EOL is April 30th, 2020, so at least that's one adapter where going full ESM will be possible. Not sure about the other serverless platforms or something more exotic like Cloudflare Workers. This was part of why I decided to go with the current "CJS entrypoint, ESM everything else" approach - it's practical, it works now instead of waiting on the platforms to update, and it's easy to rip out the CJS entrypoint once we can migrate it to full ESM.

The current requirement to bundle all SSR dependencies for serverless functions is also a bit unfortunate, as it means the file is just one giant blob of JS that takes longer and longer to execute the more SSR dependencies you include, instead of importing the ones you need at runtime. That's also something dependent on the platforms to support, though.

@thenikso
Copy link

in an experiment to adapt to AWS lambdas I ended up building to CJS directly in the output directory. Not the cleanest solution.

Probably allowing an adapter to have full access to the rollup settings used to build the app would clear all possible future cases.

@nhe23

This comment has been minimized.

@babichjacob

This comment has been minimized.

@Rich-Harris
Copy link
Member Author

See also: #612

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters - general Support for functionality general to all adapters
Projects
None yet
Development

No branches or pull requests

6 participants