feat: Add pnpm monorepo support for Metro bundler #160
+35
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for pnpm workspace monorepos in Granite's Metro bundler configuration, resolving critical issues with module resolution and bundling in pnpm-based projects.
Problem Statement
When using Granite in a pnpm workspace monorepo with multiple apps using different React Native versions, the following issues occur:
.pnpmvirtual directory due to symlink handlingExample Error Scenario
Monorepo Structure:
Errors encountered:
Changes Made
1. Enhanced Module Resolver (
enhancedResolver.ts)Added
aliasconfiguration to force local React/React Native versions:Why this works:
reactandreact-nativefrom each app's localnode_modulesmodules: ['node_modules', ...]2. Dependency Graph SHA-1 Fallback (
DependencyGraph.js)Added 2-tier fallback logic for SHA-1 computation:
Why this works:
3. Metro Configuration Optimization (
getMetroConfig.ts)Changes:
disableHierarchicalLookup: trueas defaultwatchFoldersWhy this works:
Testing
Environment:
Test Results:
Patch File for Users
For users who want to apply this fix immediately without waiting for a release, here's a pnpm patch:
1. Create patch:
2. Apply changes from this PR to the temporary directory
3. Commit patch:
4. The patch will be saved to
patches/@[email protected]and automatically added topackage.json:{ "pnpm": { "patchedDependencies": { "@granite-js/[email protected]": "patches/@[email protected]" } } }Breaking Changes
None. These changes are backward compatible:
disableHierarchicalLookupcan still be overridden in configBenefits
Related Issues
This PR addresses the fundamental incompatibility between Granite's Metro bundler and pnpm workspace monorepos, particularly when multiple React Native versions coexist.
Tested with: