-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(mobile): add more context to each metro customization option
- Loading branch information
Showing
1 changed file
with
36 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,44 @@ | ||
// Learn more https://docs.expo.dev/guides/monorepos | ||
// Learn more https://docs.expo.dev/guides/customizing-metro/ | ||
const { getDefaultConfig } = require('expo/metro-config'); | ||
const { FileStore } = require('metro-cache'); | ||
const path = require('path'); | ||
|
||
const projectRoot = __dirname; | ||
const workspaceRoot = path.resolve(projectRoot, '../..'); | ||
module.exports = withTurborepoManagedCache(withMonorepoPaths(getDefaultConfig(__dirname))); | ||
|
||
const config = getDefaultConfig(projectRoot); | ||
/** | ||
* Add the monorepo paths to the Metro config. | ||
* This allows Metro to resolve modules from the monorepo. | ||
* | ||
* @see https://docs.expo.dev/guides/monorepos/#modify-the-metro-config | ||
* @param {import('expo/metro-config').MetroConfig} config | ||
* @returns {import('expo/metro-config').MetroConfig} | ||
*/ | ||
function withMonorepoPaths(config) { | ||
const projectRoot = __dirname; | ||
const workspaceRoot = path.resolve(projectRoot, '../..'); | ||
|
||
// #1 - Watch all files in the monorepo | ||
config.watchFolders = [workspaceRoot]; | ||
// #2 - Try resolving with project modules first, then workspace modules | ||
config.resolver.nodeModulesPaths = [ | ||
path.resolve(projectRoot, 'node_modules'), | ||
path.resolve(workspaceRoot, 'node_modules'), | ||
]; | ||
// #1 - Watch all files in the monorepo | ||
config.watchFolders = [workspaceRoot]; | ||
|
||
// Use turborepo to restore the cache when possible | ||
config.cacheStores = [ | ||
new FileStore({ root: path.join(projectRoot, 'node_modules', '.cache', 'metro') }), | ||
]; | ||
// #2 - Resolve modules within the project's `node_modules` first, then all monorepo modules | ||
config.resolver.nodeModulesPaths = [ | ||
path.resolve(projectRoot, 'node_modules'), | ||
path.resolve(workspaceRoot, 'node_modules'), | ||
]; | ||
|
||
module.exports = config; | ||
return config; | ||
} | ||
|
||
/** | ||
* Move the Metro cache to the `node_modules/.cache/metro` folder. | ||
* This repository configured Turborepo to use this cache location as well. | ||
* If you have any environment variables, you can configure Turborepo to invalidate it when needed. | ||
* | ||
* @see https://turbo.build/repo/docs/reference/configuration#env | ||
* @param {import('expo/metro-config').MetroConfig} config | ||
* @returns {import('expo/metro-config').MetroConfig} | ||
*/ | ||
function withTurborepoManagedCache(config) { | ||
config.cacheStores = [new FileStore({ root: path.join(__dirname, 'node_modules/.cache/metro') })]; | ||
return config; | ||
} |