emscripten-core / emscripten-core/emscripten

Module.locateFile is not working

Open
#10,434 18 comments 2 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
27.6k
Forks
3.6k
Avg merge
1d 1h
Merged PRs (30d)
105

Description

hello friends, i'm the author of [webp-hero](https://github.com/chase-moskal/webp-hero), a polyfill that uses google's webp decoder clientside via emscripten

i have taken the existing emscripten build of google's libwebp, and wrapped it into an es module myself at `/libwebp/dist/webp.js`, exporting the cwrap'd functions i need

however at runtime, emscripten is looking for `webp.wasm` and `webp.js.mem` at *the server root*, which is the wrong location, so requests are going to `/webp.wasm` when it should be to `/libwebp/dist/webp.wasm` -- so i need to instruct emscripten where to look

i thought using [Module.locateFile](https://emscripten.org/docs/api_reference/module.html#Module.locateFile) should be the solution:

```js
window.Module = {
locateFile: url => {
// never called :(
console.log("LOCATE", url)
debugger
return `./path/${url}`
}
}
// ... emscripten build ...
// then exporting what's needed
```

even though i can see the emscripten runtime ingesting this above configuration -- `locateFile` is not being called even one time -- and emscripten is still looking for the wrong paths

i noticed in the minified emscripten code, it's looking for things like `document.currentScript`, which i don't think is available within an es module? is this not working because i've wrapped the emscripten build into an es module? i mean, i've simply wrapped the output and exported the needed cwrap'd function..

is there any other way to influence how emscripten looks for files? or some way to get this working? is there a way to get emscripten to look for the files relative to the module, rather than at the server's root?

i have a webp-hero [commit](https://github.com/chase-moskal/webp-hero/tree/78193921eb94cae82f27c63f001d7899f421eaee) which demonstrates the issue after `sudo ./libwebp/build && npm install && npm start` then browsing to http://localhost:5000/esm?force

thanks,
:wave: chase

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.