Skip to content

feat(js): add tree-shaking-based dependency narrowing to the JS project graph#35798

Open
cw-alexcroteau wants to merge 12 commits into
nrwl:masterfrom
cw-alexcroteau:feat/skip-source-analysis-metadata
Open

feat(js): add tree-shaking-based dependency narrowing to the JS project graph#35798
cw-alexcroteau wants to merge 12 commits into
nrwl:masterfrom
cw-alexcroteau:feat/skip-source-analysis-metadata

Conversation

@cw-alexcroteau

@cw-alexcroteau cw-alexcroteau commented May 25, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds conservative tree-shaking-based dependency narrowing to the JS project graph.

It covers two related capabilities:

  1. Static-graph narrowing: tree-shake projects out of the JS project graph when symbol-level analysis can prove the imported runtime symbols from a side-effect-free target are unused.
  2. Affected narrowing: reduce the scope of affected-projects when a change only modifies some exported symbols and downstream projects do not import those symbols.

The affected-project narrowing is the higher-value part of the feature, because it directly improves affected precision for real code changes.

Current Behavior

  1. When a project depends on another project via JS imports (i.e. an application importing a single symbol from a library), this creates a transitive relationship that can cascade into a lot of nested affected projects.
  2. When affecting a project by changing some symbols in JS code, the all of the library's dependencies are considered affected even for projects that do not use the modified symbols.

Expected Behavior

  1. Projects dependency graph is narrowed down based on imported symbols, especially for transient dependencies. If a project does not import symbols from other projects, it is excluded from the graph edges computation.
  2. When affecting a project by changing some symbols in JS code, the modified library's dependencies should be considered as affected only if they use one or more of the modified symbols.

What This PR Does

In practice, that means:

  • removing edges only when symbol-level analysis can prove the imported runtime symbols are unused
  • keeping edges for side-effect imports, dynamic imports, namespace imports, re-exports, and other cases that cannot be narrowed safely
  • applying the same conservative reasoning during affected calculation when only some exports in a file are modified

Public Reproduction

Related Context

Related Issue(s)

Fixes #35797

@netlify

netlify Bot commented May 25, 2026

Copy link
Copy Markdown

👷 Deploy request for nx-dev pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 8c15c27

@netlify

netlify Bot commented May 25, 2026

Copy link
Copy Markdown

👷 Deploy request for nx-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 8c15c27

@cw-alexcroteau cw-alexcroteau marked this pull request as ready for review May 25, 2026 22:17
@cw-alexcroteau cw-alexcroteau requested a review from a team as a code owner May 25, 2026 22:17
@cw-alexcroteau cw-alexcroteau requested a review from AgentEnder May 25, 2026 22:17
@cw-alexcroteau cw-alexcroteau changed the title feat(js): add tree-shaken dependency narrowing to the JS project graph feat(js): add tree-shaking-based dependency narrowing to the JS project graph May 25, 2026
@cw-alexcroteau

cw-alexcroteau commented May 25, 2026

Copy link
Copy Markdown
Contributor Author

@nrwl/nx-pipelines-reviewers I believe this is a sufficiently complex feature addition that warrants its own minor release for testing purposes, per the PR template. Please let me know if you believe the same.

Memoize imported names per source and target during affected narrowing, and precompute changed-file sets per touched project to avoid repeated work.
extract shared helper flows in import and export analysis.
add a dedicated guide and link the new @nx/js config from reference and affected docs.
keep the nx.json reference focused on config shape and link dependency narrowing details to the dedicated guide.
alias the narrowing pipeline to Nx's canonical raw dependency type so createDependencies no longer needs a local cast.
cover the case where @nx/js dependency narrowing excludes consumers whose imports are not used in emitted code while keeping consumers that still use the import.
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.

feat(js): add tree-shaking-based dependency narrowing to the JS project graph

1 participant