Skip to content

fix: enhance tree shaking behavior of LLS : W-19346529#618

Merged
madhur310 merged 4 commits intomainfrom
madhur/tree-shaking
Sep 19, 2025
Merged

fix: enhance tree shaking behavior of LLS : W-19346529#618
madhur310 merged 4 commits intomainfrom
madhur/tree-shaking

Conversation

@madhur310
Copy link
Contributor

@madhur310 madhur310 commented Sep 11, 2025

What does this PR do?

This PR refactors the code to allow better tree shaking:

  • update import/export so it allows better tree shaking behavior commonJS i.e. no namespaces, direct imports and adding paths to package json so imports can be more direct without import the whole common for example.
  • updates OOP to more functional. I eliminated all the classes that seemed unnecessary in favor of types/interfaces with methods.
  • Asked cursor to generate some metrics for tree shaking, it came back with a couple of scripts (see npm run analyze:all and couple scripts in PR). I kept this scripts for reference, but can remove them if folks think not needed.

Seeing some good potential improvements (per generated scripts):

🚀 LSP Package Tree Shaking Analysis

==================================================
📦 Analyzing LSP Package Sizes

🔍 packages/lightning-lsp-common/lib:
base-context.js: 19.77 KB
utils.js: 10.36 KB
index.js: 7.28 KB
shared.js: 5.41 KB
namespace-utils.js: 3.24 KB
indexer/tagInfo.js: 3.21 KB
logger.js: 1.14 KB
indexer/attributeInfo.js: 0.89 KB
decorators/index.js: 0.31 KB
📊 Total: 51.62 KB

🔍 packages/aura-language-server/lib:
tern/plugin/node.js: 189.12 KB
tern/lib/infer.js: 77.62 KB
tern/plugin/angular.js: 45.94 KB
tern/lib/tern.js: 40.54 KB
tern/lib/def.js: 25.04 KB
tern/plugin/requirejs.js: 19.14 KB
tern/plugin/doc_comment.js: 19.08 KB
tern-server/tern-aura.js: 17.33 KB
aura-utils.js: 15.44 KB
tern/plugin/modules.js: 13.62 KB
tests/aura-utils.test.js: 13.39 KB
aura-server.js: 13.05 KB
tern-server/tern-server.js: 12.72 KB
aura-indexer/indexer.js: 10.19 KB
tern/lib/condense.js: 10.14 KB
context/aura-context.js: 7.12 KB
tern/plugin/commonjs.js: 6.55 KB
tern-server/tests/string-util.test.js: 6.35 KB
aura-indexer/tests/indexer.spec.js: 5.42 KB
tern/plugin/es_modules.js: 4.51 KB
tests/package-dependencies.test.js: 3.76 KB
tern-server/string-util.js: 3.34 KB
util/component-util.js: 3.30 KB
tern/lib/comment.js: 2.82 KB
tern/plugin/webpack.js: 2.80 KB
tern/plugin/node_resolve.js: 2.68 KB
tern/plugin/complete_strings.js: 2.39 KB
tern-server/tests/ternCompletion.spec.js: 1.94 KB
markup/auraTags.js: 1.54 KB
tern/lib/signal.js: 1.54 KB
decorators/index.js: 1.02 KB
indexer/index.js: 1.02 KB
tern-server/tern-indexer.js: 0.96 KB
index.js: 0.83 KB
server.js: 0.37 KB
decorators/sharedTypes.js: 0.32 KB
indexer/sharedTypes.js: 0.32 KB
constants.js: 0.18 KB
📊 Total: 583.42 KB

