Skip to content

Conversation

@0xfffxfff
Copy link

@0xfffxfff 0xfffxfff commented May 25, 2025

TL;DR: This PR adds a version of the gunzipScript that supports ESM modules. Basis for discussion: Do not merge yet.

Problem

Three.js r161 and onward is forcing developers to use ESM. This makes it impossible to use the latest threejs versions with ethfs and the gunzip script and would force users to bundle and upload threejs as part of their own js themselves. Making a umd build doesnt work because the addons use esm imports as well AFAICT.

Solution

This PR adds gunzipScripts-esm.js that:

  • Decompresses gzipped ESM modules from inline data URIs (using type="text/javascript+gzip;module")
  • Rewrites relative import paths to work with blob URLs before handing it over to es-module-shims
  • Result: Decompressed modules can import cross-module (which again AFAICT isn't possible with ESM if you want to work with inline scripts)
<script type="text/javascript+gzip;module" data-path="three" src="data:application/gzip;base64,..."></script><!-- three.module.js -->
<script type="text/javascript+gzip;module" data-path="./three.core.js" src="data:application/gzip;base64,..."></script>
<script type="text/javascript+gzip;module" data-path="three/addons/controls/OrbitControls.js" src="data:application/gzip;base64,..."></script>

<script type="module-shim">
    import * as THREE from 'three';
    import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
    // .... 
</script>

<!-- gunzipScripts-esm.js -->
<script src="data:text/javascript;base64,..."></script>

Caveats

  • Consider this a proposal / basis for discussion. It's quite possible there's a cleaner, simpler and smaller solution (currently 50KB)
  • This was quickly put together using Claude Code. Even though it has a comprehensive test suite, I'd still would wanna do a thorough review before merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant