Allow tree-shaking by adding "sideEffects": false flag#16317
Allow tree-shaking by adding "sideEffects": false flag#16317marcofugaro wants to merge 1 commit intomrdoob:devfrom marcofugaro:side-effects
Conversation
|
Why does the And I'm not sure if I agree that we should encourage importing the source file directly. I'm also not sure how this will work with plugins that try to import the existing three.js examples such as the @wildpeaks/three-webpack-plugin or when importing the new jsm ones. Have you given that a try? |
|
Yes, if enabled with the sideEffects flag, tree-shaking would work even with the single file function Vector3 () {
// ...
}
Objec.assign(Vector3.prototype, {
set: function() {}
// ...
})which is the structure of the entire codebase of three.js. It's not encouraged but many people area already doing it. It's for people who know what they're doing. Regarding the jsm, they would be a problem, since they import from the |
Sounds like this would potentially bring confusion to a lot of users then? |
|
TBH I think the right approach here is to change the library so that it works with tree-shaking. We should only recommend that people import from @marcofugaro is it correct that if we change this style: function Vector3 ( x, y, z ) {
...
}
Objec.assign(Vector3.prototype, {
set: function() {}
...
})to class Vector3 {
constructor( x, y, z ) { ... }
set( x, y, z ) { ... }
...
}then tree-shaking will work? Class support is higher than script type="module", but again we lose IE11 support. However, people can run their code through Babel to get that support in their apps if they need it. |
|
Closing this for now. |
|
@marcofugaro there's already discussion of adopting ES6 classes in #11552, so if you want to continue this conversation please do so there. We're proceeding step by step with adopting ES6 features, and the current focus is on converting Once these are done, we can move on to the next step. IMO making tree-shaking work should be given the highest priority. |
|
Still webpack checks if there is sideEffects: false. Then you can still |
|
Decided to give it a go: #21313 |
This PR adds the flag
"sideEffects": falsein the package.json, with the objective to allow tree-shaking for webpack users. More precisely this will allow people to dowhile including only the components which are actually used in the code in the bundle.
Example
Setting
sideEffectsto false means there isn't any shared "state" in three.js which is affected by other non-required files (side effects). This is true since three.js' core uses only import/exports and isn't using any non-imported module (correct me if I'm wrong, don't know 100% of three.js' core).More info here