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

Issues with bundling date-fns #1889

Closed
danielroe opened this issue Nov 7, 2023 · 7 comments
Closed

Issues with bundling date-fns #1889

danielroe opened this issue Nov 7, 2023 · 7 comments
Labels
bug Something isn't working cjs rollup

Comments

@danielroe
Copy link
Member

Environment

node 20.9
nitro 2.7.2

Reproduction

https://stackblitz.com/edit/github-yysl4x?file=routes/index.ts,nitro.config.ts

You'll need to download locally to run wrangler

Describe the bug

Some libraries (specifically date-fns here) can be inlined incorrectly in cloudflare-pages output (for example) but I suspect any single-file output.

Downstream issue: nuxt/nuxt#23963

Additional context

No response

Logs

No response

@pi0
Copy link
Member

pi0 commented Nov 7, 2023

Thanks for making issue with reproduction.

It seems a bug with rollup cjs plugin generated code.

Wrangler:

service core:user:worker: Uncaught TypeError: _interopRequireDefault2 is not a function
  at d9b7i8l49fs.js:7105:18
  at d9b7i8l49fs.js:7123:3
✘ [ERROR] MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.

Deno deploy:

error: The deployment failed: UNCAUGHT_EXCEPTION

TypeError: _interopRequireDefault is not a function
    at file:///src/server/index.ts:7086:18
    at file:///src/server/index.ts:7137:3

Re date-fns, it's dist format is CommonJS adding one _interopRequireDefault per function/entry from babel runtime which is it's dependencies also (example)!!! This is how many of them will be added by import { format } from 'date-fns' in dist/_worker.js:

image

I made (yet another) esm redist date-fns-es which is WIP (no subpaths for now but treeshakable) and might opensource it if cannot figure out a stable reproduction for the root cause of rollup/wrangler issue with CJS/babel helpers. (for reference, they planned esm support since 2020 date-fns/date-fns#1781)

"resolutions": {
    "date-fns": "npm:date-fns-es"
}

@pi0 pi0 changed the title date-fns isn't inlined correctly when producing single output Issues with bundling date-fns Nov 7, 2023
@MickL
Copy link
Contributor

MickL commented Nov 11, 2023

The same error happens on Vercel Edge Functions (it is working on normal Vercel Functions tho):

TypeError: t is not a function
at (index.mjs:1:110837)
at (index.mjs:1:110877)
at (__nitro:middleware.js:1:17)

And on Cloudflare Pages I get:

Error: Failed to publish your Function. Got error: Uncaught TypeError: r2 is not a function
at functionsWorker-0.5884842512200903.js:3588:12
at functionsWorker-0.5884842512200903.js:3590:2

Minimal reproduction repo (Nuxt 3): https://github.com/MickL/nuxt-datefns

I am not exactly sure if the issue is on Nitro side or date-fns? The ESM support issue at date-fns is open for more than 3 years with an open PR for more than one year.

@pi0
Copy link
Member

pi0 commented Nov 12, 2023

@MickL Thanks for confirming. Yes this issue with bundling date-fns via rollup to an esm bundle applies for every bundled runtime.

If we cannot find an easy solution i think we shall start auto patching date-fns to esm version

@MickL
Copy link
Contributor

MickL commented Nov 23, 2023

date-fns has fixed this and it will be within the upcoming 3.0.0 release: date-fns/date-fns#1781 (comment)

@pi0
Copy link
Member

pi0 commented Nov 23, 2023

Thanks for update @MickL. Checking 3.0.0-alpha.1 on unpkg, seems also babel dependency is droped 🤞🏼

@danielroe danielroe closed this as not planned Won't fix, can't repro, duplicate, stale Nov 23, 2023
@danielroe danielroe reopened this Nov 23, 2023
@MickL
Copy link
Contributor

MickL commented Nov 24, 2023

I can confirm that date-fns 3.0.0-alpha.1 works on Vercel Edge functions (therefor I guess on Cloudflare Workers, too).

@MickL
Copy link
Contributor

MickL commented Apr 21, 2024

I think this can be close as completed! date-fns works fine since 3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cjs rollup
Projects
None yet
Development

No branches or pull requests

3 participants