-
Notifications
You must be signed in to change notification settings - Fork 4k
Reduce entry bundle size - Part 3 #13099
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
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
✅ PR preview is ready!
|
This stack of pull requests is managed by Graphite. Learn more about stacking. |
📉 Significant wheel size change detectedThe wheel file size has decreased by 0.27% (threshold: 0.25%)
Please verify that this change is expected. |
📉 Significant bundle size change detected
Please verify that this change is expected. |
e9a7b34 to
248e04c
Compare
f21f5cd to
dd86849
Compare
539d019 to
34b8a87
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR reduces the entry bundle size from 3.51 MB to 2.37 MB (~32% reduction) by lazy-loading heavy dependencies and migrating from lodash to the tree-shakeable lodash-es library.
Key Changes:
- Lazy-load
rehype-katex,rehype-raw, andremark-emojiin StreamlitMarkdown only when needed - Lazy-load
node-emojiin Favicon component for emoji shortcode conversion - Migrate all lodash imports to lodash-es for better tree-shaking
Reviewed changes
Copilot reviewed 44 out of 45 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| frontend/yarn.lock | Update dependencies: lodash → lodash-es and corresponding type definitions |
| frontend/lib/package.json | Add lodash-es dependency, update types, add sideEffects: false for tree-shaking |
| frontend/connection/package.json | Migrate to lodash-es |
| frontend/app/package.json | Migrate to lodash-es |
| frontend/app/vite.config.ts | Add alias to redirect lodash imports to lodash-es |
| frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx | Implement lazy loading for rehype-katex, rehype-raw, and remark-emoji with detection heuristics |
| frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.test.tsx | Update tests to handle async plugin loading |
| frontend/lib/src/components/elements/Favicon/Favicon.tsx | Refactor to lazy-load node-emoji only for shortcode conversion |
| frontend/lib/src/components/elements/Favicon/Favicon.test.tsx | Update tests to handle async favicon setting |
| frontend/app/src/util/AppNavigation.ts | Add hasSetDefaultFavicon flag and make page config flags sticky |
| frontend/app/src/App.tsx | Ensure default favicon is set only once per page load; replace lodash without with native filter |
| frontend/lib/src/**/*.ts(x) | Migrate lodash imports from default imports to named imports from lodash-es |
| frontend/app/src/**/*.ts(x) | Migrate lodash imports to lodash-es |
| frontend/connection/src/**/*.ts(x) | Migrate lodash imports to lodash-es |
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx
Show resolved
Hide resolved
frontend/lib/src/components/shared/StreamlitMarkdown/StreamlitMarkdown.tsx
Outdated
Show resolved
Hide resolved
34b8a87 to
e5d6194
Compare
dd86849 to
a1e1e47
Compare
e5d6194 to
4de93bd
Compare
a1e1e47 to
2623e74
Compare
4de93bd to
e11bf89
Compare
2623e74 to
2a6fd5b
Compare
e11bf89 to
a6e325e
Compare
📉 Frontend coverage change detectedThe frontend unit test (vitest) coverage has decreased by 0.0200%
✅ Coverage change is within normal range. |
| } | ||
| }, | ||
| "sideEffects": [ | ||
| "./src/index.ts" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: whats adding for having this here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We previously had the below, but with the bokeh change we effectively changed our sideEffects specification to true, making for inefficient tree shaking (bundlers now assume they cannot remove any imported module in the package).
"sideEffects": [
"**/vendor/bokeh/**"
],In particular, this was causing the inclusion of @testing-library in the entry bundle.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, that explains why the the entry size went up after the bokeh removal. Good to know!
| this.onPageIconChange = onPageIconChange | ||
| this.isPageIconSet = false | ||
| this.isPageTitleSet = false | ||
| this.hasSetDefaultFavicon = false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: I think it might be good to add some unit tests for the new logic around the hasSetDefaultFavicon flag
| function containsEmojiShortcodes(source: string): boolean { | ||
| return /:(?!material\/|streamlit:)\w[\w_-]*:/.test(source) | ||
| } | ||
|
|
||
| /** | ||
| * Detects if the markdown source contains math syntax that requires KaTeX. | ||
| * Checks for inline math ($...$) and display math ($$...$$) patterns. | ||
| * | ||
| * @param source - The markdown source string to check | ||
| * @returns true if math syntax is detected, false otherwise | ||
| */ | ||
| function containsMathSyntax(source: string): boolean { | ||
| // Detect inline math: $...$ or display math: $$...$$ | ||
| return /\$\$[\s\S]+?\$\$|\$[^$\n]+?\$/.test(source) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: I think it would be good to add some parameterized unit tests for these contains checks. Not 100% sure, but I believe the containsMathSyntax can be improved by not allowing white spaces after the first $ and before the last $. Otherwise, it might also match something like: the prices is between $5 and $10 as a latex notation.
| * @returns true if emoji shortcodes are detected, false otherwise | ||
| */ | ||
| function containsEmojiShortcodes(source: string): boolean { | ||
| return /:(?!material\/|streamlit:)\w[\w_-]*:/.test(source) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there might be some emoji shortcodes that might not be covered: e.g. :+1:... maybe a few other cases. You can find the full list of supported shortcodes here: https://streamlit-emoji-shortcodes-streamlit-app-gwckff.streamlit.app/
a6e325e to
9cda13b
Compare
lukasmasuch
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍

Describe your changes
Attempt to improve initial load time by reducing dependencies included in entry bundle.
Dependencies removed from entry:
rehype-katex&rehype-raw&parse5rehype-katexinStreamlitMarkdownwhen markdown contains math syntaxrehype-rawinStreamlitMarkdownwhenallowHTMLis true@sindresorhus:node-emojidepends on@sindresorhus- refactorFavicon&StreamlitMarkdownto lazy loadremark-emojifor shortcodes@testing-library@streamlit/libso improve tree-shakinglodashlodash-esBundle Analysis:
Before (left): 3.51 MB & After (right): 2.38 MB
