无 Bundler
此示例展示了如何使用 `--target web` 标志直接在浏览器中加载代码。对于这种部署策略,不需要像 Webpack 这样的打包器。有关部署的更多信息,请参阅 专用文档。
首先,您需要将 `web-sys` 添加到您的 Cargo.toml 中。
[dependencies.web-sys]
version = "0.3.4"
features = [
'Document',
'Element',
'HtmlElement',
'Node',
'Window',
]
然后,让我们看一下代码,看看当我们使用 `--target web` 时,我们实际上并没有丢失任何功能!
use wasm_bindgen::prelude::*; // Called when the wasm module is instantiated #[wasm_bindgen(start)] fn main() -> Result<(), JsValue> { // Use `web_sys`'s global `window` function to get a handle on the global // window object. let window = web_sys::window().expect("no global `window` exists"); let document = window.document().expect("should have a document on window"); let body = document.body().expect("document should have a body"); // Manufacture the element we're gonna append let val = document.create_element("p")?; val.set_inner_html("Hello from Rust!"); body.append_child(&val)?; Ok(()) } #[wasm_bindgen] pub fn add(a: u32, b: u32) -> u32 { a + b }
否则,其余的部署魔法发生在 `index.html` 中
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
</head>
<body>
<!-- Note the usage of `type=module` here as this is an ES6 module -->
<script type="module">
// Use ES module import syntax to import functionality from the module
// that we have compiled.
//
// Note that the `default` import is an initialization function which
// will "boot" the module and make it ready to use. Currently browsers
// don't support natively imported WebAssembly as an ES module, but
// eventually the manual initialization won't be required!
import init, { add } from './pkg/without_a_bundler.js';
async function run() {
// First up we need to actually load the wasm file, so we use the
// default export to inform it where the wasm file is located on the
// server, and then we wait on the returned promise to wait for the
// wasm to be loaded.
//
// It may look like this: `await init('./pkg/without_a_bundler_bg.wasm');`,
// but there is also a handy default inside `init` function, which uses
// `import.meta` to locate the wasm file relatively to js file.
//
// Note that instead of a string you can also pass in any of the
// following things:
//
// * `WebAssembly.Module`
//
// * `ArrayBuffer`
//
// * `Response`
//
// * `Promise` which returns any of the above, e.g. `fetch("./path/to/wasm")`
//
// This gives you complete control over how the module is loaded
// and compiled.
//
// Also note that the promise, when resolved, yields the wasm module's
// exports which is the same as importing the `*_bg` module in other
// modes
await init();
// And afterwards we can use all the functionality defined in wasm.
const result = add(1, 2);
console.log(`1 + 2 = ${result}`);
if (result !== 3)
throw new Error("wasm addition doesn't work!");
}
run();
</script>
</body>
</html>
注意:由于 CORS 限制,您不能直接在 Web 浏览器中打开 `index.html`。相反,您可以使用 Python 的内置 HTTP 服务器设置一个简单的开发环境
wasm-pack build --target web python3 -m http.server 8080
如果您没有安装 Python,您也可以使用 miniserve,它可以通过 Cargo 安装
cargo install miniserve miniserve . --index "index.html" -p 8080
就是这样!请务必阅读有关 部署选项 的内容,以了解无 Bundler 部署的含义。
使用旧的 `--target no-modules`
使用 `wasm-bindgen` 无 Bundler 的旧版本是使用 `--target no-modules` 标志传递给 `wasm-bindgen` CLI。
虽然与较新的 `--target web` 类似,但 `--target no-modules` 标志有一些注意事项
- 它不支持 本地 JS 代码片段
- 它不会生成 ES 模块
- 它不支持在文档之外使用 `--split-linked-modules`,例如在工作线程中
考虑到这一点,主要区别在于 wasm/JS 代码的加载方式,以下是如何加载与上面相同的模块的 `wasm-pack` 输出的示例。
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
</head>
<body>
<!-- Include the JS generated by `wasm-pack build` -->
<script src='pkg/without_a_bundler_no_modules.js'></script>
<script>
// Like with the `--target web` output the exports are immediately
// available but they won't work until we initialize the module. Unlike
// `--target web`, however, the globals are all stored on a
// `wasm_bindgen` global. The global itself is the initialization
// function and then the properties of the global are all the exported
// functions.
//
// Note that the name `wasm_bindgen` can be configured with the
// `--no-modules-global` CLI flag
const { add } = wasm_bindgen;
async function run() {
await wasm_bindgen();
const result = add(1, 2);
console.log(`1 + 2 = ${result}`);
}
run();
</script>
</body>
</html>