Ask coding questions

← Back to all posts
Possible to support using script as ES6 and in <script>
tussiez (1668)

Is it possible to "test" if a script is being imported as a module, or being included as a <script> tag?
I'm trying to make it possible to use SortaCanvas as a module, and as a "normal" library.

Comments
hotnewtop
Coder100 (18199)

you know how three.js does it? They have two separate versions
.mjs/.module.js and .js

tussiez (1668)

@Coder100 Yes. My question is, would it be possible to use only one file?

YodaCode (116)

@tussiez I mean, I guess you could use require(“fs”) inside of a try catch and if it fails, you’re likely in the browser because it doesn’t have the fs module. Just a guess; I haven’t tested it.

tussiez (1668)

@YodaCode I meant browser ES6 (e.g import * as THREE)

YodaCode (116)

@tussiez You can import stuff in the browser?

YodaCode (116)

@tussiez Lol. How does that work?

tussiez (1668)

@YodaCode
@Coder100 , huh?
<script src="script.js" type="module">
script.js:

import * as THREE from 'https://threejs.org/build/three.module.js';
// ..code
Coder100 (18199)

@tussiez .module.js did you see that

tussiez (1668)

@Coder100 yes, my question is (once again) is it possible to do this in one file?