|
1 | 1 | import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
|
2 |
| -import { dirname, join, relative } from 'node:path' |
| 2 | +import { dirname, join } from 'node:path' |
3 | 3 |
|
4 | 4 | import { glob } from 'fast-glob'
|
5 | 5 | import type { EdgeFunctionDefinition as NextDefinition } from 'next/dist/build/webpack/plugins/middleware-plugin.js'
|
@@ -116,44 +116,34 @@ const copyHandlerDependencies = async (
|
116 | 116 | const srcDir = join(ctx.standaloneDir, ctx.nextDistDir)
|
117 | 117 | const destDir = join(ctx.edgeFunctionsDir, getHandlerName({ name }))
|
118 | 118 |
|
119 |
| - await Promise.all( |
120 |
| - files.map(async (file) => { |
121 |
| - if (file === `server/${name}.js`) { |
122 |
| - const edgeRuntimeDir = join(ctx.pluginDir, 'edge-runtime') |
123 |
| - const shimPath = join(edgeRuntimeDir, 'shim/index.js') |
124 |
| - const shim = await readFile(shimPath, 'utf8') |
125 |
| - |
126 |
| - const importsDir = relative(dirname(join(srcDir, file)), join(srcDir, 'server')) |
127 |
| - const importsSrc = `${importsDir || '.'}/edge-runtime-webpack.js` |
128 |
| - const imports = `import '${importsSrc}';` |
129 |
| - |
130 |
| - const exports = `export default _ENTRIES["middleware_${name}"].default;` |
131 |
| - |
132 |
| - const parts = [shim, imports] |
133 |
| - |
134 |
| - if (wasm?.length) { |
135 |
| - parts.push( |
136 |
| - `import { decode as _base64Decode } from "../edge-runtime/vendor/deno.land/[email protected]/encoding/base64.ts";`, |
137 |
| - ) |
138 |
| - for (const wasmChunk of wasm ?? []) { |
139 |
| - const data = await readFile(join(srcDir, wasmChunk.filePath)) |
140 |
| - parts.push( |
141 |
| - `const ${wasmChunk.name} = _base64Decode(${JSON.stringify( |
142 |
| - data.toString('base64'), |
143 |
| - )}).buffer`, |
144 |
| - ) |
145 |
| - } |
146 |
| - } |
147 |
| - |
148 |
| - const entrypoint = await readFile(join(srcDir, file), 'utf8') |
149 |
| - |
150 |
| - await mkdir(dirname(join(destDir, file)), { recursive: true }) |
151 |
| - await writeFile(join(destDir, file), [...parts, entrypoint, exports].join('\n;')) |
152 |
| - } else { |
153 |
| - await cp(join(srcDir, file), join(destDir, file)) |
154 |
| - } |
155 |
| - }), |
156 |
| - ) |
| 119 | + const edgeRuntimeDir = join(ctx.pluginDir, 'edge-runtime') |
| 120 | + const shimPath = join(edgeRuntimeDir, 'shim/index.js') |
| 121 | + const shim = await readFile(shimPath, 'utf8') |
| 122 | + |
| 123 | + const parts = [shim] |
| 124 | + |
| 125 | + if (wasm?.length) { |
| 126 | + parts.push( |
| 127 | + `import { decode as _base64Decode } from "../edge-runtime/vendor/deno.land/[email protected]/encoding/base64.ts";`, |
| 128 | + ) |
| 129 | + for (const wasmChunk of wasm ?? []) { |
| 130 | + const data = await readFile(join(srcDir, wasmChunk.filePath)) |
| 131 | + parts.push( |
| 132 | + `const ${wasmChunk.name} = _base64Decode(${JSON.stringify( |
| 133 | + data.toString('base64'), |
| 134 | + )}).buffer`, |
| 135 | + ) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + for (const file of files) { |
| 140 | + const entrypoint = await readFile(join(srcDir, file), 'utf8') |
| 141 | + parts.push(`;// Concatenated file: ${file} \n`, entrypoint) |
| 142 | + } |
| 143 | + const exports = `export default _ENTRIES["middleware_${name}"].default;` |
| 144 | + await mkdir(dirname(join(destDir, `server/${name}.js`)), { recursive: true }) |
| 145 | + |
| 146 | + await writeFile(join(destDir, `server/${name}.js`), [...parts, exports].join('\n')) |
157 | 147 | }
|
158 | 148 |
|
159 | 149 | const createEdgeHandler = async (ctx: PluginContext, definition: NextDefinition): Promise<void> => {
|
|
0 commit comments