Skip to content

WIP: feat: Treeshake macro implementation #10740

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

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft

Conversation

ScriptedAlchemy
Copy link
Contributor

Summary

This pull request introduces several changes across multiple files to enhance functionality related to module dependency handling, export tracking, and runtime template generation. The key updates focus on supporting ConsumeShared modules, improving export handling with conditional macros, and introducing new utility functions for processing shared object literals. Additionally, there are minor formatting adjustments and code improvements for better readability.

Enhancements for ConsumeShared Modules:

  • Support for ConsumeShared Modules:

    • Added a new method get_consume_shared_key in the Module trait to retrieve the share_key for ConsumeShared modules.
    • Updated CommonJsExportRequireDependencyTemplate and CommonJsExportsDependencyTemplate to wrap exports and assignments with conditional macros based on the share_key. [1] [2] [3]
  • Handling Object Literals in ConsumeShared Exports:

    • Introduced functions extract_share_key_from_condition and process_consume_shared_object_literal to process object literals in ConsumeShared modules, ensuring all properties are wrapped with conditional macros.

Improvements to Runtime Template:

  • Export Purity Annotation:

    • Added logic to determine whether an import is pure based on its type (e.g., side-effect imports vs. named imports) and annotated pure imports with /* #__PURE__ */. [1] [2]
  • ConsumeShared Export Wrapping:

    • Enhanced ESMExportExpressionDependencyTemplate to wrap fallback exports in ConsumeShared modules with conditional macros. This includes handling connections from ConsumeShared modules. [1] [2]

Utility and Codebase Enhancements:

  • Utility Integration:
    • Added CowUtils for efficient string manipulation in init_fragment.rs.

Related links

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

ScriptedAlchemy and others added 11 commits June 18, 2025 19:33
…lugin

ConsumeShared modules now participate in export usage analysis like normal modules:
- Detect ConsumeShared module type and route to enhanced processing
- Apply same usage state logic (Used, OnlyPropertiesUsed, side-effects)
- Handle mangling and inlining constraints from dependencies
- Enable proper tree-shaking of unused ConsumeShared exports
- Maintain fallback module completeness for module federation reliability

This allows ConsumeShared proxy modules to be tree-shaken based on actual
usage while preserving fallback modules for runtime safety.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Implement copy_metadata_from_fallback() and copy_exports_from_fallback() methods
- Add CompilationFinishModules hook for proper lifecycle timing
- Enable comprehensive export analysis with prefetched export info
- Support ProvidedExports variants (ProvidedNames, ProvidedAll, Unknown)
- Copy export provision status, mangling capabilities, and nested structures
- Add proper error handling and diagnostic reporting for metadata copying failures

This ensures ConsumeShared proxy modules accurately reflect their fallback
module's export capabilities for tree-shaking analysis.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Add conditional tree-shaking comments to ESM export dependencies
- Implement ConsumeShared module detection in export specifier templates
- Use macro format: /* @common:if [condition="treeShake.{shareKey}.{export}"] */
- Add fallback module detection via incoming connection analysis
- Support both direct ConsumeShared parents and fallback modules
- Remove extra EXPORT comments from runtime modules for cleaner output

This enables build tools to conditionally include exports based on
tree-shaking analysis while maintaining module federation compatibility.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…ration

- Add ShareUsagePlugin for ConsumeShared module analysis
- Add SharedExportUsagePlugin for general export tracking
- Implement enhanced dependency analysis using get_referenced_exports()
- Add comprehensive export usage types and data structures
- Support incoming connection analysis for accurate usage detection
- Enable detailed usage reporting with JSON output capabilities

These plugins provide deep insights into module federation export usage
patterns and support advanced tree-shaking analysis workflows.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Add comprehensive basic example with module federation setup
- Include analysis scripts and debug tooling for export tracking
- Add package configuration and build scripts for testing
- Update dependency manifests and IDE configurations
- Provide JSON analysis output and extraction utilities

This testing infrastructure enables validation of the ConsumeShared
tree-shaking implementation and provides debugging capabilities.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…rt processing

- Add intelligent ConsumeShared macro detection in ESM export init fragments
- Implement object literal processing for ConsumeShared default exports
- Add share_key extraction from existing condition strings
- Support automatic @common:endif closure for ConsumeShared declarations
- Handle complex nested export structures with conditional macro wrapping

This provides sophisticated macro handling for ConsumeShared modules,
ensuring proper conditional export inclusion in generated code.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Add enhanced share usage plugin for advanced analysis workflows
- Include legacy export usage plugin version for comparison
- Add test documentation for ConsumeShared usage validation
- Provide additional debugging and development tools

These supplementary tools support research and development of the
ConsumeShared tree-shaking implementation.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Add sophisticated object literal parsing for ConsumeShared exports
- Implement unused exports detection from share-usage.json
- Enhance replacement enforcement for proper macro ordering
- Clean up import dependencies and improve source manipulation
- Support complex nested object structures in export expressions

This provides more robust handling of ConsumeShared export patterns
and ensures proper macro application in complex scenarios.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
… module federation

This commit implements two key improvements for rspack's module federation and tree-shaking:

