Skip to content

Decouple Metro config from CLI config #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4396a67
Initial commit - delete Metro-related config
grabbou Dec 3, 2018
0995784
Rewrite of the config, part 2
grabbou Dec 3, 2018
b1ae80e
Finish adopting link & unlink to new config format
grabbou Dec 3, 2018
18c0e7b
Rewrite 'server' command and the way we load config
grabbou Dec 3, 2018
b3cce4e
Fix errors and make server run
grabbou Dec 3, 2018
e3f44c0
Use require.resolve
grabbou Dec 3, 2018
aba53ce
Simplify runServer code
grabbou Dec 3, 2018
e32d6c7
Updates to the config
grabbou Dec 3, 2018
6d3afbf
Remove duplicate platforms
grabbou Dec 3, 2018
6e21ad5
Use real and unified project root
grabbou Dec 3, 2018
c5d3e41
Fix projectRoots
grabbou Dec 3, 2018
9793d32
Unifiy root paths
grabbou Dec 3, 2018
cb76d47
Remove projectRoot from type
grabbou Dec 3, 2018
cf98b5a
Leave two todos for tomororw
grabbou Dec 3, 2018
15e43ba
Use projectRoot to read the root
grabbou Dec 3, 2018
038c190
Fix projectRoot and implement config again
grabbou Dec 3, 2018
213c130
Improve Metro config
grabbou Dec 4, 2018
ae258fd
Fix configuration again
grabbou Dec 4, 2018
372084a
Fix remaining Flow issues inside bundle
grabbou Dec 4, 2018
a67fe59
Fix two failing test suites
grabbou Dec 4, 2018
e2fa20b
Fix getDependencyConfig tests and use snapshots
grabbou Dec 4, 2018
46c9ae2
Remove console
grabbou Dec 4, 2018
b9ca8b7
Fix remaining Jest tests
grabbou Dec 4, 2018
3cb7bc6
Remove console.log
grabbou Dec 4, 2018
5bf9c71
Fix issues with link/unlink tested in production
grabbou Dec 4, 2018
40528b0
Add legacy config to avoid breaking changes
grabbou Dec 7, 2018
63fbef1
Fix a typo
grabbou Dec 7, 2018
1560725
Exclude React Native CLI from being linked
grabbou Dec 10, 2018
f20605e
Remove console.log from production code
grabbou Dec 10, 2018
7a67759
Update getCommands.js
grabbou Dec 15, 2018
da4136d
Update loadMetroConfig.js
grabbou Dec 15, 2018
4407e42
Update package.json
grabbou Dec 15, 2018
2925aa2
Update package.json
grabbou Dec 15, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve Metro config
  • Loading branch information
grabbou committed Dec 4, 2018
commit 213c130a119176426d1e9799c0538d59e1d9c305
1 change: 1 addition & 0 deletions packages/local-cli/cliEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ const addCommand = (command: CommandT, ctx: ContextT) => {
typeof opt.default === 'function' ? opt.default(ctx) : opt.default,
),
);

// This is needed to avoid `unknown option` error by Commander.js
cmd.option('--projectRoot [string]', 'Path to the root of the project');
};
Expand Down
2 changes: 0 additions & 2 deletions packages/local-cli/core/getAssets.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/**
* @flow
*
* @todo(grabbou): This file should be moved to a `link`-related folder.
*/

'use strict';
Expand Down
3 changes: 0 additions & 3 deletions packages/local-cli/core/getHooks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
/**
* @flow
*
* @todo(grabbou): This file should be moved to a `link`-related folder.

*/

'use strict';
Expand Down
2 changes: 0 additions & 2 deletions packages/local-cli/core/getParams.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/**
* @flow
*
* @todo(grabbou): This file should be moved to a `link`-related folder.
*/

'use strict';
Expand Down
1 change: 0 additions & 1 deletion packages/local-cli/core/types.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export type PlatformsT = {
[name: string]: PlatformConfigT<any, any, any>
};

