11import { 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
44import { glob } from 'fast-glob'
55import type { EdgeFunctionDefinition as NextDefinition } from 'next/dist/build/webpack/plugins/middleware-plugin.js'
@@ -81,42 +81,45 @@ const copyHandlerDependencies = async (
8181 ctx : PluginContext ,
8282 { name, files, wasm } : NextDefinition ,
8383) => {
84- const edgeRuntimePath = join ( ctx . pluginDir , 'edge-runtime' )
8584 const srcDir = join ( ctx . standaloneDir , '.next' )
86- const shimPath = join ( edgeRuntimePath , 'shim/index.js' )
87- const shim = await readFile ( shimPath , 'utf8' )
88- const imports = `import './edge-runtime-webpack.js';`
89- const exports = `export default _ENTRIES["middleware_${ name } "].default;`
90- const parts = [ shim , imports ]
91-
92- if ( wasm ?. length ) {
93- parts . push (
94- `import { decode as _base64Decode } from "../edge-runtime/vendor/deno.land/[email protected] /encoding/base64.ts";` , 95- )
96- for ( const wasmChunk of wasm ?? [ ] ) {
97- const data = await readFile ( join ( srcDir , wasmChunk . filePath ) )
98- parts . push (
99- `const ${ wasmChunk . name } = _base64Decode(${ JSON . stringify (
100- data . toString ( 'base64' ) ,
101- ) } ).buffer`,
102- )
103- }
104- }
85+ const destDir = join ( ctx . edgeFunctionsDir , getHandlerName ( { name } ) )
10586
10687 await Promise . all (
10788 files . map ( async ( file ) => {
108- const destDir = join ( ctx . edgeFunctionsDir , getHandlerName ( { name } ) )
109-
11089 if ( file === `server/${ name } .js` ) {
90+ const edgeRuntimeDir = join ( ctx . pluginDir , 'edge-runtime' )
91+ const shimPath = join ( edgeRuntimeDir , 'shim/index.js' )
92+ const shim = await readFile ( shimPath , 'utf8' )
93+
94+ const importsDir = relative ( dirname ( join ( srcDir , file ) ) , join ( srcDir , 'server' ) )
95+ const importsSrc = `${ importsDir || '.' } /edge-runtime-webpack.js`
96+ const imports = `import '${ importsSrc } ';`
97+
98+ const exports = `export default _ENTRIES["middleware_${ name } "].default;`
99+
100+ const parts = [ shim , imports ]
101+
102+ if ( wasm ?. length ) {
103+ parts . push (
104+ `import { decode as _base64Decode } from "../edge-runtime/vendor/deno.land/[email protected] /encoding/base64.ts";` , 105+ )
106+ for ( const wasmChunk of wasm ?? [ ] ) {
107+ const data = await readFile ( join ( srcDir , wasmChunk . filePath ) )
108+ parts . push (
109+ `const ${ wasmChunk . name } = _base64Decode(${ JSON . stringify (
110+ data . toString ( 'base64' ) ,
111+ ) } ).buffer`,
112+ )
113+ }
114+ }
115+
111116 const entrypoint = await readFile ( join ( srcDir , file ) , 'utf8' )
112117
113118 await mkdir ( dirname ( join ( destDir , file ) ) , { recursive : true } )
114119 await writeFile ( join ( destDir , file ) , [ ...parts , entrypoint , exports ] . join ( '\n;' ) )
115-
116- return
120+ } else {
121+ await cp ( join ( srcDir , file ) , join ( destDir , file ) )
117122 }
118-
119- await cp ( join ( srcDir , file ) , join ( destDir , file ) )
120123 } ) ,
121124 )
122125}
@@ -134,9 +137,10 @@ const buildHandlerDefinition = (
134137 { name, matchers, page } : NextDefinition ,
135138) : Array < NetlifyDefinition > => {
136139 const fun = getHandlerName ( { name } )
137- const funName =
138- name === 'middleware' ? 'Next.js Middleware Handler' : `Next.js Edge Handler: ${ page } `
139- const cache = name === 'middleware' ? undefined : 'manual'
140+ const funName = name . endsWith ( 'middleware' )
141+ ? 'Next.js Middleware Handler'
142+ : `Next.js Edge Handler: ${ page } `
143+ const cache = name . endsWith ( 'middleware' ) ? undefined : 'manual'
140144 const generator = `${ ctx . pluginName } @${ ctx . pluginVersion } `
141145 return matchers . map ( ( matcher ) => ( {
142146 function : fun ,
0 commit comments