Skip to content

Improve bundle tracing and tree-shaking for Shape defaults#7348

Merged
PavelVanecek merged 2 commits into
mainfrom
trace-bundle-fix
May 21, 2026
Merged

Improve bundle tracing and tree-shaking for Shape defaults#7348
PavelVanecek merged 2 commits into
mainfrom
trace-bundle-fix

Conversation

@PavelVanecek

@PavelVanecek PavelVanecek commented May 20, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • run the bundle tracing script through native Node instead of tsx and keep the trace-bundle tests aligned with that flow
  • remove the shared shapeType selector from Shape so each chart component provides its own default renderer
  • update tree-shaking expectations for the affected chart bundles and assert that Area no longer reaches Rectangle through ActiveShapeUtils

Summary by CodeRabbit

  • Chores

    • Modernized build scripts for improved Node.js ESM compatibility.
    • Enhanced bundle tracing performance with optimized pathfinding algorithm to handle multiple source and target modules efficiently.
  • Tests

    • Added comprehensive test coverage for bundle tracing helper functions.

Review Change Stack

…path between the "from" and "to" components rather than "index.js" and "to"
@coderabbitai

coderabbitai Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

This PR migrates two build scripts to ESM with a consistent execution guard pattern, refactors the trace-bundle utility to export reusable pathfinding functions, improves its BFS algorithm with pruning and memoization, and enables multi-source/target bundle tracing with comprehensive tests.

Changes

ESM Migration & Trace-Bundle Refactor

Layer / File(s) Summary
ESM path resolution and CLI execution guard
package.json, scripts/trace-bundle.ts, scripts/treeshaking.ts
Both scripts migrate to fileURLToPath(import.meta.url) for ESM-compatible path resolution. An isExecutedAsScript() helper replaces the CommonJS require.main === module pattern to detect direct execution. Package.json script invocation switches from npx tsx to direct node command.
trace-bundle pathfinding API and BFS improvements
scripts/trace-bundle.ts
findShortestPaths is now exported. BFS pruning introduces shortestResultLength tracking to skip paths longer than the best found so far; per-node shortestPathLengthById memoization avoids redundant exploration; in-path cycle avoidance skips nodes already in the current search path.
trace-bundle multi-source/target matching and reporting
scripts/trace-bundle.ts, scripts/treeshaking.test.ts
main() refactored to find all matching sourceNodes and targetNodes via findMatchingModules instead of using a single entry point. Output loops across each source/target pair and calls findShortestPaths(source.id, target.id, ...) for each. Tests verify exact-match preference in findMatchingModules and shortest-path-only behavior in findShortestPaths using both synthetic and real module graphs.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • ckifer
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description provides a clear summary of changes but lacks required template sections like motivation, testing details, and type classifications. Add missing sections from the template: Related Issue, Motivation and Context, How Has This Been Tested, and Types of changes checklist.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title directly relates to the main changes: improving bundle tracing (moving from tsx to native Node) and tree-shaking for Shape defaults (decoupling shared selector).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch trace-bundle-fix

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented May 20, 2026

Copy link
Copy Markdown

Bundle Report

Bundle size has no change ✅

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
scripts/trace-bundle.ts (1)

29-32: 💤 Low value

Consider extracting isExecutedAsScript() to a shared module.

This helper is duplicated verbatim in treeshaking.ts. For two script files this is acceptable, but if more scripts adopt this pattern, consider exporting it from a shared utility.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@scripts/trace-bundle.ts` around lines 29 - 32, The helper function
isExecutedAsScript is duplicated across modules; extract it into a shared
utility module (e.g., utils or script-utils) and export it so both trace-bundle
and the treeshaking module can import it. Update the current isExecutedAsScript
definition in trace-bundle (and remove the duplicate in the other module) to
import the shared function instead, ensuring the implementation and behavior
remain identical (uses process.argv[1] and path.resolve). Add a brief unit test
or usage example in the shared module to validate correct behavior when executed
as a script.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@scripts/trace-bundle.ts`:
- Around line 29-32: The helper function isExecutedAsScript is duplicated across
modules; extract it into a shared utility module (e.g., utils or script-utils)
and export it so both trace-bundle and the treeshaking module can import it.
Update the current isExecutedAsScript definition in trace-bundle (and remove the
duplicate in the other module) to import the shared function instead, ensuring
the implementation and behavior remain identical (uses process.argv[1] and
path.resolve). Add a brief unit test or usage example in the shared module to
validate correct behavior when executed as a script.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d39e2303-3205-4c7a-8c77-7cb4ea4e1e2a

📥 Commits

Reviewing files that changed from the base of the PR and between 6c7e9bf and aeb2d8e.

📒 Files selected for processing (4)
  • package.json
  • scripts/trace-bundle.ts
  • scripts/treeshaking.test.ts
  • scripts/treeshaking.ts

@chromatic-com

chromatic-com Bot commented May 20, 2026

Copy link
Copy Markdown

Tip

All tests passed and all changes approved!

🟢 UI Tests: 193 tests unchanged
🟢 UI Review: 193 stories published -- no changes
Storybook icon Storybook Publish: 193 stories published

@codecov

