js导入找不到wasm文件

时间:2019-07-18 16:56:50

标签: javascript node.js rust webassembly

我正在努力从rust构建以下wasm示例:

example

我已经执行了

  

货运量

并使用target / debug /文件中的libdom.d和libdom.so文件进行了解析。

应用从给定

开始
  

npm运行服务

但是当js尝试在线导入wasm文件时会抛出错误:

  

import('./ pkg / dom')

导致

> @ serve /home/kamil/projects/dom
> webpack-dev-server

?  Checking for wasm-pack...

ℹ️  Installing wasm-pack 

ℹ 「wds」: Project is running at http://localhost:8080/
ℹ 「wds」: webpack output is served from /
ℹ 「wds」: Content not from webpack is served from /home/kamil/projects/dom
ℹ 「wdm」: wait until bundle finished: /
wasm-pack error: undefined
✖ 「wdm」: Hash: d6af309c4a2560e4a081
Version: webpack 4.36.1
Time: 21666ms
Built at: 07/18/2019 6:53:10 PM
     Asset       Size  Chunks             Chunk Names
index.html  226 bytes          [emitted]  
  index.js    360 KiB    main  [emitted]  main
Entrypoint main = index.js
[0] multi (webpack)-dev-server/client?http://localhost ./index.js 40 bytes {main} [built]
[./index.js] 133 bytes {main} [built]
[./node_modules/ansi-html/index.js] 4.16 KiB {main} [built]
[./node_modules/ansi-regex/index.js] 135 bytes {main} [built]
[./node_modules/html-entities/index.js] 231 bytes {main} [built]
[./node_modules/loglevel/lib/loglevel.js] 7.68 KiB {main} [built]
[./node_modules/strip-ansi/index.js] 161 bytes {main} [built]
[./node_modules/webpack-dev-server/client/index.js?http://localhost] (webpack)-dev-server/client?http://localhost 4.29 KiB {main} [built]
[./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.51 KiB {main} [built]
[./node_modules/webpack-dev-server/client/socket.js] (webpack)-dev-server/client/socket.js 1.53 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/createSocketUrl.js] (webpack)-dev-server/client/utils/createSocketUrl.js 2.77 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/log.js] (webpack)-dev-server/client/utils/log.js 964 bytes {main} [built]
[./node_modules/webpack-dev-server/client/utils/reloadApp.js] (webpack)-dev-server/client/utils/reloadApp.js 1.63 KiB {main} [built]
[./node_modules/webpack-dev-server/client/utils/sendMessage.js] (webpack)-dev-server/client/utils/sendMessage.js 402 bytes {main} [built]
[./node_modules/webpack/hot sync ^\.\/log$] (webpack)/hot sync nonrecursive ^\.\/log$ 170 bytes {main} [built]
    + 18 hidden modules

ERROR in ./index.js
Module not found: Error: Can't resolve './pkg/dom' in '/home/kamil/projects/dom'
 @ ./index.js 3:0-19
Child html-webpack-plugin for "index.html":
     1 asset
    Entrypoint undefined = index.html
    [./node_modules/html-webpack-plugin/lib/loader.js!./index.html] 372 bytes {0} [built]
    [./node_modules/lodash/lodash.js] 528 KiB {0} [built]
    [./node_modules/webpack/buildin/global.js] (webpack)/buildin/global.js 472 bytes {0} [built]
    [./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 497 bytes {0} [built]
ℹ 「wdm」: Failed to compile.

没有./pkg/dom文件夹。我不知道如何生成它,或者应该包含什么。

1 个答案:

答案 0 :(得分:1)

index.js文件正在尝试import('./pkg')不存在。您可以通过运行wasm-pack build来生成此文件夹。之后,您应该有一个./pkg文件夹,其中包含wasm二进制文件和JS包装器代码。有关命令的更多信息,请参见wasm-pack README

在玩Rust时,很诱人的是只调用cargo build并期望它创建wasm二进制文件,但是wasm-pack实际上是doing a lot more的幕后黑手。但最终,wasm-pack build确实会呼叫cargo build