opennextjs / opennextjs-aws

Open-source Next.js adapter for AWS
https://opennext.js.org
MIT License
4.13k stars 126 forks source link

CommonJS server function #367

Closed r34son closed 7 months ago

r34son commented 8 months ago

Can i somehow have my handler be bundled in commonjs format?

r34son commented 8 months ago

@khuezy @conico974 Could you help me please?

r34son commented 8 months ago

I guess i can patch here https://github.com/sst/open-next/blob/3deb2022d0bb506e4c4b66baeae248c7c2b153e5/packages/open-next/src/build.ts#L913 And maybe rewrite some banners in cjs

conico974 commented 8 months ago

It might work, but that's likely not something that we'll merge in the repo. And it will never work in V3 since we need top level await there for core functionality. What's your usecase for this?

r34son commented 8 months ago

Oh, i`m trying to deploy sls function to the Yandex Cloud. They do not support esm yet. https://cloud.yandex.ru/en/docs/functions/lang/nodejs/handler

r34son commented 8 months ago

@conico974 Can you please suggest, why next is external here? Can i bundle it and upload only index.mjs without need to archive node_modules. My usecase that I use pnpm and i just can`t simply zip node_modules, because pnpm uses symlinks. Or maybe i need to resolve them before passing to zip. Maybe you have any ideas on it?

P.S. Will try node-linker=hoisted

Legitimate reasons to use this setting: ... Your project is deployed to a serverless hosting provider. Some serverless providers (for instance, AWS Lambda) don't support symlinks. An alternative solution for this problem is to bundle your application before deployment.

khuezy commented 8 months ago

I use hoisted for my pnpm and it worked. The reason why we need external: ['next'] is because nextjs requires (or perhaps loads some next libs dynamically) so we can't bundle the code. That may or may not have changed in v14.

r34son commented 8 months ago

Yeah, got you, thanks.

It dynamically requires "@opentelemetry/api", "critters", "styled-jsx" and etc.

conico974 commented 8 months ago

Just bundling into CJS won't be enough if you want to deploy outside of AWS and without ESM. In V2 there is a lot of dependencies to AWS (S3, SQS, DynamoDB, Cloudfront, Lambda event) that you'll have to replace if you want everything to work. For example this will need to be rewritten.

i just can`t simply zip node_modules, because pnpm uses symlinks

zip command (at least on linux) will properly copy the symlinks you need. One other reasons why you can't just bundle next is that the build output of next also depends on node_modules

r34son commented 8 months ago

I am using terraform to archive lambda, maybe i need some additional options to pass, but i tried pnpm hoisted setting and it easily solves my issue