Skip to content

Commit 5a324e7

Browse files
committed
fix(middleware): Support wasm in bundled middleware
1 parent 86f4b44 commit 5a324e7

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

packages/open-next/src/build/createServerBundle.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "node:path";
33

44
import type { FunctionOptions, SplittedFunctionOptions } from "types/open-next";
55

6+
import { loadMiddlewareManifest } from "config/util.js";
67
import type { Plugin } from "esbuild";
78
import logger from "../logger.js";
89
import { minifyAll } from "../minimize-js.js";
@@ -135,12 +136,14 @@ async function generateBundle(
135136
// `.next/standalone/package/path` (ie. `.next`, `server.js`).
136137
// We need to output the handler file inside the package path.
137138
const packagePath = buildHelper.getPackagePath(options);
138-
fs.mkdirSync(path.join(outputPath, packagePath), { recursive: true });
139+
const outPackagePath = path.join(outputPath, packagePath);
140+
141+
fs.mkdirSync(outPackagePath, { recursive: true });
139142

140143
const ext = fnOptions.runtime === "deno" ? "mjs" : "cjs";
141144
fs.copyFileSync(
142145
path.join(options.buildDir, `cache.${ext}`),
143-
path.join(outputPath, packagePath, "cache.cjs"),
146+
path.join(outPackagePath, "cache.cjs"),
144147
);
145148

146149
if (fnOptions.runtime === "deno") {
@@ -150,7 +153,7 @@ async function generateBundle(
150153
// Bundle next server if necessary
151154
const isBundled = fnOptions.experimentalBundledNextServer ?? false;
152155
if (isBundled) {
153-
await bundleNextServer(path.join(outputPath, packagePath), appPath, {
156+
await bundleNextServer(outPackagePath, appPath, {
154157
minify: options.minify,
155158
});
156159
}
@@ -159,15 +162,26 @@ async function generateBundle(
159162
if (!config.middleware?.external) {
160163
fs.copyFileSync(
161164
path.join(options.buildDir, "middleware.mjs"),
162-
path.join(outputPath, packagePath, "middleware.mjs"),
165+
path.join(outPackagePath, "middleware.mjs"),
166+
);
167+
168+
const middlewareManifest = loadMiddlewareManifest(
169+
path.join(options.appBuildOutputPath, ".next"),
163170
);
171+
const wasmFiles = middlewareManifest.middleware["/"]?.wasm ?? [];
172+
if (wasmFiles.length > 0) {
173+
fs.mkdirSync(path.join(outPackagePath, "wasm"), { recursive: true });
174+
for (const wasmFile of wasmFiles) {
175+
fs.copyFileSync(
176+
path.join(options.appBuildOutputPath, ".next", wasmFile.filePath),
177+
path.join(outPackagePath, `wasm/${wasmFile.name}.wasm`),
178+
);
179+
}
180+
}
164181
}
165182

166183
// Copy open-next.config.mjs
167-
buildHelper.copyOpenNextConfig(
168-
options.buildDir,
169-
path.join(outputPath, packagePath),
170-
);
184+
buildHelper.copyOpenNextConfig(options.buildDir, outPackagePath);
171185

172186
// Copy env files
173187
buildHelper.copyEnvFile(appBuildOutputPath, packagePath, outputPath);

packages/open-next/src/plugins/edge.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,18 @@ if (!globalThis.URLPattern) {
139139
`
140140
}
141141
${wasmFiles
142-
.map((file) =>
142+
.map((file, i) =>
143143
isInCloudfare
144-
? `import ${file.name} from './wasm/${file.name}.wasm';`
144+
? // Decorate the name to avoid name collisions
145+
`import __OpenNextWasm${i} from './wasm/${file.name}.wasm';
146+
globalThis.${file.name} = __OpenNextWasm${i}`
145147
: `const ${file.name} = readFileSync(path.join(__dirname,'/wasm/${file.name}.wasm'));`,
146148
)
147149
.join("\n")}
148150
${entryFiles.map((file) => `require("${file}");`).join("\n")}
149151
${contents}
150152
`;
153+
151154
return {
152155
contents,
153156
};

0 commit comments

Comments
 (0)