Skip to content

Commit 0bf460a

Browse files
committed
fix: handle node middleware bundling when pmpm is used
1 parent ec69eeb commit 0bf460a

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

src/build/functions/edge.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
1+
import { cp, mkdir, readdir, readFile, rm, stat, writeFile } from 'node:fs/promises'
22
import { dirname, join, relative } from 'node:path/posix'
33

44
import type { Manifest, ManifestFunction } from '@netlify/edge-functions'
@@ -259,14 +259,26 @@ const copyHandlerDependenciesForNodeMiddleware = async (ctx: PluginContext) => {
259259

260260
parts.push(`const virtualModules = new Map();`)
261261

262-
for (const file of files) {
263-
const srcPath = join(srcDir, file)
262+
const handleFileOrDirectory = async (fileOrDir: string) => {
263+
const srcPath = join(srcDir, fileOrDir)
264264

265-
const content = await readFile(srcPath, 'utf8')
265+
const stats = await stat(srcPath)
266+
if (stats.isDirectory()) {
267+
const filesInDir = await readdir(srcPath)
268+
for (const fileInDir of filesInDir) {
269+
await handleFileOrDirectory(join(fileOrDir, fileInDir))
270+
}
271+
} else {
272+
const content = await readFile(srcPath, 'utf8')
266273

267-
parts.push(
268-
`virtualModules.set(${JSON.stringify(join(commonPrefix, file))}, ${JSON.stringify(content)});`,
269-
)
274+
parts.push(
275+
`virtualModules.set(${JSON.stringify(join(commonPrefix, fileOrDir))}, ${JSON.stringify(content)});`,
276+
)
277+
}
278+
}
279+
280+
for (const file of files) {
281+
await handleFileOrDirectory(file)
270282
}
271283
parts.push(`registerCJSModules(import.meta.url, virtualModules);
272284

0 commit comments

Comments
 (0)