then it works fine. we can’t mix a local three with remote packages, or target a specific entry file (build/three.module.js) or else there will be multiple three’s and namespaces. but then it will work fine.
I’m running into this error : The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script.
so you’re trying to load an mjs or “browser esm” with dynamic imports? if you’re using a bundler, imo you shouldn’t have script tags in your application, nor externals. you just import/dynamic import locals and/or npm modules from index.js.
what i usually do is make one module for everything that has to do with the canvas, then i dynamic import it into the root app. there won’t be any issues with that, nor is it dependent on systems supporting dynamic imports or not. usually people also combine this with routes, every route is a dynamic import.
index.js
...
const { ... } = await import ('./canvas)
canvas.js
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
export { ... }
you normally don’t add any of these to index.html, the bundler should do that for you. or at least it only has the main entry (index.js).
Bsaed on what is written in webpack’s documentation there are two ways of resolving dynamic import:
Two similar techniques are supported by webpack when it comes to dynamic code splitting. The first and recommended approach is to use the import() syntax that conforms to the ECMAScript proposal for dynamic imports. The legacy, webpack-specific approach is to use require.ensure. Let’s try using the first of these two approaches…
I suggest you to update your webpack, if this solution works for you:
@gorskidev@drcmda thanks again for all this information ! In my case all my app including three is loaded conditionally from a tiny script like the Google Analytics snippet you paste in <head>, it is the first time I do that so I’m a bit lost.
I found that my original attempt of loading three dynamically via a script tag ( which is still the only method supported by all browsers ) didn’t work only with skypack. Doing the same with an unpkg url works fine, I can find three in window.THREE. Go figure…
hmm, it seems a little fragile. this is a 1.6 megabyte bomb through a remote cdn bundler, it will 100% have service fallouts in prod. it’s not a module but the umd, if you have use additional modules (three/jsm) you’ll end up with two or more threejs. for unpkg you normally need the ?module postfix: https://unpkg.com/three@0.131.0?module
which is still the only method supported by all browsers
you are right, but browser esm is not real, it can’t be used in browsers without bundlers (webpack, skypack, unpkg, etc), you need something that resolves dependencies. if you introduce browser semantics (script tags), it will adverse effects. only a suggestion ofc.
This moderately improved Lighthouse scores but more importantly it made the page interactive way faster - you can interact with the text pretty much instantly even when the network is throttled to 2G speeds.