// @todo(grabbou): Get this from `DefinitelyTyped` or somewhere
export type InquirerPromptT = any;

/**
Expand Down
97 changes: 53 additions & 44 deletions packages/local-cli/util/loadMetroConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,63 +43,51 @@ const getBlacklistRE = () => {
/**
* Default configuration
*/
const getDefaultConfig = () => ({
resolver: {
resolverMainFields: ['react-native', 'browser', 'main'],
blacklistRE: getBlacklistRE()
},
serializer: {
getModulesRunBeforeMainModule: () => [
require.resolve('react-native/Libraries/Core/InitializeCore'),
],
getPolyfills: require('react-native/rn-get-polyfills'),
},
server: {
port: process.env.RCT_METRO_PORT || 8081,
},
transformer: {
babelTransformerPath: require.resolve('metro/src/reactNativeTransformer'),
},
watchFolders: getWatchFolders(),
});
const getDefaultConfig = (root: string) => {
const plugins = findPlugins(root);

return ({
resolver: {
resolverMainFields: ['react-native', 'browser', 'main'],
blacklistRE: getBlacklistRE(),
platforms: ['ios', 'android', 'native', ...plugins.haste.platforms],
providesModuleNodeModules: plugins.haste.providesModuleNodeModules,
hasteImplModulePath: require.resolve('react-native/jest/hasteImpl')
},
serializer: {
getModulesRunBeforeMainModule: () => [
require.resolve('react-native/Libraries/Core/InitializeCore'),
],
getPolyfills: require('react-native/rn-get-polyfills'),
},
server: {
port: process.env.RCT_METRO_PORT || 8081,
},
transformer: {
babelTransformerPath: require.resolve('metro/src/reactNativeTransformer'),
assetRegistryPath: require.resolve('react-native/Libraries/Image/AssetRegistry')
},
watchFolders: getWatchFolders(),
});
};

export type ConfigOptionsT = {
maxWorkers?: number,
port?: number,
resetCache?: boolean,
watchFolders?: string[],
sourceExts?: string[],
reporter: any,
reporter?: any,
config?: string,
};

/**
* Loads Metro Config and applies `options` on top of the resolved config.
*
* This allows the CLI to always overwrite the file settings.
* Overwrites Metro configuration with options.
*
* @todo(grabbou): Is this really how we want it?
* Is it breaking to just use "defaults"?
* This ensures that options are always taking precedence over other
* configuration present.
*/
module.exports = async function load(projectRoot: string, options: ConfigOptionsT): Promise<ConfigT> {
const plugins = findPlugins(projectRoot);

const config = await loadConfig({
cwd: projectRoot,
config: options.config
}, getDefaultConfig());

config.transformer.assetRegistryPath = 'react-native/Libraries/Image/AssetRegistry';

config.resolver.hasteImplModulePath =
config.resolver.hasteImplModulePath || require.resolve('react-native/jest/hasteImpl');

config.resolver.platforms = config.resolver.platforms
.concat(plugins.haste.platforms);

config.resolver.providesModuleNodeModules = config.resolver.providesModuleNodeModules
.concat(plugins.haste.providesModuleNodeModules);

const overwriteWithOptions = (config: ConfigT, options: ConfigOptionsT) => {
if (options.maxWorkers) {
config.maxWorkers = options.maxWorkers;
}
Expand Down Expand Up @@ -128,6 +116,27 @@ module.exports = async function load(projectRoot: string, options: ConfigOptions
config.resolver.sourceExts,
);
}
};

/**
* Loads Metro Config and applies `options` on top of the resolved config.
*
* This allows the CLI to always overwrite the file settings.
*/
module.exports = async function load(
projectRoot: string,
options?: ConfigOptionsT = {}
): Promise<ConfigT> {
const defaultConfig = getDefaultConfig(projectRoot);

const config = await loadConfig({
cwd: projectRoot,
config: options.config
}, defaultConfig);

if (options) {
overwriteWithOptions(config, options);
}

return config;
};