Skip to content

Commit

Permalink
Merge pull request #1770 from vvsamin/fix/metro_config_merge
Browse files Browse the repository at this point in the history
Fix and improve metro config merging
  • Loading branch information
pauliusguzas authored Oct 29, 2024
2 parents d219d67 + 5c7c066 commit 5308230
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 125 deletions.
74 changes: 43 additions & 31 deletions packages/engine-rn-macos/src/adapters/metroAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Env } from '@rnv/core';
import { InputConfig } from '@rnv/sdk-react-native';
import { withMetroConfig, mergeConfig, InputConfig } from '@rnv/sdk-react-native';

const path = require('path');

const sharedBlacklist = [
const sharedExclusions = [
/node_modules\/react\/dist\/.*/,
/website\/node_modules\/.*/,
/heapCapture\/bundle\.js/,
Expand All @@ -21,20 +21,22 @@ function escapeRegExp(pattern: RegExp | string) {
} else if (Object.prototype.toString.call(pattern) === '[object RegExp]') {
return pattern.source.replace(/\//g, path.sep);
}
throw new Error(`Unexpected blacklist pattern: ${pattern}`);
throw new Error(`Unexpected exclusion pattern: ${pattern}`);
}

function blacklist(additionalBlacklist: RegExp[]) {
return new RegExp(`(${(additionalBlacklist || []).concat(sharedBlacklist).map(escapeRegExp).join('|')})$`);
function exclusionList(additionalExclusions: RegExp[]) {
return [...additionalExclusions, ...sharedExclusions].map((regexp) => new RegExp(escapeRegExp(regexp)));
}

export const withRNVMetro = (config: InputConfig): InputConfig => {
const projectPath = process.env.RNV_PROJECT_ROOT || process.cwd();
const projectPath = env.RNV_PROJECT_ROOT || process.cwd();

const defaultConfig = withMetroConfig(projectPath);

const watchFolders = [path.resolve(projectPath, 'node_modules')];

if (env.RNV_IS_MONOREPO === 'true' || env.RNV_IS_MONOREPO === true) {
const monoRootPath = process.env.RNV_MONO_ROOT || projectPath;
const monoRootPath = env.RNV_MONO_ROOT || projectPath;
watchFolders.push(path.resolve(monoRootPath, 'node_modules'));
watchFolders.push(path.resolve(monoRootPath, 'packages'));
}
Expand All @@ -44,37 +46,47 @@ export const withRNVMetro = (config: InputConfig): InputConfig => {

const exts: string = env.RNV_EXTENSIONS || '';

const cnf = {
...config,
const cnfRnv: InputConfig = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
// this defeats the RCTDeviceEventEmitter is not a registered callable module
inlineRequires: true,
},
}),
...(config?.transformer || {}),
getTransformOptions: async (entryPoints, options, getDependenciesOf) => {
const transformOptions =
(await config?.transformer?.getTransformOptions?.(entryPoints, options, getDependenciesOf)) || {};

return {
...transformOptions,
transform: {
experimentalImportSupport: false,
// this defeats the RCTDeviceEventEmitter is not a registered callable module
inlineRequires: true,
...(transformOptions?.transform || {}),
},
};
},
},
resolver: {
blacklistRE: blacklist([
/platformBuilds\/.*/,
/buildHooks\/.*/,
/projectConfig\/.*/,
/website\/.*/,
/appConfigs\/.*/,
/renative.local.*/,
/metro.config.local.*/,
/.expo\/.*/,
/.rollup.cache\/.*/,
]),
...(config?.resolver || {}),
blockList: exclusionList(
[
/platformBuilds\/.*/,
/buildHooks\/.*/,
/projectConfig\/.*/,
/website\/.*/,
/appConfigs\/.*/,
/renative.local.*/,
/metro.config.local.*/,
/.expo\/.*/,
/.rollup.cache\/.*/,
]
.concat(config?.resolver?.blockList || [])
.concat(config?.resolver?.blacklistRE || [])
),
blacklistRE: undefined, // must be reset to prevent it from being processed by metro
sourceExts: [...(config?.resolver?.sourceExts || []), ...exts.split(',')],
extraNodeModules: config?.resolver?.extraNodeModules,
},
watchFolders,
projectRoot: path.resolve(projectPath),
projectRoot: config?.projectRoot || path.resolve(projectPath),
};

const cnf = mergeConfig(defaultConfig, config, cnfRnv);

return cnf;
};
98 changes: 64 additions & 34 deletions packages/engine-rn-tvos/src/adapters/metroAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const path = require('path');
const os = require('os');
const { doResolve } = require('@rnv/core');

