-
Notifications
You must be signed in to change notification settings - Fork 89
fix: add require shims #2050
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
fix: add require shims #2050
Changes from 3 commits
8bb592e
f4ce727
b0b994b
409354d
946533e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,6 +2,7 @@ | |||||
// deno-lint-ignore-file no-var prefer-const no-unused-vars no-explicit-any | ||||||
import { decode as _base64Decode } from 'https://deno.land/std@0.175.0/encoding/base64.ts' | ||||||
import { AsyncLocalStorage as ALSCompat } from 'https://deno.land/std@0.175.0/node/async_hooks.ts' | ||||||
import { Buffer as BufferCompat } from 'https://deno.land/std@0.175.0/io/buffer.ts' | ||||||
|
||||||
/** | ||||||
* These are the shims, polyfills and other kludges to make Next.js work in standards-compliant runtime. | ||||||
|
@@ -48,6 +49,16 @@ const fetch /* type {typeof globalThis.fetch} */ = async (url, init) => { | |||||
} | ||||||
} | ||||||
|
||||||
// Turbopack aliases "Buffer" to a module import, so we need to provide a shim for that | ||||||
if (typeof require === 'undefined') { | ||||||
globalThis.require = (name) => { | ||||||
if (name === 'buffer' || name === 'node:buffer') { | ||||||
return { Buffer: BufferCompat } | ||||||
} | ||||||
throw new ReferenceError('require is not defined') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'll seem weird if we get the error This will help with E2E canary test runs to signal earlier on exactly what the issue is. Something along these lines.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about matching the Next.js message? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even better 😎 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Do you mean the other stuff in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They're all the Node modules that are available in workerd, so which Vercel edge runtime now assumes are available. Buffer is the only on they're using in core Next.js right now (I think), but they're basically telling users that they can use them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ascorbic okay, I'll shim all the things! |
||||||
} | ||||||
} | ||||||
|
||||||
// Next edge runtime uses "self" as a function-scoped global-like object, but some of the older polyfills expect it to equal globalThis | ||||||
// See https://nextjs.org/docs/basic-features/supported-browsers-features#polyfills | ||||||
const self = { ...globalThis, fetch } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So many shims. 😅