question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add sideEffects: false to package.json.

See original GitHub issue

Hello D3 Family!!! @TheLarkInn from webpack here!

We have been working on a new feature for webpack 4+ which allow’s library authors to add a special field to package.json allowing them to declare that their libraries that have “reexports” in them do not contain side effects!

Tiny Backstory

Because webpack implements the Harmony Module Specification to spec, we include code that was reexported (even if it wasn’t used). This is because all code [per spec] must be eval’d). The beautiful news is that we do not have to implement exactly to spec. So we have created what is called “pure-module”. webpack as imports are being resolved will also check to see if the module is pure (which is indicated through the pure-module flag in the package.json fields). Therefore, there would be no bundle size difference between the following examples:

import {bottomAxis} from "d3-axis";

and

import bottomAxis from "d3-axis/direct/path/to/d3AxisBottom"

Proposal

If there are no side-effects within the D3 packages in places where re-exports occur, then I’d love to propose that all D3 packages add pure-module: true to their package.json.

Pros

Developers (using webpack) get huge perf wins by doing nothing.

Cons

Bit more work to get it all done.

Please let me know if you have any questions, the webpack team is really anxious to help in any way if we can (that means, submitting PR’s, adding issues to individual repo’s to track, etc).

sideEffects: true

sideEffects: false

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:14
  • Comments:35 (11 by maintainers)

github_iconTop GitHub Comments

15reactions
TheLarkInncommented, Aug 8, 2017

How can we test that it is working as intended?

For now you could pull down the PR to test it out. (In the future we are working on webpack-canary which can help us and everyone else test webpack versions/commits/branches against anything they want.

Currently today the behavior is this (I’m using a plugin in VSCode that calculates the cost based on way it is imported):

Larger sized because reexported code is being evaluated and included in bundle even if not used d3importlarge

Smaller sized because importing directly from module w/ no reexports d3importsmall

So with this pure-module change, you will know its working in that you can use either syntax seen above and the size should be representative of the second image. (aka it should be small no matter what for any pure modules).

Most D3 modules should be side-effect free, but I know of at least a few places that are not, and I don’t know whether there is a way to restructure them to make them “pure”.

I think there are going to be times where its not really avoidable and that’s totally fine. If that’s the case you can leave the property off for those specific modules and I think that’s totally fine. In terms of suggestions to do it another way, I’m not quite sure what would be the easiest approach or if even possible.

Once webpack-canary is closer to a place where it could be leveraged in lets say unit/functional tests, I’ll let you know, but otherwise if you wanted to try it out earlier you can try and run webpack from this PR.

Let me know if there’s any other ways we can help. When I find some time in the near future, I’ll try and run the PR locally against a modded package.json file for d3 and make sure it is doing the expected behaviors myself. Thank you for the collaboration. This is going to be an awesome out of the box perf win when people use d3 with webpack 4 and that’s what excites me the most.

5reactions
TheLarkInncommented, Mar 19, 2018

Here is a better explanation that I provided for a user on Stack Overflow. Please let me know if this answers your questions.

https://stackoverflow.com/a/49203452/1624684

Read more comments on GitHub >

github_iconTop Results From Across the Web

false flag to package.json to allow tree shaking · Issue #16059 ...
Consider adding "sideEffects": false to package.json so that bundlers like webpack are able to tree shake three.js.
Read more >
What Does Webpack 4 Expect From A Package With sideEffects
The "sideEffects": false flag in big-module's package.json indicates that the package's modules have no side effects (on evaluation) and ...
Read more >
Everything you never wanted to know about side effects
If your library doesn't include side effects, set sideEffects: false in its package.json . If your library does include side effects, you can ......
Read more >
Tree Shaking - webpack
A "side effect" is defined as code that performs a special behavior when imported, other than exposing one or more exports. An example...
Read more >
Angular package format
json is to declare whether the package has side effects. content_copy { "sideEffects": false }.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found