Skip to content

Commit 1d78ba9

Browse files
committed
fix(config): add hook for mergeConfig to pass cwd for internal extends processor
1 parent 6d6b01e commit 1d78ba9

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

packages/metro-config/src/loadConfig.js

+37-13
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ async function resolveConfig(
103103
return result;
104104
}
105105

106-
const resolveExtra = (base: any = {}, key: string, resolver) => {
106+
const resolveExtra = (base: any = {}, key: string, resolver: Function) => {
107107
const value = base[key];
108108
return value != null ? {[key]: resolver(value)} : {};
109109
};
@@ -115,17 +115,25 @@ function mergeConfig<T: $ReadOnly<InputConfigT>>(
115115
// If the file is a plain object we merge the file with the default config,
116116
// for the function we don't do this since that's the responsibility of the user
117117
return xtends.populateSync(defaultConfig, {
118+
// $FlowFixMe[prop-missing]
119+
cwd: mergeConfig.cwd,
118120
extends: configs.map(c => {
119121
return {
120122
...c,
121123
transformer: {
122124
...(c.transformer || {}),
123-
...resolveExtra(c.transformer, 'babelTransformerPath', resolve),
125+
...(resolveExtra(c.transformer, 'babelTransformerPath', resolve): {
126+
babelTransformerPath?: string,
127+
}),
124128
},
125129
resolver: {
126130
...(c.resolver || {}),
127-
...resolveExtra(c.resolver, 'dependencyExtractor', resolve),
128-
...resolveExtra(c.resolver, 'hasteImplModulePath', resolve),
131+
...(resolveExtra(c.resolver, 'dependencyExtractor', resolve): {
132+
dependencyExtractor?: string,
133+
}),
134+
...(resolveExtra(c.resolver, 'hasteImplModulePath', resolve): {
135+
hasteImplModulePath?: string,
136+
}),
129137
},
130138
...resolveExtra(c, 'cacheStores', stores => {
131139
return typeof stores === 'function' ? stores(MetroCache) : stores;
@@ -146,6 +154,24 @@ function mergeConfig<T: $ReadOnly<InputConfigT>>(
146154
});
147155
}
148156

157+
// mergeConfig is public, we should try to keep the API stable
158+
// We use this trick to pass cwd to `populateSync`
159+
const mergeConfigHooked = <T: $ReadOnly<InputConfigT>>(
160+
cwd: string,
161+
base: T,
162+
extra: ConfigT | InputConfigT,
163+
): T => {
164+
// $FlowFixMe[prop-missing]
165+
mergeConfig.cwd = cwd;
166+
167+
// $FlowFixMe[incompatible-variance]
168+
// $FlowFixMe[incompatible-call]
169+
const config = mergeConfig(base, extra);
170+
// $FlowFixMe[prop-missing]
171+
mergeConfig.cwd = undefined;
172+
return config;
173+
};
174+
149175
async function loadMetroConfigFromDisk(
150176
path?: string,
151177
cwd?: string,
@@ -160,23 +186,21 @@ async function loadMetroConfigFromDisk(
160186
const rootPath = dirname(filepath);
161187

162188
const defaults = await getDefaultConfig(rootPath);
163-
// $FlowFixMe[incompatible-variance]
164-
// $FlowFixMe[incompatible-call]
165-
const defaultConfig: ConfigT = mergeConfig(defaults, defaultConfigOverrides);
189+
const defaultConfig: ConfigT = mergeConfigHooked(
190+
rootPath,
191+
defaults,
192+
defaultConfigOverrides,
193+
);
166194

167195
if (typeof configModule === 'function') {
168196
// Get a default configuration based on what we know, which we in turn can pass
169197
// to the function.
170198

171199
const resultedConfig = await configModule(defaultConfig);
172-
// $FlowFixMe[incompatible-call]
173-
// $FlowFixMe[incompatible-variance]
174-
return mergeConfig(defaultConfig, resultedConfig);
200+
return mergeConfigHooked(rootPath, defaultConfig, resultedConfig);
175201
}
176202

177-
// $FlowFixMe[incompatible-variance]
178-
// $FlowFixMe[incompatible-call]
179-
return mergeConfig(defaultConfig, configModule);
203+
return mergeConfigHooked(rootPath, defaultConfig, configModule);
180204
}
181205

182206
function overrideConfigWithArguments(

0 commit comments

Comments
 (0)