Skip to content

Commit

Permalink
chore: improves implementation to allow any fallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy committed Aug 3, 2022
1 parent 1fe553e commit 5f7a329
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
16 changes: 11 additions & 5 deletions packages/next/build/webpack/plugins/middleware-plugin.ts
Expand Up @@ -103,13 +103,22 @@ export default class MiddlewarePlugin {

export async function handleWebpackExtenalForEdgeRuntime({
request,
context,
contextInfo,
getResolve,
}: {
request: string
context: string
contextInfo: any
getResolve: () => any
}) {
if (contextInfo.issuerLayer === 'middleware' && isNodeJsModule(request)) {
return `root globalThis.__import_unsupported('${request}')`
// allows user to provide and use their polyfills, as we do with buffer.
try {
await getResolve()(context, request)
} catch {
return `root globalThis.__import_unsupported('${request}')`
}
}
}

Expand Down Expand Up @@ -760,8 +769,5 @@ function isProcessEnvMemberExpression(memberExpression: any): boolean {
}

function isNodeJsModule(moduleName: string) {
return (
moduleName !== 'buffer' &&
require('module').builtinModules.includes(moduleName)
)
return require('module').builtinModules.includes(moduleName)
}
Expand Up @@ -599,10 +599,7 @@ describe('Edge runtime code with imports', () => {
})

it('does not throw in production at runtime', async () => {
const { stderr } = await nextBuild(context.appDir, undefined, {
stderr: true,
})
expect(stderr).not.toContain(getUnsupportedModuleWarning(moduleName))
await nextBuild(context.appDir, undefined, { stderr: true })
context.app = await nextStart(context.appDir, context.appPort, appOption)
const res = await fetchViaHTTP(context.appPort, url)
expect(res.status).toBe(200)
Expand Down

0 comments on commit 5f7a329

Please sign in to comment.