Skip to content

Commit

Permalink
refactor: reuse monorepo metro config
Browse files Browse the repository at this point in the history
  • Loading branch information
KiwiKilian committed Nov 23, 2024
1 parent 9db96cf commit 582d585
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 90 deletions.
53 changes: 53 additions & 0 deletions packages/examples/MetroWithMonorepoPaths.js
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;
49 changes: 4 additions & 45 deletions packages/expo-app/metro.config.js
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 });
49 changes: 4 additions & 45 deletions packages/react-native-app/metro.config.js
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 });

0 comments on commit 582d585

Please sign in to comment.