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

Reduce bundle size fortawesome #1289

Merged
merged 8 commits into from
Sep 26, 2024

Conversation

domschab23
Copy link
Contributor

@domschab23 domschab23 commented Sep 24, 2024

I looked at our bundle sizes and I've noticed that our fortawesome is occupying 2 top spots for dependencies

From prod

Screenshot 2024-09-24 at 4 55 07 PM

We're already guarding against it the rs repo, but looks like we never did that for DS

Locally before changes
Screenshot 2024-09-24 at 4 55 52 PM

Locally after changes
Screenshot 2024-09-24 at 4 57 28 PM

Shedding off about 5.4 MB

Copy link

coderabbitai bot commented Sep 24, 2024

Important

Review skipped

Auto reviews are limited to specific labels.

🏷️ Labels to auto review (1)
  • robo-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@domschab23 domschab23 changed the title Reduce bundle size fontawesome Reduce bundle size fortawesome Sep 24, 2024
@@ -1,6 +1,6 @@
import React from 'react';

import { faCreditCard } from '@fortawesome/pro-regular-svg-icons';
import { faCreditCard } from '../font_awesome/regular';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using relative imports; Locally I kept getting

[webpack-cli] ModuleNotFoundError: Module not found: Error: Can't resolve 'src/font_awesome/solid' in '/Users/domschab/Work/rails-server/node_modules/@user-interviews/ui-design-system/lib/Accordion'

When building rs shakapacker

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can look at absolute imports without reexporting this as a nice to have in the follow up

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we can address the best course of actions for relative imports in a follow up PR; I was reading up about it and this may be tricky to get done. Adding alias in RS shakapacker config doesn't seem like a good solution

Copy link
Collaborator

Choose a reason for hiding this comment

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

is DS wired up to respect ts files? I'm assuming it is... but if it only had TSX until now then that could be why its not resolving

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is DS wired up to respect ts files?

I think so; we've had .ts files here for a while. Based on what I was reading, it might seem like webpack/shaka might also have be aware of the correct alias configuration for bundling/resolving modules (which that seems weird to add an alias in shaka config in rs repo). So it may not be enough to specify an alias in .babelrc file in DS repo. Happy to look more into this in a separate PR

Copy link
Collaborator

Choose a reason for hiding this comment

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

is that an error with rails server, or building DS?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rails server; there's no error building DS

@@ -4,7 +4,7 @@ import {
type ColProps as ReactBootstrapColProps,
} from 'react-bootstrap';

