Two more episodes this week of In The Land of Sloppiness, as promised:
#1. After your last post, I gave Vite another try. First, I tried to update node.js and I got this nice message:
I have Win7 x64 and I’m not willing to spend a week upgrading to Windows 10 150GBs of program installations and adjustments -not now, so I tried to find the latest node.js version that my OS supports by trial-and-error, downloading and attempting to run each version (I couldn’t find that info in release text).
Then, I tried to install and run Vite once more. After a tsunami of different errors and different attempts, the issue sums up to the following error:
Note the irony: It requires either node version 10, 12, or >=14 but I had… 13.14.0 and so it didn’t work (what was I thinking - a bad-luck number? No way, only even ones!
)
#2. Long story short, I found redemption in esbuild, a rather new bundler.
For anyone interested, here is the (tested) procedure:
-
Make and name a new folder, then open it.
-
Ctrl + right-click, select “Open command window here” and type + enter:
-
npm init
-
npm install three
-
npm install --save @types/three
-
(optionally, install other libraries/packages you might need)
-
npm install esbuild
-
Open package.json and make sure “main” points to your source javascript file eg:
"main": "script.js",
(comma including)
-
Bellow “scripts” in package.json add the following line:
"build": "esbuild ./script.js --bundle --minify --sourcemap --outfile=out.js",
-
Also bellow “license” add:
"private":true,
to avoid accidental publishment (as has been suggested by others).
-
Copy/create in this folder your html file and point the script tag to the output build name you entered in project.json without referring to module type, like so:
<script src="out.js" defer></script>
You can put it either inside the <head>
or <body>
element
“defer” is optional and is used to call the script after HTML has fully loaded.
-
make a bat file named “BUILD.bat” with the following:
npm run build
-
Copy/create in this folder your source javascript file. Open it and make sure you point to THREE.js module like so:
import * as THREE from "three";
Note: do NOT include the full path, if you do, it will affect intellisense! (insane, but true).
The difference will be this:
vs this:
For extra modules, include them like so:
import { GLTFLoader } from './node_modules/three/examples/jsm/loaders/GLTFLoader.js';
import { DRACOLoader } from './node_modules/three/examples/jsm/loaders/DRACOLoader.js';
- include the local server of your choice.
- You have to remember to double-click the BUILD.bat file before testing.
Pros:
a) I don’t have to hack the paths anymore in order for an ES6-modules based web-page to work on the browser (as esbuild bundles it and converts it to a non-module output script).
b) Intellisense works properly.
Cons:
a) I have to remember double-clicking the BUILD.bat file every time before testing on the browser.
b) I’ve seen some error reports pointing to the packed output file instead of the source (which doesn’t help), but I was using PaleMoon, so I’m not sure yet if Chrome will behave the same.
More info: esbuild - Getting Started