-
Notifications
You must be signed in to change notification settings - Fork 151
fix(middleware): Support wasm in bundled middleware #802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: c2dfbd6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Coverage Report
File Coverage
|
commit: |
path.join(outPackagePath, "middleware.mjs"), | ||
); | ||
|
||
const middlewareManifest = loadMiddlewareManifest( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should create a generic function for this and use it both here and there
opennextjs-aws/packages/open-next/src/build/edge/createEdgeBundle.ts
Lines 212 to 230 in fbe09ae
//Copy wasm files | |
const wasmFiles = middlewareInfo.wasm; | |
mkdirSync(path.join(outputDir, "wasm"), { recursive: true }); | |
for (const wasmFile of wasmFiles) { | |
fs.copyFileSync( | |
path.join(buildOutputDotNextDir, wasmFile.filePath), | |
path.join(outputDir, `wasm/${wasmFile.name}.wasm`), | |
); | |
} | |
// Copy assets | |
const assets = middlewareInfo.assets; | |
mkdirSync(path.join(outputDir, "assets"), { recursive: true }); | |
for (const asset of assets) { | |
fs.copyFileSync( | |
path.join(buildOutputDotNextDir, asset.filePath), | |
path.join(outputDir, `assets/${asset.name}`), | |
); | |
} |
We also need to copy the assets
? `import ${file.name} from './wasm/${file.name}.wasm';` | ||
? // Decorate the name to avoid name collisions | ||
`import __OpenNextWasm${i} from './wasm/${file.name}.wasm'; | ||
globalThis.${file.name} = __OpenNextWasm${i}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm probably missing something here, but if there is collision, wouldn't this also cause some issue with the global ?
Only things that use this (without being a separate function) should be the middleware
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.next/server/src/middleware.js
would have something like
683: (e) => {
e.exports = wasm_ea010aa52414c7de383495546f15cb7d49b055fc;
}
If the code here is
import wasm_ea010aa52414c7de383495546f15cb7d49b055fc from '...';
Then ESBuild will change that to wasm_ea010aa52414c7de383495546f15cb7d49b055fc2
(appending a 2 at the end).
So we want the "collision" here.
@@ -159,15 +162,26 @@ async function generateBundle( | |||
if (!config.middleware?.external) { | |||
fs.copyFileSync( | |||
path.join(options.buildDir, "middleware.mjs"), | |||
path.join(outputPath, packagePath, "middleware.mjs"), | |||
path.join(outPackagePath, "middleware.mjs"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All this copy logic should also be in a function on this file so that everything middleware related is there https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/build/createMiddleware.ts.
Pretty sure we need to copy the wasm/asset files for the external middleware as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM Thanks
Thanks for the review Nico! |
Related to opennextjs/opennextjs-cloudflare#471
2 fixes in this PR:
createServerBundle
but we also need to copy .wasm files