-
Notifications
You must be signed in to change notification settings - Fork 26k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose wasm files in middleware-manifest
- Loading branch information
Showing
3 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/next/build/webpack/loaders/next-middleware-wasm-loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { webpack } from 'next/dist/compiled/webpack/webpack' | ||
import crypto from 'crypto' | ||
|
||
type WasmBinding = { | ||
file: string | ||
name: string | ||
} | ||
|
||
const MiddlewareWasmLoader: webpack.loader.Loader = function ( | ||
source, | ||
_sourceMap | ||
) { | ||
const cb = this.async() | ||
if (!cb) throw new Error('async() must be used with wasm-loader') | ||
|
||
const name = `wasm_${sha1(source)}` | ||
const file = `middleware-chunks/${name}.wasm` | ||
const binding: WasmBinding = { file, name } | ||
this._module.buildInfo.nextWasmMiddlewareBinding = binding | ||
this.emitFile(`/${file}`, source, undefined) | ||
|
||
cb(null, `module.exports = globalThis[${JSON.stringify(name)}];`) | ||
} | ||
|
||
function sha1(source: string | Buffer) { | ||
return crypto.createHash('sha1').update(source).digest('hex') | ||
} | ||
|
||
export default MiddlewareWasmLoader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters