@@ -214,9 +214,9 @@ export const createEdgeHandlers = async (ctx: PluginContext) => {
214214 const srcDir = join ( ctx . standaloneDir , ctx . nextDistDir )
215215 // const destDir = join(ctx.edgeFunctionsDir, getHandlerName({ name }))
216216
217- const fakeNodeModuleName = 'fake-module-with-middleware'
217+ // const fakeNodeModuleName = 'fake-module-with-middleware'
218218
219- const fakeNodeModulePath = ctx . resolveFromPackagePath ( join ( 'node_modules' , fakeNodeModuleName ) )
219+ // const fakeNodeModulePath = ctx.resolveFromPackagePath(join('node_modules', fakeNodeModuleName))
220220
221221 const nftFilesPath = join ( process . cwd ( ) , ctx . nextDistDir , nft )
222222 const nftManifest = JSON . parse ( await readFile ( nftFilesPath , 'utf8' ) )
@@ -246,47 +246,53 @@ export const createEdgeHandlers = async (ctx: PluginContext) => {
246246 prefixPath += `nested-${ nestedIndex } /`
247247 }
248248
249+ let virtualModules = ''
249250 for ( const file of files ) {
250251 const srcPath = join ( srcDir , file )
251- const destPath = join ( fakeNodeModulePath , prefixPath , file )
252-
253- await mkdir ( dirname ( destPath ) , { recursive : true } )
254-
255- if ( file === entry ) {
256- const content = await readFile ( srcPath , 'utf8' )
257- await writeFile (
258- destPath ,
259- // Next.js needs to be set on global even if it's possible to just require it
260- // so somewhat similar to existing shim we have for edge runtime
261- `globalThis.AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage;\n${ content } ` ,
262- )
263- } else {
264- await cp ( srcPath , destPath , { force : true } )
265- }
252+
253+ const content = await readFile ( srcPath , 'utf8' )
254+
255+ virtualModules += `virtualModules.set(${ JSON . stringify ( join ( prefixPath , file ) ) } , ${ JSON . stringify ( content ) } );\n`
256+
257+ // const destPath = join(fakeNodeModulePath, prefixPath, file)
258+
259+ // await mkdir(dirname(destPath), { recursive: true })
260+
261+ // if (file === entry) {
262+ // const content = await readFile(srcPath, 'utf8')
263+ // await writeFile(
264+ // destPath,
265+ // // Next.js needs to be set on global even if it's possible to just require it
266+ // // so somewhat similar to existing shim we have for edge runtime
267+ // `globalThis.AsyncLocalStorage = require('node:async_hooks').AsyncLocalStorage;\n${content}`,
268+ // )
269+ // } else {
270+ // await cp(srcPath, destPath, { force: true })
271+ // }
266272 }
267273
268- await writeFile ( join ( fakeNodeModulePath , 'package.json' ) , JSON . stringify ( { type : 'commonjs' } ) )
274+ // await writeFile(join(fakeNodeModulePath, 'package.json'), JSON.stringify({ type: 'commonjs' }))
269275
270276 // there is `/chunks/**/*` require coming from webpack-runtime that fails esbuild due to nothing matching,
271277 // so this ensure something does
272- const dummyChunkPath = join ( fakeNodeModulePath , prefixPath , 'server' , 'chunks' , 'dummy.js' )
273- await mkdir ( dirname ( dummyChunkPath ) , { recursive : true } )
274- await writeFile ( dummyChunkPath , '' )
278+ // const dummyChunkPath = join(fakeNodeModulePath, prefixPath, 'server', 'chunks', 'dummy.js')
279+ // await mkdir(dirname(dummyChunkPath), { recursive: true })
280+ // await writeFile(dummyChunkPath, '')
275281
276282 // there is also `@opentelemetry/api` require that fails esbuild due to nothing matching,
277283 // next is try/catching it and fallback to bundled version of otel package in case of errors
278- const otelApiPath = join (
279- fakeNodeModulePath ,
280- 'node_modules' ,
281- '@opentelemetry' ,
282- 'api' ,
283- 'index.js' ,
284- )
285- await mkdir ( dirname ( otelApiPath ) , { recursive : true } )
286- await writeFile (
287- otelApiPath ,
288- `throw new Error('this is dummy to satisfy esbuild used for npm compat using fake module')` ,
289- )
284+ // const otelApiPath = join(
285+ // fakeNodeModulePath,
286+ // 'node_modules',
287+ // '@opentelemetry',
288+ // 'api',
289+ // 'index.js',
290+ // )
291+ // await mkdir(dirname(otelApiPath), { recursive: true })
292+ // await writeFile(
293+ // otelApiPath,
294+ // `throw new Error('this is dummy to satisfy esbuild used for npm compat using fake module')`,
295+ // )
290296
291297 // await writeHandlerFile(ctx, definition)
292298
@@ -333,11 +339,23 @@ export const createEdgeHandlers = async (ctx: PluginContext) => {
333339 await writeFile (
334340 join ( handlerDirectory , `${ handlerName } .js` ) ,
335341 `
342+ import { createRequire } from "node:module";
336343 import { init as htmlRewriterInit } from './edge-runtime/vendor/deno.land/x/[email protected] /src/index.ts' 337344 import { handleMiddleware } from './edge-runtime/middleware.ts';
345+ import { registerCJSModules } from "./edge-runtime/lib/cjs.ts";
346+ import { AsyncLocalStorage } from 'node:async_hooks';
347+
348+ globalThis.AsyncLocalStorage = AsyncLocalStorage;
349+
350+ // needed for path.relative and path.resolve to work
351+ Deno.cwd = () => ''
338352
339- import * as handlerMod from '${ fakeNodeModuleName } /${ prefixPath } ${ entry } ';
353+ const virtualModules = new Map();
354+ ${ virtualModules }
355+ registerCJSModules(import.meta.url, virtualModules);
340356
357+ const require = createRequire(import.meta.url);
358+ const handlerMod = require("./${ prefixPath } /${ entry } ");
341359 const handler = handlerMod.default || handlerMod;
342360
343361 await htmlRewriterInit({ module_or_path: Uint8Array.from(${ JSON . stringify ( [
0 commit comments