Skip to content
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

Three.js: Log a warning when importing multiple instances of the library #21128

Merged
merged 1 commit into from
Jan 21, 2021

Conversation

mrdoob
Copy link
Owner

@mrdoob mrdoob commented Jan 21, 2021

Related issue: #17482

Description

This change warns the user when they're importing the library more than once.

Screen Shot 2021-01-21 at 5 19 12 PM

This would only work from r125+

@mrdoob mrdoob added this to the r125 milestone Jan 21, 2021
Copy link
Collaborator

@Mugen87 Mugen87 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely better than before 👍

@mrdoob
Copy link
Owner Author

mrdoob commented Jan 21, 2021

Sweet! Lets give it a try...

@mrdoob mrdoob merged commit 952a482 into dev Jan 21, 2021
@mrdoob mrdoob deleted the multiple branch January 21, 2021 20:08
@Mugen87
Copy link
Collaborator

Mugen87 commented Jan 22, 2021

Um, for some reasons I always see the warning when testing the examples. if ( typeof __THREE__ !== undefined ) { always evaluates to true 🤔

@Mugen87
Copy link
Collaborator

Mugen87 commented Jan 22, 2021

Ah wait. undefined should be 'undefined'. The quotes are missing. However, fixing this produces a runtime error in the else clause:

three.module.js:49015 Uncaught ReferenceError: __THREE__ is not defined

@mrdoob
Copy link
Owner Author

mrdoob commented Jan 22, 2021

Indeed! I reverted it to the code I originally tried: 2a853ac

@mrdoob
Copy link
Owner Author

mrdoob commented Jan 22, 2021

A nice side effect of this is that now we'll be able to type __THREE__ in devtools and see if a site is using the library and which version it is 🤓

@donmccurdy
Copy link
Collaborator

donmccurdy commented Jan 22, 2021

I'm not sure if bundlers like Parcel and Webpack will leak the global variable the way simple scripts or older build tools would do. But importing multiple instances is much easier to do accidentally in the latter cases, so this seems helpful all the same. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants