Unverified Commit 0ccb3e7f authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

[16.3] Turbopack: don't trace embedded WASM loader helpers (#97353) (#97463)

Backport of https://github.com/vercel/next.js/pull/97353Co-authored-by: 's avatarvercel-fleet[bot] <308483924+vercel-fleet[bot]@users.noreply.github.com>
parent f4dc7c82
import { addOne } from 'wasm-dep'
export async function GET() {
return Response.json({ result: await addOne(1) })
}
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return <p>hello</p>
}
module.exports = {
serverExternalPackages: ['wasm-dep'],
}
import { nextTestSetup } from 'e2e-utils'
describe('turbopack wasm output tracing', () => {
const { next, isTurbopack, isNextStart, isNextDeploy } = nextTestSetup({
files: __dirname,
dependencies: {
'wasm-dep': 'file:./wasm-dep',
},
})
it('builds and serves a node route whose external package references a wasm module', async () => {
const res = await next.fetch('/api/wasm')
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ result: 2 })
})
if (isNextStart && !isNextDeploy) {
it('does not list the embedded wasm runtime helper in the route trace', async () => {
const trace = (await next.readJSON(
'.next/server/app/api/wasm/route.js.nft.json'
)) as { files: string[] }
// The `[turbopack-wasm]/**/loadWasm.ts` helper is compiled into the output
// chunk, it is not a deployable source file. Tracing it used to fail the
// build outright, because a traced file has to live in the project or in
// the output directory.
expect(
trace.files.filter(
(file) => file.includes('turbopack-wasm') || file.includes('loadWasm')
)
).toEqual([])
if (isTurbopack) {
// The wasm file itself has to stay in the trace, the external package
// reads it at runtime.
expect(trace.files.some((file) => file.endsWith('add.wasm'))).toBe(true)
}
})
}
})
const fs = require('fs')
const path = require('path')
const WASM_PATH = path.join(__dirname, 'add.wasm')
// This is never called: it only exists so that the module graph of this package
// reaches a WebAssembly module through a static reference, like packages that
// ship a bundler-friendly WASM entry next to a runtime fallback do.
exports.loadWasmModule = function loadWasmModule() {
return require('./add.wasm')
}
// The runtime fallback, which is what the route below actually uses.
exports.addOne = async function addOne(value) {
const bytes = await fs.promises.readFile(WASM_PATH)
const { instance } = await WebAssembly.instantiate(bytes)
return instance.exports.add_one(value)
}
{
"name": "wasm-dep",
"version": "1.0.0",
"main": "index.js"
}
...@@ -685,22 +685,34 @@ impl ModuleOptions { ...@@ -685,22 +685,34 @@ impl ModuleOptions {
vec![ModuleRuleEffect::ModuleType(ModuleType::NodeAddon)], vec![ModuleRuleEffect::ModuleType(ModuleType::NodeAddon)],
), ),
// WebAssembly // WebAssembly
// In tracing mode these are `Raw` modules: a WebAssembly module is loaded through a
// generated JS loader that references an embedded runtime helper, which is compiled
// into the output and has no path on disk, so tracing it would produce a file
// reference that cannot be resolved to a real file.
ModuleRule::new( ModuleRule::new(
RuleCondition::any(vec![ RuleCondition::any(vec![
RuleCondition::ResourcePathEndsWith(".wasm".to_string()), RuleCondition::ResourcePathEndsWith(".wasm".to_string()),
RuleCondition::ContentTypeStartsWith("application/wasm".to_string()), RuleCondition::ContentTypeStartsWith("application/wasm".to_string()),
]), ]),
vec![ModuleRuleEffect::ModuleType(ModuleType::WebAssembly { if is_tracing {
source_ty: WebAssemblySourceType::Binary, vec![ModuleRuleEffect::ModuleType(ModuleType::Raw)]
})], } else {
vec![ModuleRuleEffect::ModuleType(ModuleType::WebAssembly {
source_ty: WebAssemblySourceType::Binary,
})]
},
), ),
ModuleRule::new( ModuleRule::new(
RuleCondition::any(vec![RuleCondition::ResourcePathEndsWith( RuleCondition::any(vec![RuleCondition::ResourcePathEndsWith(
".wat".to_string(), ".wat".to_string(),
)]), )]),
vec![ModuleRuleEffect::ModuleType(ModuleType::WebAssembly { if is_tracing {
source_ty: WebAssemblySourceType::Text, vec![ModuleRuleEffect::ModuleType(ModuleType::Raw)]
})], } else {
vec![ModuleRuleEffect::ModuleType(ModuleType::WebAssembly {
source_ty: WebAssemblySourceType::Text,
})]
},
), ),
ModuleRule::new( ModuleRule::new(
RuleCondition::any(vec![ RuleCondition::any(vec![
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment