ESM consumption breaks resource resolution: document.currentScript is null in module scripts
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:if (document.currentScript instanceof HTMLScriptElement && document.currentScript.src !== "") { currentScriptURL = new URL(".", src) }web/packages/core/src/public-path.tsfalls back to""whencurrentScriptURLis null: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:
<!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 originLoading 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.urlwhendocument.currentScriptisnull(e.g. derivecurrentScriptURLfromimport.meta.url), or - publish/support an official ESM entry for
@ruffle-rs/rufflethat usesimport.meta.url, or - at minimum, document that ESM/bundler consumers must set
window.RufflePlayer.config.publicPathbefore loading Ruffle.
Workarounds
- Set
window.RufflePlayer.config.publicPath = "<module-dir>/"before importing, or - load the pre-bundled
ruffle.jswith a classic<script src=".../ruffle.js">tag, wheredocument.currentScriptis defined.
Versions
@ruffle-rs/[email protected], Chromium (Chrome Dev). Reproduced 2026-09-28.
Source: ruffle-rs/ruffle