Skip to content

Commit 760708f

Browse files
brentvatnethymikee
andauthored
feat: default to enableOptionalDependencies: true in metro config (#1350)
* feat: default to enableOptionalDependencies: true in metro config * feat: add test for getDefaultConfig * simplify type Co-authored-by: Michał Pierzchała <[email protected]>
1 parent 22a3c25 commit 760708f

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {getDefaultConfig} from '../loadMetroConfig';
2+
3+
jest.mock('fs');
4+
jest.mock('path');
5+
6+
describe('getDefaultConfig', () => {
7+
it('should preserve transformer.allowOptionalDependencies=true when overriding other transformer options', async () => {
8+
const config = getDefaultConfig({
9+
root: '/',
10+
reactNativePath: '',
11+
platforms: {},
12+
});
13+
14+
expect(config.transformer.allowOptionalDependencies).toBe(true);
15+
});
16+
});

packages/cli/src/tools/loadMetroConfig.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ const INTERNAL_CALLSITES_REGEX = new RegExp(
2020
].join('|'),
2121
);
2222

23+
type ConfigLoadingContext = Pick<
24+
Config,
25+
'root' | 'reactNativePath' | 'platforms'
26+
>;
27+
2328
export interface MetroConfig {
2429
resolver: {
2530
resolveRequest?: (
@@ -43,6 +48,7 @@ export interface MetroConfig {
4348
customizeFrame: (frame: {file: string | null}) => {collapse: boolean};
4449
};
4550
transformer: {
51+
allowOptionalDependencies?: boolean;
4652
babelTransformerPath: string;
4753
assetRegistryPath: string;
4854
assetPlugins?: Array<string>;
@@ -55,7 +61,7 @@ export interface MetroConfig {
5561
/**
5662
* Default configuration
5763
*/
58-
export const getDefaultConfig = (ctx: Config): MetroConfig => {
64+
export const getDefaultConfig = (ctx: ConfigLoadingContext): MetroConfig => {
5965
const outOfTreePlatforms = Object.keys(ctx.platforms).filter(
6066
(platform) => ctx.platforms[platform].npmPackageName,
6167
);
@@ -106,6 +112,7 @@ export const getDefaultConfig = (ctx: Config): MetroConfig => {
106112
},
107113
},
108114
transformer: {
115+
allowOptionalDependencies: true,
109116
babelTransformerPath: require.resolve(
110117
'metro-react-native-babel-transformer',
111118
),
@@ -135,7 +142,7 @@ export interface ConfigOptionsT {
135142
* This allows the CLI to always overwrite the file settings.
136143
*/
137144
export default function load(
138-
ctx: Config,
145+
ctx: ConfigLoadingContext,
139146
options?: ConfigOptionsT,
140147
): Promise<MetroConfig> {
141148
const defaultConfig = getDefaultConfig(ctx);

0 commit comments

Comments
 (0)