Older versions of the npm Three.js build had a default export in the built file:
exports = module.exports = THREE;
This is considered a "default export" in es6 package language. That means you (used to be able to) do this:
import THREE from 'three';
However, in the current rollup version of Three.js, there is no default export. Instead, there are lots of:
exports.WebGLRenderTargetCube = WebGLRenderTargetCube;
exports.WebGLRenderTarget = WebGLRenderTarget;
exports.WebGLRenderer = WebGLRenderer;
Which is fine, except that this doesn't allow for the above import syntax, breaking backwards compatibility and also goes against intuitive Javascript package principles. For anyone who hits this problem, you can use the less ideal syntax:
import * as THREE from 'three';
Ping @Rich-Harris. I imagine it's a fairly easy change to restore backwards compatibility to include a default export?
import * as THREE from 'three';
...is exactly how you should be importing Three.js in an ES2015 project – THREE is just a namespace. Default exports are for things that 'do something' (i.e. functions).
Is there a downside to having a default export? One annoying thing about that syntax is you can't do
import * as THREE, { Vector3 } from 'three';
Not sure why, but that's a syntax error. I think your reasoning makes sense, but an automatic default option also makes sense to me, even if it's a namespaced object.
Additionally it took a bit of hunting to figure out why this worked in an older project (older three version) and not a newer one.
Is there a downside to having a default export?
It means extra maintenance, lack of consistency in examples and blog posts, and all sorts of interop headaches that arise when you have apps that use transpilers (the things I've seen...!). It's better to do things one way – the correct, future-proof way – and avoid the extra complexity.
Fair enough!
Most helpful comment
...is exactly how you should be importing Three.js in an ES2015 project –
THREEis just a namespace. Default exports are for things that 'do something' (i.e. functions).