Skip to content

Conversation

@mayagbarnes
Copy link
Collaborator

@mayagbarnes mayagbarnes commented Nov 24, 2025

Describe your changes

Attempt to improve initial load time by reducing dependencies included in entry bundle.

Dependencies removed from entry:

  • rehype-katex & rehype-raw & parse5

    • Lazy load rehype-katex in StreamlitMarkdown when markdown contains math syntax
    • Lazy load rehype-raw in StreamlitMarkdown when allowHTML is true
  • @sindresorhus:

    • node-emoji depends on @sindresorhus - refactor Favicon & StreamlitMarkdown to lazy load remark-emoji for shortcodes
  • @testing-library

    • We export some test utilities from @streamlit/lib so improve tree-shaking
  • lodash

    • reduce dependency size by transitioning to lodash-es

Bundle Analysis:

Before (left): 3.51 MB & After (right): 2.38 MB
Entry bundle - after part 2 Entry bundle - after part 3

@snyk-io
Copy link
Contributor

snyk-io bot commented Nov 24, 2025

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 24, 2025

✅ PR preview is ready!

Name Link
📦 Wheel file https://core-previews.s3-us-west-2.amazonaws.com/pr-13099/streamlit-1.51.0-py3-none-any.whl
📦 @streamlit/component-v2-lib Download from artifacts
🕹️ Preview app pr-13099.streamlit.app (☁️ Deploy here if not accessible)

Copy link
Collaborator Author

mayagbarnes commented Nov 24, 2025

@mayagbarnes mayagbarnes added change:refactor PR contains code refactoring without behavior change impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR labels Nov 24, 2025 — with Graphite App
@mayagbarnes mayagbarnes changed the title Remove plugins from entry Reduce entry bundle size - Part 3 Nov 24, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 24, 2025

📉 Significant wheel size change detected

The wheel file size has decreased by 0.27% (threshold: 0.25%)

  • Current PR: 8758.08 KB
  • Latest develop: 8781.64 KB

Please verify that this change is expected.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 24, 2025

📉 Significant bundle size change detected

Metric This Branch develop Change (%)
Total (gzip) 8.16 MiB 8.21 MiB -0.60%
Entry (gzip) 756.32 KiB 1.01 MiB -27.11%

Please verify that this change is expected.

📊 View detailed bundle comparison

Copy link
Contributor

Copilot AI left a 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, and remark-emoji in StreamlitMarkdown only when needed
  • Lazy-load node-emoji in 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

@mayagbarnes mayagbarnes changed the base branch from reduce-bundle-2 to graphite-base/13099 November 24, 2025 20:37
@mayagbarnes mayagbarnes changed the base branch from graphite-base/13099 to reduce-bundle-2 November 24, 2025 21:36
@mayagbarnes mayagbarnes changed the base branch from reduce-bundle-2 to graphite-base/13099 November 24, 2025 21:53
@mayagbarnes mayagbarnes changed the base branch from graphite-base/13099 to develop November 24, 2025 23:07
@github-actions
Copy link
Contributor

github-actions bot commented Nov 25, 2025

📉 Frontend coverage change detected

The frontend unit test (vitest) coverage has decreased by 0.0200%

  • Current PR: 87.5700% (51006 lines, 6335 missed)
  • Latest develop: 87.5900% (50855 lines, 6309 missed)

✅ Coverage change is within normal range.

📊 View detailed coverage comparison

}
},
"sideEffects": [
"./src/index.ts"
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator

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
Copy link
Collaborator

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

Comment on lines 103 to 117
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)
}
Copy link
Collaborator

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)
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 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/

Copy link
Collaborator

@lukasmasuch lukasmasuch left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@mayagbarnes mayagbarnes merged commit 5c45d2b into develop Nov 25, 2025
43 of 46 checks passed
@mayagbarnes mayagbarnes deleted the reduce-bundle-3 branch November 25, 2025 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

change:refactor PR contains code refactoring without behavior change impact:users PR changes affect end users security-assessment-completed Security assessment has been completed for PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants