Shipping both three.webgl.js
and three.module.js
with duplicate Three.js code causes a duplicate-threejs hazard when any projects import both.
It is too easy for one library to import from 'three'
and some other library to import from three/tsl
or three/webgpu
. One or more of the following warning may appear in console:
WARNING: Multiple instances of Three.js being imported.
When this happens, there are a couple ways to fix it:
- vanilla browser ESM users can easily modify their
importmap
- people with a build, especially people new to web development, may have a harder time trying to figure how to alias their tooling to the correct paths.
- some people may be oblivous, and their app accidentally works (later it may break)
The original philosophy of Three.js is to keep things as simple as possible. For example that’s why TypeScript is not part of core, because it adds complexity that the main maintainers don’t want.
Likewise, the current Rollup build and the Node-specific exports
field in package.json add a barrier of complexity for people who don’t know web tooling, or for people who are not into Node.js but trying to consume code written with Node.js standards (which are not web standards).
My thought is that the duplicate problem can be eliminated at least in the build/
folder.
Here’s my suggestion:
- make
src/Three.WebGPU.js
re-export stuff from./Three.js
instead of duplicating the exports - then build
three.module.js
the same as usual - then build
three.webgpu.js
withthree.module.js
as an external module (f.e.three.webgpu.js
will haveexport * from 'three'
orexport * from './three.module.js'
, I think the last one is better for plain ESM, plus either one can be overriden in animportmap
).
Downside (worst case): two files are downloaded for Three.js for vanilla JSM users instead of one, the second file now provides extra WebGPU stuff. Two files instead of one is not a bad compromise for the overall gain:
Upside: the duplicate Three.js problem will be eliminated, and the setup made overall simpler with less likely of tooling issues needing to be configured. Libraries can freely import from three
, three/tsl
, or three/webgpu
without causing tooling conflicts or duplicate Three.js instances. With this simpler setup, existing projects could just add an import
statement and move forward.
I believe this change will be non-breaking, and if anything it may fix some projects that may currently have accidental duplicate Three.js libs.
P.S. happy to try to make this change if approved to move forward with it.