Hi.
I’m a noob to JavaScript and Three.js. Sorry for a long post.
TL;DR I managed to hack together working thing using JS and Three.js. So far I had an simple index.html where I had script tags for Three.js and GLTFLoader, then a script for my code that loaded glTF model for display. Converting this to TS is the issue, I can’t get imports working, and I’m also lacking info how to make TS version run in browser…
JS to TS
When I tried to convert this mini project to Typescript (while learning TS), I got stuck. I don’t know if it is even possible to convert TS into JS that works in browser by just doing “tsc” to compile files into JS. When trying to compile TS files into JS, and then open the index.html page (that points to compiled js) from browser, I get errors that require doesn’t exist, which led me to thing that TS can’t actually make working JS+html compo with just by running tsc. I haven’t found any proper articles that would give general overview on this topic only some esoteric problem descriptions and articles that are way above my understanding on this topic.
After going through official TS intro pages, I noticed in their Gulp tutorial they used Browserify (which I was not familiar with) to somehow package the TS code into one file that browser can understand. I followed the tutorial, and for simple TS files this seemed to work just fine, resulting in html page + JS that ran without errors. But this didn’t work for my TS files that had imports for THREE.js;
1. General import errors.
First I had errors with local copy of Three.js. I just copied the three.js file into project folder. However when I tried importing it with:
"import * as THREE from 'threePathHere';
I got errors in code, like THREE wasn’t even there (not possible to access functions of THREE.*). Then I swapped to version in Node npm package repository, and now the import seems to give no errors.
2. The GLTFLoader doesn’t seem to want to cooperate.
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
And this seems to be no-go - trying this gives no error, but when trying to compile with browserify, I get error about GLTFLoader:
SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (9:0) while parsing PATHHERE\GLTFLoader.js while parsing file: PATHHERE\GLTFLoader.js
Seems like the issue is somehow related to using these JS files in TS project, yet I don’t have any means yet to spot what is going on…
Questions:
Do I actually even need browserify, can I somehow avoid whole bundling thing? Just JS + html?
Can I actually import THREE or any other similar libraries to TS project with import?
Any pointers and general explanations would be helpful!