Skip to content

Commit ac3b909

Browse files
committed
fix: generate correct import path when 'src' directory is used and middleware imports wasm module
1 parent 939e2ab commit ac3b909

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/build/functions/edge.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
2-
import { dirname, join } from 'node:path'
2+
import { dirname, join, relative } from 'node:path'
33

44
import type { Manifest, ManifestFunction } from '@netlify/edge-functions'
55
import { glob } from 'fast-glob'
@@ -107,10 +107,17 @@ const copyHandlerDependencies = async (
107107

108108
const parts = [shim]
109109

110+
const outputFile = join(destDir, `server/${name}.js`)
111+
110112
if (wasm?.length) {
111-
parts.push(
112-
`import { decode as _base64Decode } from "../edge-runtime/vendor/deno.land/[email protected]/encoding/base64.ts";`,
113+
const base64ModulePath = join(
114+
destDir,
115+
'edge-runtime/vendor/deno.land/[email protected]/encoding/base64.ts',
113116
)
117+
118+
const base64ModulePathRelativeToOutputFile = relative(dirname(outputFile), base64ModulePath)
119+
120+
parts.push(`import { decode as _base64Decode } from "${base64ModulePathRelativeToOutputFile}";`)
114121
for (const wasmChunk of wasm ?? []) {
115122
const data = await readFile(join(srcDir, wasmChunk.filePath))
116123
parts.push(
@@ -126,9 +133,9 @@ const copyHandlerDependencies = async (
126133
parts.push(`;// Concatenated file: ${file} \n`, entrypoint)
127134
}
128135
const exports = `const middlewareEntryKey = Object.keys(_ENTRIES).find(entryKey => entryKey.startsWith("middleware_${name}")); export default _ENTRIES[middlewareEntryKey].default;`
129-
await mkdir(dirname(join(destDir, `server/${name}.js`)), { recursive: true })
136+
await mkdir(dirname(outputFile), { recursive: true })
130137

131-
await writeFile(join(destDir, `server/${name}.js`), [...parts, exports].join('\n'))
138+
await writeFile(outputFile, [...parts, exports].join('\n'))
132139
}
133140

134141
const createEdgeHandler = async (ctx: PluginContext, definition: NextDefinition): Promise<void> => {

0 commit comments

Comments
 (0)