-
-
Notifications
You must be signed in to change notification settings - Fork 36.2k
Animation: Fix ReferenceError in non-broswer environment #30835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
📦 Bundle sizeFull ESM build, minified and gzipped.
🌳 Bundle size after tree-shakingMinimal build including a renderer, camera, empty scene, and dependencies.
|
52be400 to
a8b860a
Compare
src/renderers/common/Animation.js
Outdated
| this._context = typeof self !== 'undefined' ? self : // browser or Web Worker | ||
| typeof globalThis !== 'undefined' ? globalThis : // ES 2020 | ||
| typeof global !== 'undefined' ? global : // Node.js | ||
| null; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just use globalThis which is the standard for JS? Then build tools can polyfill this based on build target.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not find globalThis used anywhere else in this repository, so I assume it’s some kind of convention.
@mrdoob do you think using globalThis is OK?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The main reason is that we've decided to stick to ES2018 for compatibility reasons. globalThis is ES2020.
However, if the usage of globalThis does not affect build tools like WebPack 4, I'm okay with using it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just doing this similar to WebGLRenderer:
this._context = typeof self !== 'undefined' ? self : null;The code is a bit different but the outcome is the same (context evaluates to null):
three.js/src/renderers/WebGLRenderer.js
Line 1494 in d3ca4be
| if ( typeof self !== 'undefined' ) animation.setContext( self ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just doing this similar to
WebGLRenderer:this._context = typeof self !== 'undefined' ? self : null;
Because I want to polyfill requestAnimationFrame by add it as a global function. If we set _context to null when no self exists, the program would throw here:
| this._requestId = this._context.requestAnimationFrame( update ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even with this polyfill is the renderer actually usable in a node or deno environment? Do you mind explaining your use case a bit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even with this polyfill is the renderer actually usable in a node or deno environment? Do you mind explaining your use case a bit?
Yes. I try to run three.js on Deno with WebGPU1. the polyfill of requestAnimationFrame is
It is hard to resolve the problem with simplely assigning null to this._context, because Animation is not exported from three.webgpu.js (so we cannot modify its prototype ahead of time), and this._animation.start(); is almost right behind this._animation = new Animation(...) in Renderer.prototype.init(). This even cannot be resolved by set a self member on globalThis, because Deno would remove it when calling npm packages.
If this problem is fixed, most demos would work fine.
Footnotes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about moving the code into a new function in src/utils.js and call it getSelf(). In this way, we don't clutter Animation.js. Besides, we might need the same logic elsewhere.
I would not simply use globalThis to avoid issues in older environments or tools.
a8b860a to
ad72e21
Compare
|
@Mugen87 |
ad72e21 to
45dc5f7
Compare
Node.js do not support `self`.
45dc5f7 to
fc79110
Compare
|
I found another way to make my polyfill work (basically using code in One more thing I want to request is, whether we should add export { WebXRController } from './renderers/webxr/WebXRController.js';
+export { Animation } from './renderers/common/Animation.js';
export { FogExp2 } from './scenes/FogExp2.js';This would make it easier for me to polyfill and allow me to use the code in If you don't agree to export |
|
|
Node.js and Deno do not support
self.