My question is why did the method of importing components from /examples change? Was it to get better tree-shaking? I’ve been seeing several users running into importing issues on Twitter, StackOverflow, and here lately, especially since import-maps have very poor browser support.
So I’m just trying to get a better understanding, why did this change to import-maps take place?
I don’t think there is, because it’s part of the Three.js module itself. Additionally, I wouldn’t want to disable that warning because I do want to know if I’m accidentally importing multiple instances.
The ES6 module syntax is actually incomplete without import maps. Only with import maps you can use bare module specifiers in browsers which is a prerequisite for a unique import syntax.
The problem is that modules like OrbitControls should have a unique way for importing dependencies. Before import maps were introduced, it was necessary to have different imports in the npm package and the repository.
npm:
import { EventDispatcher, Vector3 } from 'three';
Repo:
import { EventDispatcher, Vector3 } from '../../build/three.module.js';
Import maps solve this difference by always using the npm style.
This syntax ought to work in webpack and other modern bundlers both before and after r137 though, shouldn’t it? Import maps solve a problem in web browsers but I don’t think you need a manual alias in a bundler here, I’m using webpack with r139 without this issue.
And yeah, the “Multiple instances of threejs…” warning is a pretty important one — can cause very odd bugs when this happens.
then webpack will ensure that all occurrences of three that it finds in any of your imported scripts while bundling, will be linked with what it finds at ./node_modules/three.
If you are still seeing this error in the browser console after you’ve recreated the bundle, then it is likely that you are importing a script outside of the bundle. Check your HTML doesn’t import any other libs outside of the bundle.
If you are importing the bundle, then you also don’t need to set any import maps because all the required scripts are already referenced and included in the bundle.
Also check the network tab in your browser developer tools to see whether the bundle is being downloaded, plus any other JS scripts that me be also downloaded as well, that may contain another reference to threejs outside of the bundle.
I have checked this with Three r140.2 and the glTFloader and don’t have this problem since all my scripts were in the single bundle created by webpack. No need for importmap in this case.
You could also try npm install again to ensure the installed libs in your node_modules folder actually match what is written in your package.json before bundling.
Its not really why you need it, its if you need it.
I think the misunderstanding is that if you are using webpack, then you don’t need importmaps as well in your html. because you are no longer using bare module specifiers to reference the threejs libraries client side.
If you need a webpack alias configuration then something else would seem to be wrong, I think. A minimal repro would be helpful for understanding what’s going on.