11import { cp , mkdir , readFile , rm , writeFile } from 'node:fs/promises'
2- import { dirname , join } from 'node:path'
2+ import { dirname , join , relative , sep } from 'node:path'
3+ import { sep as posixSep } from 'node:path/posix'
34
45import type { Manifest , ManifestFunction } from '@netlify/edge-functions'
56import { glob } from 'fast-glob'
@@ -8,6 +9,8 @@ import { pathToRegexp } from 'path-to-regexp'
89
910import { EDGE_HANDLER_NAME , PluginContext } from '../plugin-context.js'
1011
12+ const toPosixPath = ( path : string ) => path . split ( sep ) . join ( posixSep )
13+
1114const writeEdgeManifest = async ( ctx : PluginContext , manifest : Manifest ) => {
1215 await mkdir ( ctx . edgeFunctionsDir , { recursive : true } )
1316 await writeFile ( join ( ctx . edgeFunctionsDir , 'manifest.json' ) , JSON . stringify ( manifest , null , 2 ) )
@@ -107,10 +110,19 @@ const copyHandlerDependencies = async (
107110
108111 const parts = [ shim ]
109112
113+ const outputFile = join ( destDir , `server/${ name } .js` )
114+
110115 if ( wasm ?. length ) {
111- parts . push (
112- `import { decode as _base64Decode } from "../edge-runtime/vendor/deno.land/std@0.175.0/encoding/base64.ts";` ,
116+ const base64ModulePath = join (
117+ destDir ,
118+ 'edge-runtime/vendor/deno.land/std@0.175.0/encoding/base64.ts' ,
113119 )
120+
121+ const base64ModulePathRelativeToOutputFile = toPosixPath (
122+ relative ( dirname ( outputFile ) , base64ModulePath ) ,
123+ )
124+
125+ parts . push ( `import { decode as _base64Decode } from "${ base64ModulePathRelativeToOutputFile } ";` )
114126 for ( const wasmChunk of wasm ?? [ ] ) {
115127 const data = await readFile ( join ( srcDir , wasmChunk . filePath ) )
116128 parts . push (
@@ -126,9 +138,9 @@ const copyHandlerDependencies = async (
126138 parts . push ( `;// Concatenated file: ${ file } \n` , entrypoint )
127139 }
128140 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 } )
141+ await mkdir ( dirname ( outputFile ) , { recursive : true } )
130142
131- await writeFile ( join ( destDir , `server/ ${ name } .js` ) , [ ...parts , exports ] . join ( '\n' ) )
143+ await writeFile ( outputFile , [ ...parts , exports ] . join ( '\n' ) )
132144}
133145
134146const createEdgeHandler = async ( ctx : PluginContext , definition : NextDefinition ) : Promise < void > => {
0 commit comments