Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将webassembly包含在typescript中(不含nodejs)_Typescript_Rust_Webassembly - Fatal编程技术网

如何将webassembly包含在typescript中(不含nodejs)

如何将webassembly包含在typescript中(不含nodejs),typescript,rust,webassembly,Typescript,Rust,Webassembly,我想知道是否有任何方法,尽可能简单,将webassembly包含在typescript中,我不使用node。 如果我在tsconfig.json中激活选项allowJs:true,并手动移动wasm文件,它就会工作。 这个解决方案看起来不是很干净,因为我有两个非常相似的“粘合”文件(network\u calculator\u lib.js)。尽管如此,我还是测试了它是否可以在同一个文件夹中编译,正如我所想象的那样,它不允许覆盖该文件 我希望我解释得足够好。 我使用Rust编译带有wasm包的W

我想知道是否有任何方法,尽可能简单,将webassembly包含在typescript中,我不使用node。 如果我在
tsconfig.json
中激活选项
allowJs:true
,并手动移动wasm文件,它就会工作。 这个解决方案看起来不是很干净,因为我有两个非常相似的“粘合”文件(network\u calculator\u lib.js)。尽管如此,我还是测试了它是否可以在同一个文件夹中编译,正如我所想象的那样,它不允许覆盖该文件

我希望我解释得足够好。 我使用Rust编译带有wasm包的Webassembly,尽管我更希望能够直接导入wasm。 作为一个web服务器,我也在使用一个使用ActixWeb的Rust编写的服务器

如果我问了一些非常愚蠢的问题,请提前道歉和感谢,但大多数人都使用webpack/node,我没有找到任何能澄清我的信息


此代码解决了我的问题:

import init, { exported_wasm_func } from '../pkg/network_calculator_lib.js';
async function run() {
    await init(); // You need to call init before wasm functions.
    document.body.textContent = exported_wasm_func();
}
run()
对于有类似问题的人:

  • 使用web目标选项编译rust:
    wasm包构建——目标web
  • 在HTML中,JS文件或代码需要是模块类型:
  • 在Cargo.toml中:
  • 实际上并不需要Package.json
资料来源:

这本书似乎已经涵盖了你要找的内容。
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"