const sharedBlacklist = [
const sharedExclusions = [
/node_modules\/react\/dist\/.*/,
/website\/node_modules\/.*/,
/heapCapture\/bundle\.js/,
Expand All @@ -23,11 +23,11 @@ function escapeRegExp(pattern: RegExp | string) {
} else if (Object.prototype.toString.call(pattern) === '[object RegExp]') {
return pattern.source.replace(/\//g, path.sep);
}
throw new Error(`Unexpected blacklist pattern: ${pattern}`);
throw new Error(`Unexpected exclusion pattern: ${pattern}`);
}

function blacklist(additionalBlacklist: RegExp[]) {
return new RegExp(`(${(additionalBlacklist || []).concat(sharedBlacklist).map(escapeRegExp).join('|')})$`);
function exclusionList(additionalExclusions: RegExp[]) {
return [...additionalExclusions, ...sharedExclusions].map((regexp) => new RegExp(escapeRegExp(regexp)));
}

export const withRNVMetro = (config: InputConfig) => {
Expand All @@ -53,20 +53,43 @@ export const withRNVMetro = (config: InputConfig) => {
const exts: string = env.RNV_EXTENSIONS || '';

const cnfRnv: InputConfig = {
cacheStores: [
new FileStore({
root: path.join(os.tmpdir(), 'metro-cache-tvos'),
}),
],
cacheStores: (metroCache) => {
let cacheStores: ReturnType<Extract<InputConfig['cacheStores'], (...args: any[]) => any>> = [];

if (typeof config?.cacheStores === 'function') {
cacheStores = config.cacheStores(metroCache);
} else if (config?.cacheStores?.length) {
// eslint-disable-next-line prefer-destructuring
cacheStores = config.cacheStores;
}

cacheStores = [
...cacheStores,
new FileStore({
root: path.join(os.tmpdir(), 'metro-cache-tvos'),
}),
];

return cacheStores;
},
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
// this defeats the RCTDeviceEventEmitter is not a registered callable module
inlineRequires: true,
},
}),
assetRegistryPath: path.resolve(`${doResolve('react-native-tvos')}/Libraries/Image/AssetRegistry.js`),
getTransformOptions: async (entryPoints, options, getDependenciesOf) => {
const transformOptions =
(await config?.transformer?.getTransformOptions?.(entryPoints, options, getDependenciesOf)) || {};

return {
...transformOptions,
transform: {
experimentalImportSupport: false,
// this defeats the RCTDeviceEventEmitter is not a registered callable module
inlineRequires: true,
...(transformOptions?.transform || {}),
},
};
},
assetRegistryPath:
config?.transformer?.assetRegistryPath ||
path.resolve(`${doResolve('react-native-tvos')}/Libraries/Image/AssetRegistry.js`),
},
resolver: {
resolveRequest: (context, moduleName, platform) => {
Expand All @@ -77,31 +100,38 @@ export const withRNVMetro = (config: InputConfig) => {
platform
);
}

// Chain to the custom config resolver if provided.
if (typeof config?.resolver?.resolveRequest === 'function') {
return config.resolver.resolveRequest(context, moduleName, platform);
}

// Optionally, chain to the standard Metro resolver.
return context.resolveRequest(context, moduleName, platform);
},
blacklistRE: blacklist([
/platformBuilds\/.*/,
/buildHooks\/.*/,
/projectConfig\/.*/,
/website\/.*/,
/appConfigs\/.*/,
/renative.local.*/,
/metro.config.local.*/,
/.expo\/.*/,
/.rollup.cache\/.*/,
]),
...(config?.resolver || {}),
blockList: exclusionList(
[
/platformBuilds\/.*/,
/buildHooks\/.*/,
/projectConfig\/.*/,
/website\/.*/,
/appConfigs\/.*/,
/renative.local.*/,
/metro.config.local.*/,
/.expo\/.*/,
/.rollup.cache\/.*/,
]
.concat(config?.resolver?.blockList || [])
.concat(config?.resolver?.blacklistRE || [])
),
blacklistRE: undefined, // must be reset to prevent it from being processed by metro
sourceExts: [...(config?.resolver?.sourceExts || []), ...exts.split(',')],
extraNodeModules: config?.resolver?.extraNodeModules,
},
watchFolders,
projectRoot: path.resolve(projectPath),
projectRoot: config?.projectRoot || path.resolve(projectPath),
};

const cnfWithRnv = mergeConfig(defaultConfig, cnfRnv);

const cnf = mergeConfig(cnfWithRnv, config);
const cnf = mergeConfig(defaultConfig, config, cnfRnv);

return cnf;
};
83 changes: 48 additions & 35 deletions packages/engine-rn-windows/src/adapters/metroAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const path = require('path');
import { Env } from '@rnv/core';
import fs from 'fs';
import { InputConfig } from '@rnv/sdk-react-native';
import { withMetroConfig, mergeConfig, InputConfig } from '@rnv/sdk-react-native';

const path = require('path');

const sharedBlacklist = [
const sharedExclusions = [
/node_modules\/react\/dist\/.*/,
/website\/node_modules\/.*/,
/heapCapture\/bundle\.js/,
Expand All @@ -21,21 +22,24 @@ function escapeRegExp(pattern: RegExp | string) {
} else if (Object.prototype.toString.call(pattern) === '[object RegExp]') {
return pattern.source.replace(/\//g, path.sep);
}
throw new Error(`Unexpected blacklist pattern: ${pattern}`);
throw new Error(`Unexpected exclusion pattern: ${pattern}`);
}

function blacklist(additionalBlacklist: RegExp[]) {
return new RegExp(`(${(additionalBlacklist || []).concat(sharedBlacklist).map(escapeRegExp).join('|')})$`);
function exclusionList(additionalExclusions: RegExp[]) {
return [...additionalExclusions, ...sharedExclusions].map((regexp) => new RegExp(escapeRegExp(regexp)));
}

export const withRNVMetro = (config: InputConfig): InputConfig => {
const projectPath = process.env.RNV_PROJECT_ROOT || process.cwd();
const projectPath = env.RNV_PROJECT_ROOT || process.cwd();

const rnwPath = fs.realpathSync(path.resolve(require.resolve('react-native-windows/package.json'), '..'));

const defaultConfig = withMetroConfig(projectPath);

const watchFolders = [path.resolve(projectPath, 'node_modules')];

if (env.RNV_IS_MONOREPO === 'true' || env.RNV_IS_MONOREPO === true) {
const monoRootPath = process.env.RNV_MONO_ROOT || projectPath;
const monoRootPath = env.RNV_MONO_ROOT || projectPath;
watchFolders.push(path.resolve(monoRootPath, 'node_modules'));
watchFolders.push(path.resolve(monoRootPath, 'packages'));
}
Expand All @@ -45,42 +49,51 @@ export const withRNVMetro = (config: InputConfig): InputConfig => {

const exts: string = env.RNV_EXTENSIONS || '';

const cnf = {
...config,
const cnfRnv: InputConfig = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
// this defeats the RCTDeviceEventEmitter is not a registered callable module
inlineRequires: true,
},
}),
...(config?.resolver || {}),
sourceExts: [...(config?.resolver?.sourceExts || []), ...exts.split(',')],
extraNodeModules: config?.resolver?.extraNodeModules,
...(config?.transformer || {}),
getTransformOptions: async (entryPoints, options, getDependenciesOf) => {
const transformOptions =
(await config?.transformer?.getTransformOptions?.(entryPoints, options, getDependenciesOf)) || {};

return {
...transformOptions,
transform: {
experimentalImportSupport: false,
// this defeats the RCTDeviceEventEmitter is not a registered callable module
inlineRequires: true,
...(transformOptions?.transform || {}),
},
};
},
},
resolver: {
blacklistRE: blacklist([
/platformBuilds\/.*/,
/buildHooks\/.*/,
/projectConfig\/.*/,
/website\/.*/,
/appConfigs\/.*/,
/renative.local.*/,
/metro.config.local.*/,
/.expo\/.*/,
/.rollup.cache\/.*/,
]),
blockList: exclusionList(
[
/platformBuilds\/.*/,
/buildHooks\/.*/,
/projectConfig\/.*/,
/website\/.*/,
/appConfigs\/.*/,
/renative.local.*/,
/metro.config.local.*/,
/.expo\/.*/,
/.rollup.cache\/.*/,
]
.concat(config?.resolver?.blockList || [])
.concat(config?.resolver?.blacklistRE || [])
),
blacklistRE: undefined, // must be reset to prevent it from being processed by metro
sourceExts: [...(config?.resolver?.sourceExts || []), ...exts.split(',')],
extraNodeModules: {
// Redirect react-native-windows to avoid symlink (metro doesn't like symlinks)
...(config?.resolver?.extraNodeModules || {}),
'react-native-windows': rnwPath,
},
},
watchFolders,
sourceExts: [...(config?.resolver?.sourceExts || []), ...exts.split(',')],
projectRoot: path.resolve(projectPath),
projectRoot: config?.projectRoot || path.resolve(projectPath),
};

const cnf = mergeConfig(defaultConfig, config, cnfRnv);

return cnf;
};
Loading

0 comments on commit 5308230

Please sign in to comment.