Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
106
rated 0 times [  109] [ 3]  / answers: 1 / hits: 33130  / 6 Years ago, wed, march 7, 2018, 12:00:00

Webpack 4 has added a new feature: it now supports a sideEffects flag in the package.json of the modules it is bundling.



From Webpack 4: released today




Over the past 30 days we have worked closely with each of the frameworks to ensure that they are ready to support webpack 4 in their respective cli’s etc. Even popular library’s like lodash-es, RxJS are supporting the sideEffects flag, so by using their latest version you will see instant bundle size decreases out of the box.




From Webpack docs




The sideEffects: false flag in big-module's package.json indicates that the package's modules have no side effects (on evaluation) and only expose exports. This allows tools like webpack to optimize re-exports.




Whilst the second link shows the results of using the flag, it doesn't clearly explain what constitutes a side-effect. ES6 includes the concept of side-effects for modules as outlined here, but how does this relate to what Webpack considers side-effects.



In the context of the sideEffects flag, what does a module need to avoid to use sideEffects:false without issues, or conversly, what does a module need to do in order to use sideEffects:false without issues.



For completeness, despite @SeanLarkin's solid answer below, I would love to get clarification on the following:




  1. Obviously side-effects means something particular in fp and would include logging (console or elsewhere) and the throwing of errors. I'm assuming in this context these are perfectly acceptable?


  2. Can a module contain circular references and still use sideEffects: false?


  3. Is there any way to verify or that a module is able to verify that a module can sideEffects: false beyond trying to track down errors caused by its misuse?


  4. Are there any other factors that would prevent a module from being able to use sideEffects: false?



More From » webpack

 Answers
9

Sean from the webpack Team! I'll do my best in lieu of our documentation still in progress to answer your question here!


According to the ECMA Module Spec (I'm not going to try and find the link so you'll have to trust me here because it's buried),


whenever a module re-exports all exports, (regardless if used or unused) they need to be evaluated and executed in case one of those exports created a side-effect with another.


For example I've created a small scenario with a photo to better visualize the case:


In this photo we see three modules imported into a single file. The imported modules are then re-exported from that module:


Example


You can see here that none of the re-exports are effected by each other, therefore (if webpack was given a signal), we could omit exports b and c from even being traced or used (size and build time performance benefits).


enter


However in this case, we see that exports c is "effected" by local state changes because it is reassigned to the summation of b and a. Therefore, (which is why the spec calls for this), we would need to include both b and a and any of its dependencies into the bundle.


We chose "sideEffects: false" as a way to save on both compile time and build size because this allows us to instantly prune (explicitly) exports that developers/library authors know are side-effect free (at the expense of a property in a package.json, or 2-3 more lines of config).


Although technically this example is very primitive, when you start dealing with Frameworks or Libraries that re-export a bunch of modules up to a higher level for Developer Experience (Three.js, Angular, lodash-es, etc), then performance gains are significant when (if they are sideEffect free module exports) you flag them in this manner.


Additional Clarifications:




  1. Obviously side-effects means something particular in fp and would include logging (console or elsewhere) and the throwing of errors. I'm assuming in this context these are perfectly acceptable?



In the case that this is trying to solve, yes. As long as effects created against module exports aren't effected by others that would cause pruning to not be acceptable.




  1. Can a module contain circular references and still use sideEffects: false?



It should, in theory.




  1. Is there any way to verify or that a module is able to use sideEffects: false beyond trying to track down errors caused by its misuse?



Not that I know of, however this would be a great tool.




  1. Are there any other factors that would prevent a module from being able to use sideEffects: false?



If the property is not in package.json or defined in module.rules, or mode: production is not set (which leverages the optimization).


[#54993] Sunday, March 4, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
elishaannac

Total Points: 28
Total Questions: 97
Total Answers: 101

Location: Samoa
Member since Mon, Nov 8, 2021
3 Years ago
elishaannac questions
Sun, Dec 5, 21, 00:00, 3 Years ago
Mon, Jun 14, 21, 00:00, 3 Years ago
Mon, Jul 22, 19, 00:00, 5 Years ago
Mon, Jul 8, 19, 00:00, 5 Years ago
;