question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Three.js source does not work as ES6 module

See original GitHub issue
Description of the problem

Three.js source cannot be imported as ES6 module. Developers should be able to use Three.js in pure ES6 module form.

For example, the following should work:

<script type="module">
import * as THREE from "./src/Three.js";
</script>

Instead, we get errors like this one:

Failed to load module script: The server responded with a non-JavaScript MIME type of "application/octet-stream". Strict MIME type checking is enforced for module scripts per HTML spec.

This happens because glsl shader chunks are not real modules and need to be processed during build step by rollup glsl plugin.

Fixing this would improve development experience and make it possible to use threejs without build step or even without any npm dependencies at all.

Motivation

ES6 modules are a great improvement to JavaScript. Together with HTTP/2 network protocol which eliminates multiple HTTP request overhead they enable seamless development experience without continuous build process. Building, bundling and minification should most definitely still be part of the deployment strategy, but for development process, it is not necessary. Here is an interesting article about ES6 modules by contentful.

This issue is about enabling threejs to work without any build process and without npm install during development.

Suggested solutions
  1. <del>Rollup ShadersChunkSrc.js into ShaderChunk.js separately before building the library</del>. This solution was attempted in PR #14447 but it did not reach consensus. Main issue was that this approach results with a partial “build” inside src/ directory.

  2. Convert each individual glsl shader chunk into ES6 module (automatically). This approach would suffer from the same issue as no.1 but some prefer it better.

  3. Rewrite and maintain shader chunks as ES6 modules using template literals. For example:

var encodings_fragment = glsl`
 gl_FragColor = linearToOutputTexel( gl_FragColor );
`;
export {encodings_fragment};
  1. Another solution suggested by @looeee: do nothing (or write better documentation): it will not be possible to import the three.js src directly in the browser, and people bundling the source themselves have to add plugins such as glslify to their build step (as they currently do)
Three.js version
  • [x ] Dev
Browser
  • All of them
OS
  • All of them

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dmnsgncommented, Dec 25, 2018

@arodic @mrdoob Just created a straightforward PR that could help with syntax highlighting of glsl templates literals: https://github.com/mrdoob/three.js/pull/15473 (more infos in PR)

 // Advantage here is that there would be no need for a glsl tag function, just a comment
+ var encodings_fragment = glsl`
- var encodings_fragment = /* glsl */`
  gl_FragColor = linearToOutputTexel( gl_FragColor );
 `;
export {encodings_fragment};
0reactions
Iteecommented, Sep 12, 2018

@arodic Did you take look at three-full ? All shaders are put in the same folder and are es6 modules. In that way you should be able to import them in the way you want. Waiting that #9562 be close.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why my import * as THREE from *three* didn't work? - Questions
I need to use three.js to do my school work and when I try run the code, appear nothing. I already did npm...
Read more >
unable to import THREE.js in es6 - Stack Overflow
The problem is is that you are importing THREE as es6 modules, but then trying to include DDSLoader, MTLLoader and OBJLoader which are...
Read more >
Importing Three.js as Module - Medium
Importing Three.js as Module. I am going to show you the simplest way to use the Three.js ES6 modules in your Three.js projects....
Read more >
JavaScript Modules - Discover three.js!
Discover three.js is now open source! ... All modern browsers now support ES6 modules, however, Node.js has been slow to catch up. Fortunately,...
Read more >
Importing Three.js as a Module - YouTube
When updating older Threejs projects, you may get a warning in the browser such as, "As part of the transition to ES6 Modules,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found