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

fix: tree-shaking #2790

Merged
merged 9 commits into from
Apr 11, 2024
Merged

fix: tree-shaking #2790

merged 9 commits into from
Apr 11, 2024

Conversation

xDivisionByZerox
Copy link
Member

Summary

This PR addresses tree-shaking issues by optimizing the handling of locale imports and exports. The changes ensure that only the necessary locale modules are included in the final bundle, enhancing performance and reducing unnecessary file sizes.

This is a major requirement for the release of v9.0.

Details

The previous implementation was causing unnecessary modules to be included during tree-shaking due to the additional export of the implicit allLocales variable.

export * as allLocales from './locales';

This was refactored into an explicit export under ./locales/index.ts. The generate-locales script has been updated to accommodate this change.

Additionally, the package.json file has been updated to specify "sideEffects": false, which informs bundlers that all files within this package are side-effect-free and can be safely tree-shaken.

To be perfectly honest, even after investigating this issue for multiple hours, I'm still not perfectly sure WHY this exact change fixes the issue.

Please note, that only the combination of these changes will take any action. Applying one or the other change separately does not resolve the issue of everything being included in a final project bundle.

Testing

To verify these changes, follow these steps:

  1. Check out this pull request locally.
  2. Build the faker library using pnpm run build.
  3. Clone our playground repository.
  4. Follow the instructions in the README.md file of the playground repository.
  5. Override the faker dependency in the playground's package.json (replace "overrides-for-dev" with "overrides-for-dev").
  6. Build/compile the playground projects webpack-cjs and webpack-esm.
  7. Ensure that the console prints a bundle size number of around 400-480 KB when linking binaries from this pull request branch.
  8. Compare this bundle size with the approximately 2.2 MB bundle size when linking binaries from the next branch.

These steps will confirm that the tree-shaking optimizations have reduced the bundle size appropriately and that the library functions as expected with the updated locale handling.

Additional

I consider this a breaking change as of #2335 (comment).

Fixes #1791, #2335.

@xDivisionByZerox xDivisionByZerox added c: bug Something isn't working breaking change Cannot be merged when next version is not a major release labels Apr 7, 2024
@xDivisionByZerox xDivisionByZerox added this to the v9.0 milestone Apr 7, 2024
@xDivisionByZerox xDivisionByZerox self-assigned this Apr 7, 2024
@xDivisionByZerox xDivisionByZerox requested a review from a team as a code owner April 7, 2024 18:18
Copy link

netlify bot commented Apr 7, 2024

Deploy Preview for fakerjs ready!

Name Link
🔨 Latest commit 4af8837
🔍 Latest deploy log https://app.netlify.com/sites/fakerjs/deploys/6616be26f0c1780008a5e458
😎 Deploy Preview https://deploy-preview-2790.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@xDivisionByZerox xDivisionByZerox changed the title infra: set sideEffects to false fix: tree-shaking Apr 7, 2024
@xDivisionByZerox xDivisionByZerox requested a review from a team April 7, 2024 18:19
@ST-DDT ST-DDT linked an issue Apr 7, 2024 that may be closed by this pull request
Copy link

codecov bot commented Apr 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.96%. Comparing base (200a38e) to head (4af8837).

Additional details and impacted files
@@           Coverage Diff            @@
##             next    #2790    +/-   ##
========================================
  Coverage   99.95%   99.96%            
========================================
  Files        2971     2971            
  Lines      212553   212696   +143     
  Branches      601      948   +347     
========================================
+ Hits       212467   212611   +144     
+ Misses         86       85     -1     
Files Coverage Δ
src/index.ts 100.00% <ø> (ø)
src/locales/index.ts 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

ST-DDT
ST-DDT previously approved these changes Apr 7, 2024
Copy link
Member

@ST-DDT ST-DDT left a comment

Choose a reason for hiding this comment

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

Awesome, works as expected ❤️

grafik
grafik

@ST-DDT ST-DDT requested a review from a team April 7, 2024 18:35
@ST-DDT ST-DDT added p: 2-high Fix main branch c: infra Changes to our infrastructure or project setup labels Apr 7, 2024
Shinigami92
Shinigami92 previously approved these changes Apr 8, 2024
Copy link
Member

@Shinigami92 Shinigami92 left a comment

Choose a reason for hiding this comment

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

This is so weird ...

Theoretically you changed nothing cause export * from './locale'; in index does the same export for allLocales as before... but it just works magically 🤷

I really don't understand what's going on here

Also I really don't like this sideEffects key, that is undocumented non-standard webpack introduced, but ecosystem accepted. Why is it not e.g. that it needs to be set to sideEffects: true explicitly if you have side effects and the default should be false...

However, great work and don't take my ecosystem-rant personally ❤️

@Shinigami92
Copy link
Member

Shinigami92 commented Apr 8, 2024

This is not even a breaking change, is it? 👀

Additional

I consider this a breaking change as of #2335 (comment).

Fixes #1791, #2335.

Ah..., well,
sideEffect is now "okay-ish" as we don't have side effects anymore as historically with the unique method, but this got successfully resolved
Also the investigation is now also not a breaking change anymore ^^'

IMO we can remove the label, but you already also did not add a ! to the PR title, so I'm okay with that

@xDivisionByZerox
Copy link
Member Author

xDivisionByZerox commented Apr 8, 2024

... but you already also did not add a ! to the PR title, so I'm okay with that

Haven't done any actual OSS coding in a while + my work does not use conventional commits (so no ! for breaking changes). So I honestly forgot that this is required ^^


I'd still think we should consider this breaking. Just in case. I'll add a migration guide file.

@matthewmayer
Copy link
Contributor

While it's not breaking, it does mean you no longer need to use the workaround to import one locale right? So we could explain that in the migration guide.

@matthewmayer
Copy link
Contributor

Can we also remove the "Individual localized packages" section of the Usage docs if this is no longer needed?

@xDivisionByZerox xDivisionByZerox dismissed stale reviews from Shinigami92 and ST-DDT via b25100f April 8, 2024 20:54
@xDivisionByZerox
Copy link
Member Author

Can we also remove the "Individual localized packages" section of the Usage docs if this is no longer needed?

Wasn't sure about that, but simpl removed it now.

Please check the guide section on the docs preview. I feel like the sections "Custom locales and fallbacks" and "Available lcoales" should be switched now.

Shinigami92
Shinigami92 previously approved these changes Apr 9, 2024
@xDivisionByZerox xDivisionByZerox requested review from a team April 10, 2024 16:03
@ST-DDT ST-DDT requested review from matthewmayer, Shinigami92 and a team April 10, 2024 16:28
@ST-DDT ST-DDT merged commit fb65976 into next Apr 11, 2024
20 checks passed
@ST-DDT ST-DDT deleted the fix/tree-shaking branch April 11, 2024 06:33
@ST-DDT
Copy link
Member

ST-DDT commented Apr 11, 2024

Thanks for fixing this. ❤️

@SalahAdDin
Copy link

Testing it soon, thanks!

@Shinigami92
Copy link
Member

Testing it soon, thanks!

I cant speak for the whole team, but maybe we will do an alpha release today in next 10h 👀 🚀

@ST-DDT
Copy link
Member

ST-DDT commented Apr 11, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Cannot be merged when next version is not a major release c: bug Something isn't working c: infra Changes to our infrastructure or project setup p: 2-high Fix main branch
Projects
None yet
6 participants