import { useDeprecationWarning } from 'src/utils';
import { useDeprecationWarning } from '../utils';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This actually failed for me as well locally so using relative (looks like a new addition that wasn't released yet to rs repo)

[webpack-cli] ModuleNotFoundError: Module not found: Error: Can't resolve 'src/utils' in '/Users/domschab/Work/rails-server/node_modules/@user-interviews/ui-design-system/lib/Container'

When building rs shakapacker

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cc @jeffbax

(and this is example from a different PR from a couple of days ago that would make a rs build fail if we tried to merge newest DS to rs)

with relative imports to utils / icons in DS, RS build works. The problem here is defining aliases for internal of DS package that will also work with RS build. That could be looked at when we incorporate rollup as a follow on, but the PR in current states works with rs build.


/* eslint-disable no-restricted-imports */

import { library } from '@fortawesome/fontawesome-svg-core';
Copy link
Collaborator

Choose a reason for hiding this comment

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

So the old app did this originally because we were still using <i class='fa fa-fooo' /> elements and this would make things available outside of webpack context for us... I would expect tree shaking to take care of not importing things that were not used (if may be a bit slow)

Are we totally sure the prod bundle is affected here? 5.4mb is certainly massive savings so I don't want to undermine what could be a huge win... I just want to make sure the bad things we think are happening are happening.

The thing I would generally like to see if possible, is for the DS to provide FA to the rails app so they are always in sync and managed in one spot.

I don't know if this might be digging us deeper on a mechanism we don't want to rely on.... but maybe that is not the case.

I guess my other concern in the rails app was that these "libraries" didn't tend to get things removed when they were not used any longer. Which is why tree shaking is useful, it should get what people don't clean up.

Copy link
Collaborator

Choose a reason for hiding this comment

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

All that said, being deliberate also might mean a faster build for us to not depend on tree shaking. I'm mostly just saying I'm not totally familiar with what has changed for FA in the past few years.

❓ Do we know tree-shaking is in fact turned on for DS?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tree shaking is turned on for shakapacker when in production mode by default, but we're serving our DS components as a package that are transpiled with babel into commonJS and webpacker/shakapacker does not support tree shaking for commonJS modules https://webpack.js.org/guides/tree-shaking/#conclusion

Ensure no compilers transform your ES2015 module syntax into CommonJS modules (this is the default behavior of the popular Babel preset @babel/preset-env - see the documentation for more details).

It's very likely that we're having unnecessary ~5MB of fortawesome in prod based on few tests I run

Shakapcker locally with prod setting with current DS from main
Shaka with prod mode with current DS

Shakapacker locally with prod setting and DS from this branch. 4.9MB less
shaka with prod setting wihth DS local

Bundles locally with prod setting and DS from this branch

Screenshot 2024-09-24 at 9 19 11 PM

Bundles locally with prod setting with current DS from main
Screenshot 2024-09-24 at 9 28 36 PM

You can see we're importing all of @fortawesome icons and that matches with screenshot from prod I added in PR description.

Copy link
Contributor Author

@domschab23 domschab23 Sep 25, 2024

Choose a reason for hiding this comment

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

Font awesome also mentions this in their docs

https://docs.fontawesome.com/apis/javascript/tree-shaking

Known Issues with Tree-shaking
Production Mode
Webpack only performs tree-shaking when the --mode=production flag is used. This enables the UglifyJsPlugin which performs the optimization. To nerd out for a second; this plugin is actually doing “dead code elimination” which is a slower process than the canonical tree-shaking but the results are similar.
Rollup enables tree-shaking by default but it does so only with ES modules. Font Awesome provides ES modules in all of its NPM packages so you should be good-to-go.

Which seems to confirm that currently we're not getting benefits of tree shaking or 'dead code elimination' for @fortawesome and that aligns with screenshots above comparing bundle sizes

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think my question is whether we are building the DS "in production" -- if we build it in production with tree shaking, then when it gets to the rails server it would presumably already have removed the unused icons?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think my question is whether we are building the DS "in production" -- if we build it in production with tree shaking, then when it gets to the rails server it would presumably already have removed the unused icons?

It doesn't seem like that's what happens today per screenshots above (you can see icons taking much more space than needed in screenshots above).

When you transpile your code into commonJS (what we're doing today with DS components) then treeshaking for that portion of the code is not working per

webpacker/shakapacker does not support tree shaking for commonJS modules https://webpack.js.org/guides/tree-shaking/#conclusion

Ensure no compilers transform your ES2015 module syntax into CommonJS modules (this is the default behavior of the popular Babel preset @babel/preset-env - see the documentation for more details).

src/font_awesome/brands.ts Outdated Show resolved Hide resolved
@@ -1,6 +1,6 @@
import React from 'react';

import { faCreditCard } from '@fortawesome/pro-regular-svg-icons';
import { faCreditCard } from '../font_awesome/regular';
Copy link
Collaborator

Choose a reason for hiding this comment

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

is DS wired up to respect ts files? I'm assuming it is... but if it only had TSX until now then that could be why its not resolving

@domschab23 domschab23 requested a review from jeffbax September 26, 2024 13:20
Copy link
Collaborator

@jeffbax jeffbax left a comment

Choose a reason for hiding this comment

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

lets take the short term win here if its working, but lets try to check out Rollup in the DS working group as I do think ideally we don't have to do this type of thing and its hopefully a straightforward change to package.json

@domschab23 domschab23 merged commit 969de45 into main Sep 26, 2024
4 checks passed
@domschab23 domschab23 deleted the infra/reduce-bundle-size-fortawesome-imports branch September 26, 2024 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants