Add sideEffects directive to all package.json#9961
Merged
Conversation
de6b69f to
335ebf5
Compare
mitchellhenke
approved these changes
Feb 6, 2024
changelog: Internal, Performance, Reduce size of JavaScript by removing unreachable code
335ebf5 to
e341be9
Compare
2 tasks
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🛠 Summary of changes
Adds (and enforces) the presence of a
sideEffectsdirective within each package'spackage.json.Why?
By default, Webpack and other common bundlers assume that any imported code may include side effects, and as such will not aggressively try to perform dead code elimination. Explicitly defining side effects (or the absence of them) allows Webpack to perform this optimization.
See also:
In practice, I noticed this by looking at the
platform-authenticator-avaiable-*.jsfile loaded on https://secure.login.gov/ , which includes the snippet of codenew Set(["NotAllowedError", "TimeoutError", "InvalidStateError"]);irrelevant for that bundle.Performance Impact
Total JavaScript size:
du -s public/packs/jsBefore: 12240kb
After: 12176kb
Diff: -64kb (-0.5%)
Critical path (http://localhost:3000), with gzip enabled:
Before: 6.4kb
After: 6.3kb
Diff: -0.1kb (-1.6%)
Observation: The benefit here isn't as substantial as I might have hoped, and may not warrant the effort involved in maintaining an accurate configuration for every package. I could be open to pushback here. I still think targeting a lack of side effects is ideal, with custom elements being the main point of complication. The savings here could be more substantial in the future depending on future package code revisions.
📜 Testing Plan
rm -rf public/packs/jsNODE_ENV=production yarn build:jscat public/packs/js/platform-authenticator-available-*.digested.js | grep NotAllowedErrorecho $?