#24797·ruffle

ESM consumption breaks resource resolution: document.currentScript is null in module scripts

Author: steve02081504Created Sep 28, 2026Updated Sep 28, 2026
LabelsenhancementA-web

Description

When @ruffle-rs/ruffle is loaded as an ES module (native import(), esm.sh, Vite, or any ESM-only pipeline), Ruffle fails to locate its own runtime resources. It requests ruffle.js and the *.wasm chunks relative to the page origin instead of the directory the module was served from, producing 404s, and the player never initializes.

Root cause

Ruffle discovers the base path of its resources from document.currentScript only:

  • web/packages/core/src/current-script.ts:
    typescript
    if (document.currentScript instanceof HTMLScriptElement && document.currentScript.src !== "") {
        currentScriptURL = new URL(".", src)
    }
  • web/packages/core/src/public-path.ts falls back to "" when currentScriptURL is null:
    typescript
    let path = currentScriptURL?.href ?? ""   // "" when currentScript is unavailable

Per the HTML spec, document.currentScript is always null while a module script is executing. So under ESM consumption the base path becomes "", webpack's public path is set to "", and all runtime assets resolve relative to the document instead of the module. (The core package is now "type": "module" and compiled to ESM, and the published @ruffle-rs/ruffle has no ESM entry — package.json is only "main": "ruffle.js", an IIFE — so ESM users repackage the IIFE and hit this.)

Reproduction

Minimal page, no Flash content needed — the iframe polyfill alone triggers a wrong-origin request:

xml
<!DOCTYPE html><html><body>
<iframe src="/frame.html"></iframe>
<script type="module">
if (!navigator?.plugins?.['Shockwave Flash'])
    window.addEventListener('load', () => import('https://esm.sh/@ruffle-rs/ruffle').catch(() => {}))
</script>
</body></html>

Observed network requests (page served from http://localhost:PORT/):

https://esm.sh/@ruffle-rs/ruffle
https://esm.sh/@ruffle-rs/[email protected]/es2022/ruffle.mjs
http://localhost:PORT/ruffle.js          <- 404, page origin

Loading a movie additionally requests http://localhost:PORT/<hash>.wasm (also 404), because __webpack_public_path__ was set to "".

Expected

Under ESM consumption Ruffle should resolve its own assets relative to the module URL, e.g. https://esm.sh/@ruffle-rs/[email protected]/es2022/ruffle.js / .../72a20ef1c0b8ceb37720.wasm.

Suggested fix

  • In the ESM build, fall back to import.meta.url when document.currentScript is null (e.g. derive currentScriptURL from import.meta.url), or
  • publish/support an official ESM entry for @ruffle-rs/ruffle that uses import.meta.url, or
  • at minimum, document that ESM/bundler consumers must set window.RufflePlayer.config.publicPath before loading Ruffle.

Workarounds

  • Set window.RufflePlayer.config.publicPath = "<module-dir>/" before importing, or
  • load the pre-bundled ruffle.js with a classic <script src=".../ruffle.js"> tag, where document.currentScript is defined.

Versions

@ruffle-rs/[email protected], Chromium (Chrome Dev). Reproduced 2026-09-28.