Closed sigoden closed 1 year ago
Could you add some test that fails without this PR and passes with it? I personally couldn't reproduce this issue.
This happend in dev mode, I'm afraid the tests need to be refactored.
Files to reproduce this issue.
package.json
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"@jsona/openapi": "^0.2.1",
"vite": "^3.1.0",
"vite-plugin-wasm": "^2.1.0",
"vite-plugin-top-level-await": "^1.1.1"
}
}
vite.config.js
import { defineConfig } from "vite";
import wasm from "vite-plugin-wasm";
import topLevelAwait from "vite-plugin-top-level-await";
export default defineConfig({
plugins: [
wasm(),
topLevelAwait(),
]
})
index.html
<body>
<script type="module" src="/main.js"></script>
</body>
main.js
import { parse } from "@jsona/openapi";
console.log(parse("{}"));
I merged your code with latest main changes and added unit test. Some circular dependency happened in @syntect/wasm
was found. Could you please check the error?
https://github.com/vitejs/vite/pull/10141 fix version query.
The easiest way to fix this problem now is to exclude wasm js module from pre-bundling (vite >= 3.1.2)
optimizeDeps: {
exclude: [
"@jsona/openapi"
]
},
In order to solve this problem perfectly, vite needs to provide api reverse lookup importer.
This pr solve #1.
Why some wasm functions return undefined?
If you import a wasm js module:
vite
will compile this to:NOTE: vite append a version hash to module path.
vite-wasm-plugin
will resolve this to:vite-wasm-plugin
imports the importer module/node_modules/@jsona/openapi/index_bg.js
without a version hash.That's the problem. They are two different modules because their paths are different.
If you run non-local wasm js module(vite don't add version hash to non-node_module files), call a function depends on
__wbindgen*
function, it will return undefined.