🔍 packages/lwc-language-server/lib:
tests/lwc-server.test.js: 28.53 KB
lwc-server.js: 19.12 KB
javascript/type-mapping.js: 16.82 KB
tests/component-indexer.test.js: 11.99 KB
component-indexer.js: 10.18 KB
javascript/tests/compiler.test.js: 10.00 KB
tag.js: 9.67 KB
tests/typing-indexer.test.js: 8.90 KB
typing-indexer.js: 8.84 KB
context/lwc-context.js: 8.33 KB
javascript/compiler.js: 8.00 KB
tests/tag.test.js: 7.08 KB
tests/lwc-context.test.js: 5.10 KB
tests/typing.test.js: 4.82 KB
tests/lwc-data-provider.test.js: 3.84 KB
tests/package-dependencies.test.js: 3.76 KB
typing.js: 3.67 KB
lwc-data-provider.js: 3.45 KB
javascript/tests/type-mapping.test.js: 3.36 KB
template/linter.js: 3.31 KB
tests/test-utils.js: 2.81 KB
base-indexer.js: 2.09 KB
template/tests/linter.test.js: 1.40 KB
decorators/index.js: 1.03 KB
aura-data-provider.js: 1.00 KB
constants.js: 0.48 KB
server.js: 0.36 KB
decorators/lwc-decorators.js: 0.32 KB
📊 Total: 188.27 KB

🔍 Analyzing Export Structure

📤 Total exports in lightning-lsp-common: 87

🎯 Available subpath exports:
.
./utils
./base-context
./decorators
./logger
./namespace-utils
./indexer/attributeInfo
./indexer/tagInfo

🎯 Tree Shaking Simulation

📋 VS Code Extension (LWC only):
Only imports LWC context and base utilities
Imports: @salesforce/lwc-language-server/context, @salesforce/lightning-lsp-common/base-context, @salesforce/lightning-lsp-common/utils
🎯 Estimated bundle size reduction: 78%

📋 Custom Build Tool (Indexers only):
Only imports indexer functionality
Imports: @salesforce/lwc-language-server/component-indexer, @salesforce/aura-language-server/indexer, @salesforce/lightning-lsp-common/utils
🎯 Estimated bundle size reduction: 96%

📋 Template Linter (Template only):
Only imports template linting functionality
Imports: @salesforce/lwc-language-server/template, @salesforce/lightning-lsp-common/decorators
🎯 Estimated bundle size reduction: 63%

💡 Recommendations:

  1. Use subpath imports for external consumers
  2. Use namespace imports for internal development
  3. Measure actual bundle sizes in your applications
  4. Consider lazy loading for large modules

analyze:real-world
node real-world-analysis.js

🌍 Real-World Tree Shaking Analysis

============================================================

📋 VS Code Extension (LWC Support)
A VS Code extension that only needs LWC language support
📦 Old bundle size: 233.4 KB
📦 New bundle size: 46.0 KB
🎯 Size reduction: 80.3% (187.4 KB saved)
📥 Old imports: @salesforce/lwc-language-server, @salesforce/lightning-lsp-common
📥 New imports: @salesforce/lwc-language-server/context, @salesforce/lwc-language-server/server, @salesforce/lightning-lsp-common/base-context, @salesforce/lightning-lsp-common/utils

📋 Custom Build Tool (Component Indexing)
A build tool that only needs component indexing functionality
📦 Old bundle size: 814.4 KB
📦 New bundle size: 48.4 KB
🎯 Size reduction: 94.1% (766.0 KB saved)
📥 Old imports: @salesforce/lwc-language-server, @salesforce/aura-language-server, @salesforce/lightning-lsp-common
📥 New imports: @salesforce/lwc-language-server/component-indexer, @salesforce/aura-language-server/indexer, @salesforce/lightning-lsp-common/utils

📋 Template Linter (HTML/JSX Support)
A linter that only needs template and decorator functionality
📦 Old bundle size: 233.4 KB
📦 New bundle size: 38.3 KB
🎯 Size reduction: 83.6% (195.1 KB saved)
📥 Old imports: @salesforce/lwc-language-server, @salesforce/lightning-lsp-common
📥 New imports: @salesforce/lwc-language-server/template, @salesforce/lwc-language-server/decorators, @salesforce/lightning-lsp-common/decorators

