Skip to content

feat(wasm): support inlining wasm binaries with the ?inline query - #1909

Merged
Timeless0911 merged 15 commits into
mainfrom
elecmonkey/wasm-inline
Sep 23, 2026
Merged

Timeless0911 merged 15 commits into
mainfrom
elecmonkey/wasm-inline

Conversation

@elecmonkey

@elecmonkey elecmonkey commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

Summary

Support importing small WebAssembly binaries with ?inline to embed them directly into JavaScript.

import { add } from './add.wasm?inline';

export const sum = (a, b) => add(a, b);

The binary is instantiated with top-level await and exposes the exports declared by the WebAssembly module as ESM exports. This requires ESM output.

In bundleless builds, the binary is embedded into each importing JavaScript file. WebAssembly imports are resolved as module dependencies.

@huanglinfei091-cmd

This comment was marked as off-topic.

@elecmonkey
elecmonkey force-pushed the elecmonkey/wasm-inline branch 2 times, most recently from e93a071 to 80cadfa Compare September 13, 2026 21:22
@elecmonkey
elecmonkey marked this pull request as ready for review September 13, 2026 21:33
Copilot AI lite review requested due to automatic review settings September 13, 2026 21:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@elecmonkey
elecmonkey force-pushed the elecmonkey/wasm-inline branch from 80cadfa to ebd1d43 Compare September 14, 2026 03:26
Comment thread packages/core/src/wasm/inlineLoader.ts
Comment thread packages/core/src/wasm/compose.ts Outdated
Comment thread packages/core/src/wasm/compose.ts Outdated
@Timeless0911

Copy link
Copy Markdown
Contributor

no documents about inline feature in wasm chapter?

The resolve hook read an existing issuer query off the request before
appending a new one, but appended unconditionally. Requests reaching the
hook never carry that query, so drop the fallback.
The inline loader rule was registered regardless of the wasm mode, so
`wasm: false` still inlined `?inline` imports while leaving plain `.wasm`
imports alone. Skip the rule and keep the original specifiers instead.
Absolute issuer paths ended up in the generated module identifiers of
bundleless output, which made builds depend on the source directory.
Comment thread packages/core/src/wasm/inlineLoader.ts Outdated
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

Deploying rslib with  Cloudflare Pages  Cloudflare Pages

Latest commit: ddd5cb8
Status: ✅  Deploy successful!
Preview URL: https://53a9a4bc.rslib.pages.dev
Branch Preview URL: https://elecmonkey-wasm-inline.rslib.pages.dev

View logs

Comment thread website/docs/zh/guide/advanced/wasm.mdx Outdated
@elecmonkey
elecmonkey marked this pull request as draft September 21, 2026 08:00
@elecmonkey
elecmonkey marked this pull request as ready for review September 21, 2026 10:16
@elecmonkey

Copy link
Copy Markdown
Member Author

I suggest merging the current version, since .d.mts doesn't act same as export {}. TypeScript's extension-based module detection intentionally excludes dts files. Also, I verified that the configuration described in the current docs produces a type error without @rslib/core/types, because it is an augmentation for an existing, broader *?inline declaration provided by Rsbuild.

@Timeless0911

Copy link
Copy Markdown
Contributor

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ddd5cb874f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +44 to +47
: request.replace(
/(?=#|$)/,
`${request.includes('?') ? '&' : '?'}${WASM_INLINE_ISSUER_QUERY}=${encodeURIComponent(importer)}`,
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep generated Wasm dependencies in the Wasm pipeline

When bundle: false and an inline Wasm module itself imports another Wasm module such as ./dep.wasm, appending this issuer query changes the request so it no longer ends with .wasm. Consequently, createWasmPreserveExternal and the Wasm bailout in composeBundlelessExternalConfig both miss it; the generic asset logic then rewrites the request to a JavaScript extension, without preserving or compiling the dependency. The resulting output imports a nonexistent .js file, so classification should ignore the internal issuer query.

Useful? React with 👍 / 👎.

Comment on lines +1346 to +1349
const issuer =
inlineIssuer !== null && typeof outBase === 'string'
? path.resolve(outBase, inlineIssuer)
: contextInfo.issuer;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Rebase nested Wasm imports even when path redirects are off

With bundle: false and redirect.js.path: false, an inline Wasm file in a nested directory cannot resolve its relative JavaScript imports after being folded into its importer. For example, if src/index.js imports src/nested/read.wasm?inline and that binary imports ./meta.js, the generated code is placed in dist/index.js, but the generic externalizer honors the disabled redirect and leaves ./meta.js unchanged instead of producing ./nested/meta.js. Synthetic Wasm dependencies need mandatory rebasing because their output location changed independently of the user's redirect setting.

Useful? React with 👍 / 👎.

@Timeless0911
Timeless0911 merged commit e697225 into main Sep 23, 2026
12 checks passed
@Timeless0911
Timeless0911 deleted the elecmonkey/wasm-inline branch September 23, 2026 06:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants