-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reuse monorepo metro config
- Loading branch information
1 parent
9db96cf
commit 582d585
Showing
3 changed files
with
61 additions
and
90 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* eslint-env node */ | ||
const path = require("path"); | ||
const { getConfig } = require("react-native-builder-bob/metro-config"); | ||
|
||
const pkg = require("../../package.json"); | ||
const root = path.resolve(__dirname, "..", ".."); | ||
|
||
/** | ||
* @param {import('metro-config').MetroConfig} config | ||
* @param {object} options | ||
* @param {string} options.project | ||
* @returns {import('metro-config').MetroConfig} | ||
*/ | ||
function withMonorepoPaths(config, { project }) { | ||
config = getConfig(config, { | ||
root, | ||
pkg, | ||
project, | ||
}); | ||
|
||
// Watch all files in the monorepo | ||
config.watchFolders = [root]; | ||
|
||
// Set `node_modules` to resolve | ||
config.resolver.nodeModulesPaths = [ | ||
path.resolve(project, "node_modules"), | ||
path.resolve(__dirname, "node_modules"), | ||
path.resolve(root, "node_modules"), | ||
]; | ||
|
||
// Resolve only (sub)dependencies from the `nodeModulesPaths` | ||
config.resolver.disableHierarchicalLookup = true; | ||
|
||
// Use src instead of lib | ||
config.resolver.resolveRequest = (context, moduleName, platform) => { | ||
if (moduleName.startsWith(pkg.name)) { | ||
return { | ||
filePath: path.resolve(root, "src", "index.ts"), | ||
type: "sourceFile", | ||
}; | ||
} | ||
|
||
return context.resolveRequest(context, moduleName, platform); | ||
}; | ||
|
||
return getConfig(config, { | ||
root, | ||
pkg, | ||
project, | ||
}); | ||
} | ||
|
||
exports.withMonorepoPaths = withMonorepoPaths; |
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,50 +1,9 @@ | ||
/* eslint-env node */ | ||
const { | ||
withMonorepoPaths, | ||
} = require("@maplibre-react-native/examples/MetroWithMonorepoPaths"); | ||
const { getDefaultConfig } = require("expo/metro-config"); | ||
const path = require("path"); | ||
const { getConfig } = require("react-native-builder-bob/metro-config"); | ||
|
||
const pkg = require("../../package.json"); | ||
|
||
const project = __dirname; | ||
const root = path.resolve(project, "..", ".."); | ||
|
||
/** | ||
* @param config {import('expo/metro-config').MetroConfig} | ||
* @returns {import('expo/metro-config').MetroConfig} | ||
*/ | ||
function withMonorepoPaths(config) { | ||
// Watch all files in the monorepo | ||
config.watchFolders = [root]; | ||
|
||
// Set `node_modules` to resolve | ||
config.resolver.nodeModulesPaths = [ | ||
path.resolve(project, "node_modules"), | ||
path.resolve(root, "packages/examples/node_modules"), | ||
path.resolve(root, "node_modules"), | ||
]; | ||
|
||
// Resolve only (sub)dependencies from the `nodeModulesPaths` | ||
config.resolver.disableHierarchicalLookup = true; | ||
|
||
// Use src instead of lib | ||
config.resolver.resolveRequest = (context, moduleName, platform) => { | ||
if (moduleName.startsWith(pkg.name)) { | ||
return { | ||
filePath: path.resolve(__dirname, "..", "..", "src", "index.ts"), | ||
type: "sourceFile", | ||
}; | ||
} | ||
|
||
return context.resolveRequest(context, moduleName, platform); | ||
}; | ||
|
||
return config; | ||
} | ||
|
||
module.exports = withMonorepoPaths( | ||
getConfig(getDefaultConfig(project), { | ||
root, | ||
pkg, | ||
project, | ||
}), | ||
); | ||
module.exports = withMonorepoPaths(getDefaultConfig(project), { project }); |
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,50 +1,9 @@ | ||
/* eslint-env node */ | ||
const { | ||
withMonorepoPaths, | ||
} = require("@maplibre-react-native/examples/MetroWithMonorepoPaths"); | ||
const { getDefaultConfig } = require("@react-native/metro-config"); | ||
const path = require("path"); | ||
const { getConfig } = require("react-native-builder-bob/metro-config"); | ||
|
||
const pkg = require("../../package.json"); | ||
|
||
const project = __dirname; | ||
const root = path.resolve(project, "..", ".."); | ||
|
||
/** | ||
* @param config {import('metro-config').MetroConfig} | ||
* @returns {import('expo/metro-config').MetroConfig} | ||
*/ | ||
function withMonorepoPaths(config) { | ||
// Watch all files in the monorepo | ||
config.watchFolders = [root]; | ||
|
||
// Set `node_modules` to resolve | ||
config.resolver.nodeModulesPaths = [ | ||
path.resolve(project, "node_modules"), | ||
path.resolve(root, "packages/examples/node_modules"), | ||
path.resolve(root, "node_modules"), | ||
]; | ||
|
||
// Resolve only (sub)dependencies from the `nodeModulesPaths` | ||
config.resolver.disableHierarchicalLookup = true; | ||
|
||
// Use src instead of lib | ||
config.resolver.resolveRequest = (context, moduleName, platform) => { | ||
if (moduleName.startsWith(pkg.name)) { | ||
return { | ||
filePath: path.resolve(__dirname, "..", "..", "src", "index.ts"), | ||
type: "sourceFile", | ||
}; | ||
} | ||
|
||
return context.resolveRequest(context, moduleName, platform); | ||
}; | ||
|
||
return config; | ||
} | ||
|
||
module.exports = withMonorepoPaths( | ||
getConfig(getDefaultConfig(project), { | ||
root, | ||
pkg, | ||
project, | ||
}), | ||
); | ||
module.exports = withMonorepoPaths(getDefaultConfig(project), { project }); |