1. **Conditional Pure Annotations**: Added logic to conditionally apply `/* #__PURE__ */` annotations to `__webpack_require__` calls based on import type:
   - Named/default imports (`import { foo } from 'bar'`) get pure annotations for better tree-shaking
   - Side-effect imports (`import './style.css'`) do NOT get pure annotations to preserve side effects
   - Module factories and fallback functions get pure annotations appropriately

2. **TreeShake Macro Annotations**: Enhanced ConsumeShared modules to wrap ALL exports with conditional compilation macros:
   - Export declarations wrapped with `/* @common:if [condition="treeShake.MODULE.EXPORT"] */ ... /* @common:endif */`
   - Default export objects wrapped with macro annotations
   - Only applies to ConsumeShared modules, regular modules remain unchanged

3. **NO_SIDE_EFFECTS Annotation**: Added `// #__NO_SIDE_EFFECTS__` annotation to the main require function definition for better bundler optimization.

The implementation correctly distinguishes between:
- ConsumeShared modules (get treeshake macros on ALL exports)
- Regular modules (no macro annotations)
- Side-effect imports vs named imports (for pure annotations)
- Entry points vs module imports (entry points preserve side effects)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link

netlify bot commented Jun 19, 2025

Deploy Preview for rspack failed. Why did it fail? →

Name Link
🔨 Latest commit e05eda9
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/68608963d7e9590008086f7e

ScriptedAlchemy and others added 6 commits June 19, 2025 13:08
- Add comprehensive error handling for string parsing operations
- Validate bounds before slicing to prevent panics
- Add detailed warning logs for malformed patterns
- Improve robustness of ConsumeShared macro processing

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link

codspeed-hq bot commented Jun 19, 2025

CodSpeed Performance Report

Merging #10740 will not alter performance

Comparing treeshake-macro (8342466) with main (ff654c6)

Summary

✅ 16 untouched benchmarks

ScriptedAlchemy and others added 11 commits June 19, 2025 15:31
- Add comprehensive documentation and type annotations
- Implement sophisticated ConsumeShared module integration
- Enhance template system with advanced code generation
- Add proper runtime requirement and init fragment management
- Improve export handling with tree-shaking macro support
- Follow rspack minimalist conventions and naming patterns
- Add development mode debug comments and error handling
- Optimize performance with better export usage analysis

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…ractices

Enhanced ESMExportImportedSpecifierDependency with comprehensive improvements:

- Advanced error diagnostics with context and recovery suggestions
- Sophisticated template generation with runtime condition support
- Performance optimizations through enhanced mode calculation
- Comprehensive export resolution with validation
- Enhanced connection state management for tree shaking
- Improved documentation following rspack conventions
- Better circular dependency and conflict detection
- Enhanced comment generation with contextual information

Applied research findings for enterprise-grade dependency handling
matching sophistication of core rspack ESM dependency patterns.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Enhanced error handling with comprehensive diagnostic integration
- Added sophisticated dependency template with advanced code generation
- Improved module graph integration with runtime condition support
- Added source map support and resource identifier generation
- Implemented proper ConsumeShared module detection with fallback support
- Enhanced export specification with terminal binding and priority
- Added comprehensive validation and fallback rendering
- Followed rspack's minimalist documentation and naming conventions
- Added performance optimizations with export usage caching
- Implemented proper side effect detection for module evaluation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Fixed the "borrow of moved value" error on line 605 where add_diagnostic
was being moved. Changed the function signatures to accept mutable
references instead of consuming the closure, allowing multiple uses
of add_diagnostic throughout the function.

Changes:
- Updated create_consume_shared_module to accept &mut impl FnMut(Diagnostic)
- Updated get_required_version to accept &mut impl FnMut(Diagnostic)
- Updated all call sites to use mutable references

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Remove unused import ExportProvided
- Add missing import IntoTWithDiagnosticArray
- Fix InternalError conversion to use Diagnostic::warn/error methods
- Remove duplicate find_fallback_module_id method definition
- Add lifetime parameters to fix lifetime issues in batch_prefetch_exports
- Fix iterator usage by collecting to Vec before checking is_empty()
- Fix export metadata type mismatch (Option<bool> vs bool)
- Fix diagnostic formatting to use render_report instead of Display
- Prefix unused variables with underscore
- Replace all RspackSeverity usages with rspack_error::Severity

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
… test suite

- Reverted esm_export_expression_dependency.rs to minimally invasive implementation
- Added ConsumeShared macro comment generation for tree-shaking
- Created comprehensive Node.js test runner with proper setup/teardown
- Cleaned up example directory by removing unused analysis scripts
- Added validation for macro comments and share-usage.json output
- Fixed dependency versions to use workspace references

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…ebase

- Updated esm_export_expression_dependency.rs with minimal ConsumeShared macro support
- Reverted common_js_exports_dependency.rs to minimal implementation from main branch
- Added ConsumeShared macro generation for CommonJS export dependencies
- Enhanced esm_export_imported_specifier_dependency.rs with ConsumeShared support
- Removed legacy export usage analysis plugins and types
- Cleaned up module federation plugin exports and dependencies
- Updated share usage plugin with streamlined implementation
- Fixed consume_shared_module.rs to support macro key generation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…ation

- Removed excessive comments and helper methods from CommonJS and ESM dependency files
- Simplified ConsumeShared macro logic with cleaner helper functions
- Enhanced test suite to validate 15 macro comments across shared modules
- Improved code organization and maintainability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
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.

1 participant