@@ -214,9 +214,9 @@ export const createEdgeHandlers = async (ctx: PluginContext) => {
214
214
const srcDir = join ( ctx . standaloneDir , ctx . nextDistDir )
215
215
// const destDir = join(ctx.edgeFunctionsDir, getHandlerName({ name }))
216
216
217
- const fakeNodeModuleName = 'fake-module-with-middleware'
217
+ // const fakeNodeModuleName = 'fake-module-with-middleware'
218
218
219
- const fakeNodeModulePath = ctx . resolveFromPackagePath ( join ( 'node_modules' , fakeNodeModuleName ) )
219
+ // const fakeNodeModulePath = ctx.resolveFromPackagePath(join('node_modules', fakeNodeModuleName))
220
220
221
221
const nftFilesPath = join ( process . cwd ( ) , ctx . nextDistDir , nft )
222
222
const nftManifest = JSON . parse ( await readFile ( nftFilesPath , 'utf8' ) )
@@ -246,47 +246,53 @@ export const createEdgeHandlers = async (ctx: PluginContext) => {
246
246
prefixPath += `nested-${ nestedIndex } /`
247
247
}
248
248
249
+ let virtualModules = ''
249
250
for ( const file of files ) {
250
251
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
+ // }
266
272
}
267
273
268
- await writeFile ( join ( fakeNodeModulePath , 'package.json' ) , JSON . stringify ( { type : 'commonjs' } ) )
274
+ // await writeFile(join(fakeNodeModulePath, 'package.json'), JSON.stringify({ type: 'commonjs' }))
269
275
270
276
// there is `/chunks/**/*` require coming from webpack-runtime that fails esbuild due to nothing matching,
271
277
// 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, '')
275
281
276
282
// there is also `@opentelemetry/api` require that fails esbuild due to nothing matching,
277
283
// 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
+ // )
290
296
291
297
// await writeHandlerFile(ctx, definition)
292
298
@@ -333,11 +339,23 @@ export const createEdgeHandlers = async (ctx: PluginContext) => {
333
339
await writeFile (
334
340
join ( handlerDirectory , `${ handlerName } .js` ) ,
335
341
`
342
+ import { createRequire } from "node:module";
336
343
import { init as htmlRewriterInit } from './edge-runtime/vendor/deno.land/x/[email protected] /src/index.ts'
337
344
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 = () => ''
338
352
339
- import * as handlerMod from '${ fakeNodeModuleName } /${ prefixPath } ${ entry } ';
353
+ const virtualModules = new Map();
354
+ ${ virtualModules }
355
+ registerCJSModules(import.meta.url, virtualModules);
340
356
357
+ const require = createRequire(import.meta.url);
358
+ const handlerMod = require("./${ prefixPath } /${ entry } ");
341
359
const handler = handlerMod.default || handlerMod;
342
360
343
361
await htmlRewriterInit({ module_or_path: Uint8Array.from(${ JSON . stringify ( [
0 commit comments