codecov Bot commented May 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.03%. Comparing base (6c7e9bf) to head (b26c27a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7348   +/-   ##
=======================================
  Coverage   89.03%   89.03%           
=======================================
  Files         548      548           
  Lines       40522    40522           
  Branches     5570     5570           
=======================================
  Hits        36079    36079           
  Misses       4434     4434           
  Partials        9        9           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

Copy link
Copy Markdown
Contributor

Staging Deployment Details

These deployments will remain available for 30 days.

To update snapshots: Comment /update-snapshots on this PR to automatically update the baseline screenshots.

1 similar comment
@github-actions

Copy link
Copy Markdown
Contributor

Staging Deployment Details

These deployments will remain available for 30 days.

To update snapshots: Comment /update-snapshots on this PR to automatically update the baseline screenshots.

@PavelVanecek PavelVanecek changed the title Update trace-bundle.ts to mjs instead of tsx, and allow it to return path between the "from" and "to" components rather than "index.js" and "to" Improve bundle tracing and tree-shaking for Shape defaults May 20, 2026
@PavelVanecek

Copy link
Copy Markdown
Collaborator Author

My AI pushed too many things in this branch, I will fix

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@scripts/treeshaking.test.ts`:
- Around line 183-194: The test at scripts/treeshaking.test.ts assumes a
deterministic order from findShortestPaths which makes it flaky; update the
assertion to be order-insensitive by normalizing both actual and expected
results before comparing: for each path array returned by
findShortestPaths(area[0].id, dot[0].id, nodeById, 8) sort the file-string
entries (or join them) and then compare the multiset of paths (sort the list of
normalized paths or compare via Sets) against the similarly-normalized expected
array so the test passes regardless of traversal order. Ensure you reference
findShortestPaths and nodeById when locating the test to change.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 697bd6cb-1994-48e1-bd23-30ca6281aa0d

📥 Commits

Reviewing files that changed from the base of the PR and between c65fbee and b26c27a.

📒 Files selected for processing (1)
  • scripts/treeshaking.test.ts

Comment on lines +183 to +194
expect(findShortestPaths(area[0].id, dot[0].id, nodeById, 8)).toEqual([
[
path.resolve(__dirname, '..', 'src', 'cartesian', 'Area.tsx'),
path.resolve(__dirname, '..', 'src', 'component', 'Dots.tsx'),
path.resolve(__dirname, '..', 'src', 'shape', 'Dot.tsx'),
],
[
path.resolve(__dirname, '..', 'src', 'cartesian', 'Area.tsx'),
path.resolve(__dirname, '..', 'src', 'component', 'ActivePoints.tsx'),
path.resolve(__dirname, '..', 'src', 'shape', 'Dot.tsx'),
],
]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Make shortest-path assertion order-insensitive to avoid flaky tests.

On Line 183, the test assumes deterministic ordering of returned paths. If graph traversal order changes, this can fail despite correct behavior.

Suggested change
-    expect(findShortestPaths(area[0].id, dot[0].id, nodeById, 8)).toEqual([
+    const shortestPaths = findShortestPaths(area[0].id, dot[0].id, nodeById, 8);
+    expect(shortestPaths).toHaveLength(2);
+    expect(shortestPaths).toEqual(expect.arrayContaining([
       [
         path.resolve(__dirname, '..', 'src', 'cartesian', 'Area.tsx'),
         path.resolve(__dirname, '..', 'src', 'component', 'Dots.tsx'),
         path.resolve(__dirname, '..', 'src', 'shape', 'Dot.tsx'),
       ],
       [
         path.resolve(__dirname, '..', 'src', 'cartesian', 'Area.tsx'),
         path.resolve(__dirname, '..', 'src', 'component', 'ActivePoints.tsx'),
         path.resolve(__dirname, '..', 'src', 'shape', 'Dot.tsx'),
       ],
-    ]);
+    ]));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
expect(findShortestPaths(area[0].id, dot[0].id, nodeById, 8)).toEqual([
[
path.resolve(__dirname, '..', 'src', 'cartesian', 'Area.tsx'),
path.resolve(__dirname, '..', 'src', 'component', 'Dots.tsx'),
path.resolve(__dirname, '..', 'src', 'shape', 'Dot.tsx'),
],
[
path.resolve(__dirname, '..', 'src', 'cartesian', 'Area.tsx'),
path.resolve(__dirname, '..', 'src', 'component', 'ActivePoints.tsx'),
path.resolve(__dirname, '..', 'src', 'shape', 'Dot.tsx'),
],
]);
const shortestPaths = findShortestPaths(area[0].id, dot[0].id, nodeById, 8);
expect(shortestPaths).toHaveLength(2);
expect(shortestPaths).toEqual(expect.arrayContaining([
[
path.resolve(__dirname, '..', 'src', 'cartesian', 'Area.tsx'),
path.resolve(__dirname, '..', 'src', 'component', 'Dots.tsx'),
path.resolve(__dirname, '..', 'src', 'shape', 'Dot.tsx'),
],
[
path.resolve(__dirname, '..', 'src', 'cartesian', 'Area.tsx'),
path.resolve(__dirname, '..', 'src', 'component', 'ActivePoints.tsx'),
path.resolve(__dirname, '..', 'src', 'shape', 'Dot.tsx'),
],
]));
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@scripts/treeshaking.test.ts` around lines 183 - 194, The test at
scripts/treeshaking.test.ts assumes a deterministic order from findShortestPaths
which makes it flaky; update the assertion to be order-insensitive by
normalizing both actual and expected results before comparing: for each path
array returned by findShortestPaths(area[0].id, dot[0].id, nodeById, 8) sort the
file-string entries (or join them) and then compare the multiset of paths (sort
the list of normalized paths or compare via Sets) against the
similarly-normalized expected array so the test passes regardless of traversal
order. Ensure you reference findShortestPaths and nodeById when locating the
test to change.

@github-actions

Copy link
Copy Markdown
Contributor

Staging Deployment Details

These deployments will remain available for 30 days.

To update snapshots: Comment /update-snapshots on this PR to automatically update the baseline screenshots.

@PavelVanecek
PavelVanecek merged commit 403b826 into main May 21, 2026
107 of 108 checks passed
@PavelVanecek
PavelVanecek deleted the trace-bundle-fix branch May 21, 2026 02:36
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.

2 participants