From 5f7a329844e9017e5f36fa6fcf950c92d6501199 Mon Sep 17 00:00:00 2001 From: feugy Date: Wed, 3 Aug 2022 12:52:06 +0200 Subject: [PATCH] chore: improves implementation to allow any fallbacks --- .../build/webpack/plugins/middleware-plugin.ts | 16 +++++++++++----- .../test/index.test.js | 5 +---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/next/build/webpack/plugins/middleware-plugin.ts b/packages/next/build/webpack/plugins/middleware-plugin.ts index 8a06077879c..f688d4e32e9 100644 --- a/packages/next/build/webpack/plugins/middleware-plugin.ts +++ b/packages/next/build/webpack/plugins/middleware-plugin.ts @@ -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}')` + } } } @@ -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) } diff --git a/test/integration/edge-runtime-module-errors/test/index.test.js b/test/integration/edge-runtime-module-errors/test/index.test.js index cb31f2bca6c..2906fe6ce0a 100644 --- a/test/integration/edge-runtime-module-errors/test/index.test.js +++ b/test/integration/edge-runtime-module-errors/test/index.test.js @@ -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)