From 582d585380859eaf36d1d33b9ce5ffb0cafa8d6e Mon Sep 17 00:00:00 2001 From: Kilian Finger Date: Sat, 23 Nov 2024 09:00:54 +0100 Subject: [PATCH] refactor: reuse monorepo metro config --- packages/examples/MetroWithMonorepoPaths.js | 53 +++++++++++++++++++++ packages/expo-app/metro.config.js | 49 ++----------------- packages/react-native-app/metro.config.js | 49 ++----------------- 3 files changed, 61 insertions(+), 90 deletions(-) create mode 100644 packages/examples/MetroWithMonorepoPaths.js diff --git a/packages/examples/MetroWithMonorepoPaths.js b/packages/examples/MetroWithMonorepoPaths.js new file mode 100644 index 000000000..7a27ac887 --- /dev/null +++ b/packages/examples/MetroWithMonorepoPaths.js @@ -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; diff --git a/packages/expo-app/metro.config.js b/packages/expo-app/metro.config.js index c9bed4911..f8652dd01 100644 --- a/packages/expo-app/metro.config.js +++ b/packages/expo-app/metro.config.js @@ -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 }); diff --git a/packages/react-native-app/metro.config.js b/packages/react-native-app/metro.config.js index 8320f44bf..e3fc1d297 100644 --- a/packages/react-native-app/metro.config.js +++ b/packages/react-native-app/metro.config.js @@ -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 });