nshen/vite-plugin-wasm-pack

Referencing a crate in the parent directory as `../` fails

chinoto opened this issue · 2 comments

The error given is

error when starting dev server:
Error: ENOENT: no such file or directory, open '~/mre_vite_outside/my-crate/www/...js'
    at Object.openSync (node:fs:590:3)
    at Object.readFileSync (node:fs:458:35)
    at prepareBuild (~/mre_vite_outside/my-crate/www/node_modules/vite-plugin-wasm-pack/dist/index.js:125:47)
    at async Context.buildStart (~/mre_vite_outside/my-crate/www/node_modules/vite-plugin-wasm-pack/dist/index.js:132:17)
    at async Promise.all (index 2)
    at async Object.buildStart (~/mre_vite_outside/my-crate/www/node_modules/vite/dist/node/chunks/dep-689425f3.js:39230:13)
    at async Server.httpServer.listen (~/mre_vite_outside/my-crate/www/node_modules/vite/dist/node/chunks/dep-689425f3.js:60350:21)

and the contents of my-crate/pkg are copied to to my-crate/www, which results in package.json being overridden.

I've found two workarounds to this; the first is to go up another directory and then name the directory that you came from, which may not work if the crate is also the root of the repo and the repo folder was named differently than expected; the second workaround is to use node's path.resolve method to get an absolute path to ../.
My guess as to why this is a problem is because you're expecting the name of the crate in the last segment of the path?

I've made a minimal reproducible example here: https://github.com/chinoto/mre_vite-plugin-wasm-pack_outside/tree/Fail
My workarounds are the last two commits of the master branch: https://github.com/chinoto/mre_vite-plugin-wasm-pack_outside/commits/master

+1 of appreciation, just hit the same issue.

Yeah. I went down a rabbit hole upon finding this issue. Really it can be fixed by using path.resolve() in the plugin, to get the whole directory structure, because it pulls the crate name off of the last bit of the directory pointed to. Quick fix is ../../my_crate.

But I saw a whole bunch of stuff I wanted to do differently and forked off. We'll see if it ends up merged back into this repo or if I publish with a different name; but check it out. https://github.com/Identikey/vite-plugin-wasm-pack

It runs the wasm-pack command for you and then loads it directly.