diff --git a/packages/next/build/webpack/plugins/middleware-plugin.ts b/packages/next/build/webpack/plugins/middleware-plugin.ts index 8a06077879ca0ad..f688d4e32e9dfb0 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 cb31f2bca6cca13..2906fe6ce0a3de7 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)