Skip to content

Loading extra files like the .wasm #5104

Closed
@kripken

Description

@kripken

Consider a library like ammo.js or box2d.js, where a site can have structure like this:

ammo.js
demos/
  demo1/
    index.html
  demo2/
    index.html

Both html files can have <script src="../../ammo.js"></script>. That way, we have one copy of ammo.js, used by multiple locations, and this just works. And likewise, if the code is run in a worker, the worker can do `importScripts("../../ammo.js");

If, on the other hand, there is an extra file like ammo.js.mem or ammo.wasm, that is loaded by ammo.js, then things are trickier. If ammo.js just loads the URL "ammo.wasm" then it won't find it - the current directory is demos/demoX/. There hasn't been a great solution to this, so to work around it we avoided such extra files, disabling the .mem file in particular - despite the downsides of doing so (larger code size, slower startup).

But with wasm this is unavoidable - we have to have another file.

Some options:

  • Modify the URL inside ammo.js, either to an absolute path like "http://mysite.com/ammo.wasm" or the proper relative one "../../ammo.wasm". The relative path won't work if it can be called from different nesting depths, so really only the absolute one seems like an option here. But it seems bad to ask users to do this, and do it every time they move the file around...
  • Auto-detect the proper relative path. This seems possible using document.currentScript.src, but this is not available in a web worker. Another issue is that the code may not be in a normal script tag if it is evaled (but that shouldn't be done, so maybe we can ignore it).

Maybe there's a better way to do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions