Closed
Description
Description of the problem
Here is a reproducible example, I have in my js file just the THREE import and after that the CanvasRenderer code as taken from here:
import * as THREE from 'three';
import './CanvasRenderer'; // or Projector
The error in the console is
Uncaught TypeError: Cannot set property d of #<Object> which has only a getter.
which happens when you assign the function on this line
THREE.CanvasRenderer = function ( parameters ) {
Basically says that THREE.CanvasRenderer
already exists and is not configurable, and indeed it does exist as a deprecation warning in Three.Legacy.js.
The same happens for Projector. As a bundler I use Webpack@2.2.0.
Sorry I wouldn't know what to export instead of that function in order to bypass this error.
Three.js version
- r84
Activity
mrdoob commentedon Jan 23, 2017
Hmm, yeah... we still don't know how to modularise the stuff in the examples folder without breaking traditional workflows...
marcofugaro commentedon Jan 23, 2017
What do you mean modularise stuff? Do you need any help?
cecilemuller commentedon Jan 23, 2017
@marcofugaro This thread might provide some context about the modules in examples issue: #9562
Basically, js from the examples folder aren't ES Modules, so they can't be be used with the "import" keyword yet.
marcofugaro commentedon Jan 23, 2017
Oh, the other day I did this library because I needed some Three.js addons in my workflow, maybe could be of any help.
Of course the only issue is with the two deprecation notice functions, as this issue reports.
cecilemuller commentedon Jan 25, 2017
In the case of CanvasRenderer, the issue here is the conflict with the export from Three.Legacy.js: should CanvasRenderer be part of the core or only be in examples ?
But as for modularising examples in general: as long as we only have one file per example, it won't be usable both as simple <script> tag and as import-able module.
I think the cleanest solution would be to have each example as an ES module (that can be used for "import"), and a build target for each example (to generate a version that can be used for the classic <script> workflow).
The other main constraint is examples inject themselves in the THREE namespace.
ES Modules would reference the functions directly:
But classic workflow would expect "THREE.SVGRenderer":
So the build target for classic would have to append append something like "THREE.SVGRenderer = SVGRenderer" to the built file.
Mugen87 commentedon Apr 23, 2017
Closing. As @cecilemuller mentioned, #9562 is the leading issue for this topic.
cecilemuller commentedon Apr 24, 2017
In the meantime, if you use Webpack, you can manually alias files from "examples" to
import
this way, so even if it's not as clean as examples being ES Modules, at least it's in the same bundle instead of having to have a<script>
for each file:In
webpack.config.js
:In
application.js
:ri commentedon May 24, 2017
I found that @cecilemuller's excellent workaround only worked with r82 in my situation (with Webpack 2 and using a legacy example).
In particular, cf97517 causes Webpack 2 to export the
THREE
properties in a way that can not be replaced andCanvasRenderer
(unlikeOrbitControls
in @cecilemuller's example) has a pre-existing definition onTHREE
.As far as I can tell, the builtin
THREE
functions are exported using getters instead of object properties. In my case, cf97517 is the difference between these two compiled export styles:To work with r83 and above, I just had to explicitly load the UMD version in the
ProvidesPlugin
:ghost commentedon Dec 19, 2017
The only way for me without changing the sources or using an outdated 'custom' npm package was (r89, Webpack3, ES6):
With Object.unfreeze declared before (found : here )
zstauber commentedon Feb 15, 2018
I use three.js with RequireJS (my preferred AMD loader) and have been able to successfully reincorporate CanvasRenderer.js and Projector.js into build/three.js in R90 if anyone wants a copy.
My use case was trying to use it to render a model of the solar system in Chrome on a Raspberry Pi 2 Model B, which simply not playing nice with WebGLRenderer in any way, shape, or form, including any of the examples on the threejs.org site, even after enabling the "Software override rendering list." :(
BennyHinrichs commentedon Feb 23, 2018
@pmaster06 My dude. I've been working on this for like 4 hours, and your solution is the only thing that's worked!
zstauber commentedon Feb 23, 2018
I just realized I could attach a file here. Just use CanvasRenderer like you used to.
three.js.zip
morgansegura commentedon May 24, 2018
Thank you, very helpful!
MagicLeeW commentedon Dec 5, 2018
@pmaster06 thx,very helpful