📋 JavaScript Compiler (JS Processing)
A tool that only processes JavaScript files
📦 Old bundle size: 233.4 KB
📦 New bundle size: 19.3 KB
🎯 Size reduction: 91.7% (214.1 KB saved)
📥 Old imports: @salesforce/lwc-language-server, @salesforce/lightning-lsp-common
📥 New imports: @salesforce/lwc-language-server/javascript, @salesforce/lightning-lsp-common/decorators

🔍 Module Breakdown Analysis

============================================================

📦 lightning-lsp-common (51.44 KB total):
base-context.js: 19.77 KB (38.4%) - Workspace management
utils.js: 10.36 KB (20.1%) - Utility functions
index.js: 6.96 KB (13.5%) - Main exports
shared.js: 5.36 KB (10.4%) - Shared constants
namespace-utils.js: 3.24 KB (6.3%) - Namespace resolution
indexer/tagInfo.js: 3.24 KB (6.3%) - Tag information
logger.js: 1.14 KB (2.2%) - Logging utilities
indexer/attributeInfo.js: 1.06 KB (2.1%) - Attribute information
decorators/index.js: 0.31 KB (0.6%) - Decorator types

📦 lwc-language-server (181.95 KB total):
lwc-server.js: 19.01 KB (10.4%) - Main LWC server
javascript/type-mapping.js: 16.83 KB (9.2%) - Type mapping
component-indexer.js: 9.86 KB (5.4%) - Component indexing
context/lwc-context.js: 8.33 KB (4.6%) - LWC workspace context
javascript/compiler.js: 7.87 KB (4.3%) - JavaScript compilation
tag.js: 7.36 KB (4.0%) - Tag handling
typing-indexer.js: 5.46 KB (3.0%) - TypeScript indexing
template/linter.js: 3.31 KB (1.8%) - Template linting
decorators/index.js: 1.03 KB (0.6%) - LWC decorators

⚡ Performance Impact Analysis

============================================================
📋 VS Code Extension Startup:
Bundle size: 233.39 KB → 57.47 KB (75.4% reduction)
Performance: 75% faster startup time

📋 Build Tool Execution:
Bundle size: 814.39 KB → 30.65 KB (96.2% reduction)
Performance: 96% faster execution

📋 Template Linter:
Bundle size: 233.39 KB → 4.65 KB (98.0% reduction)
Performance: 98% faster linting

💡 Key Takeaways:

  1. 🎯 Subpath exports enable 60-98% bundle size reductions
  2. ⚡ Smaller bundles = faster startup and execution
  3. 🔧 Granular imports = better maintainability
  4. 📦 Tree shaking works best with ES modules
  5. 🚀 External consumers benefit most from subpath exports

What issues does this PR fix or reference?

@W-19346529@

@madhur310 madhur310 requested a review from a team as a code owner September 11, 2025 06:09
@madhur310 madhur310 changed the title Madhur/tree shaking fix: enhance tree shaking behavior of LLS : W-19346529 Sep 11, 2025
@madhur310 madhur310 removed the request for review from CristiCanizales September 11, 2025 06:11
Copy link
Contributor

@daphne-sfdc daphne-sfdc left a comment

Choose a reason for hiding this comment

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

In lightning-language-server:

  1. yarn install
  2. yarn link-lsp

In salesforcedx-vscode madhur/bump-lls-v branch:

  1. npm install
  2. npm run link-lsp
  3. npm run compile

Aura LSP

  1. Auto-completion ✅
  2. Go to Definition ✅
  3. Hover Documentation ✅

LWC LSP

  1. Auto-completion ✅
  2. Hover Documentation (html) ✅
  3. Hover Documentation (javascript) ✅
  4. Go to definition (html) ✅
  5. Go to definition (javascript) ✅

@madhur310 madhur310 merged commit 0e07149 into main Sep 19, 2025
8 checks passed
@madhur310 madhur310 deleted the madhur/tree-shaking branch September 19, 2025 17:31
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