Skip to content

Commit a32620d

Browse files
Ives van Hoornefacebook-github-bot
authored andcommitted
Use new configuration in react-native public cli
Summary: Change the public react-native CLI to use the new configuration of Metro. Reviewed By: rafeca Differential Revision: D8801217 fbshipit-source-id: 112e4812b430ebee1ed41489f803b90c182ccdb4
1 parent f0daaf3 commit a32620d

File tree

7 files changed

+81
-140
lines changed

7 files changed

+81
-140
lines changed

local-cli/bundle/buildBundle.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@ const log = require('../util/log').out('bundle');
1414
/* $FlowFixMe(site=react_native_oss) */
1515
const Server = require('metro/src/Server');
1616

17-
const {convert} = require('metro-config');
18-
1917
/* $FlowFixMe(site=react_native_oss) */
2018
const outputBundle = require('metro/src/shared/output/bundle');
19+
const {convert} = require('metro-config');
2120
const path = require('path');
2221
const saveAssets = require('./saveAssets');
2322

24-
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
25-
2623
import type {RequestOptions, OutputOptions} from './types.flow';
2724
import type {ConfigT} from 'metro-config/src/configTypes.flow';
2825

@@ -48,6 +45,10 @@ async function buildBundle(
4845
sourceMapUrl = path.basename(sourceMapUrl);
4946
}
5047

48+
config.transformModulePath = args.transformer
49+
? path.resolve(args.transformer)
50+
: config.transformModulePath;
51+
5152
const requestOpts: RequestOptions = {
5253
entryFile: args.entryFile,
5354
sourceMapUrl,
@@ -56,13 +57,6 @@ async function buildBundle(
5657
platform: args.platform,
5758
};
5859

59-
const transformModulePath = args.transformer
60-
? path.resolve(args.transformer)
61-
: config.transformModulePath;
62-
63-
config.transformModulePath = transformModulePath;
64-
config.transformer.assetRegistryPath = ASSET_REGISTRY_PATH;
65-
6660
const {serverOptions} = convert.convertNewToOld(config);
6761

6862
const server = new Server(serverOptions);

local-cli/cliEntry.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
'use strict';
1212

13-
const config = require('./core');
13+
const {configPromise} = require('./core');
1414

1515
const assertRequiredOptions = require('./util/assertRequiredOptions');
1616
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
@@ -137,7 +137,8 @@ const addCommand = (command: CommandT, cfg: RNConfig) => {
137137
cmd.option('--config [string]', 'Path to the CLI configuration file');
138138
};
139139

140-
function run() {
140+
async function run() {
141+
const config = await configPromise;
141142
const setupEnvScript = /^win/.test(process.platform)
142143
? 'setup_env.bat'
143144
: 'setup_env.sh';

local-cli/core/index.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const findPlugins = require('./findPlugins');
1616
const findAssets = require('./findAssets');
1717
const ios = require('./ios');
1818
const wrapCommands = require('./wrapCommands');
19+
const {ASSET_REGISTRY_PATH} = require('./Constants');
1920

2021
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
2122
* found when Flow v0.54 was deployed. To see the error delete this comment and
@@ -28,18 +29,14 @@ const minimist = require('minimist');
2829
const path = require('path');
2930

3031
import type {CommandT} from '../commands';
31-
import type {ConfigT} from 'metro';
32+
import type {ConfigT} from 'metro-config/src/configTypes.flow';
3233

3334
export type RNConfig = {
3435
...ConfigT,
3536
/**
3637
* Returns an object with all platform configurations.
3738
*/
3839
getPlatformConfig(): Object,
39-
/**
40-
* Returns an array of project commands used by the CLI to load
41-
*/
42-
getProjectCommands(): Array<CommandT>,
4340
/**
4441
* Returns project config from the current working directory
4542
*/
@@ -69,7 +66,7 @@ const pluginPlatforms = plugins.platforms.reduce((acc, pathToPlatforms) => {
6966
);
7067
}, {});
7168

72-
const defaultRNConfig = {
69+
const defaultConfig = {
7370
hasteImplModulePath: require.resolve('../../jest/hasteImpl'),
7471

7572
getPlatforms(): Array<string> {
@@ -79,20 +76,9 @@ const defaultRNConfig = {
7976
getProvidesModuleNodeModules(): Array<string> {
8077
return ['react-native', 'react-native-windows'];
8178
},
79+
};
8280

83-
getProjectCommands(): Array<CommandT> {
84-
const commands = plugins.commands.map(pathToCommands => {
85-
const name = pathToCommands.split(path.sep)[0];
86-
87-
return attachPackage(
88-
require(path.join(appRoot, 'node_modules', pathToCommands)),
89-
require(path.join(appRoot, 'node_modules', name, 'package.json')),
90-
);
91-
});
92-
93-
return flatten(commands);
94-
},
95-
81+
const defaultRNConfig = {
9682
getPlatformConfig(): Object {
9783
return {
9884
ios,
@@ -141,14 +127,33 @@ const defaultRNConfig = {
141127
/**
142128
* Loads the CLI configuration
143129
*/
144-
function getCliConfig(): RNConfig {
130+
async function getCliConfig(): Promise<RNConfig> {
145131
const cliArgs = minimist(process.argv.slice(2));
146-
const config =
147-
cliArgs.config != null
148-
? Config.load(path.resolve(__dirname, cliArgs.config))
149-
: Config.findOptional(__dirname);
132+
const config = await Config.load(path.resolve(__dirname, cliArgs.config));
133+
134+
config.transformer.assetRegistryPath = ASSET_REGISTRY_PATH;
135+
config.resolver.hasteImplModulePath = defaultConfig.hasteImplModulePath;
136+
config.resolver.platforms = defaultConfig.getPlatforms();
137+
config.resolver.providesModuleNodeModules = defaultConfig.getProvidesModuleNodeModules();
150138

151139
return {...defaultRNConfig, ...config};
152140
}
153141

154-
module.exports = getCliConfig();
142+
/**
143+
* Returns an array of project commands used by the CLI to load
144+
*/
145+
function getProjectCommands(): Array<CommandT> {
146+
const commands = plugins.commands.map(pathToCommands => {
147+
const name = pathToCommands.split(path.sep)[0];
148+
149+
return attachPackage(
150+
require(path.join(appRoot, 'node_modules', pathToCommands)),
151+
require(path.join(appRoot, 'node_modules', name, 'package.json')),
152+
);
153+
});
154+
155+
return flatten(commands);
156+
}
157+
158+
module.exports.configPromise = getCliConfig();
159+
module.exports.getProjectCommands = getProjectCommands;

local-cli/dependencies/dependencies.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ const denodeify = require('denodeify');
1616
const fs = require('fs');
1717
const path = require('path');
1818

19-
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
20-
2119
async function dependencies(argv, configPromise, args, packagerInstance) {
2220
const rootModuleAbsolutePath = args.entryFile;
2321
const config = await configPromise;
@@ -31,12 +29,9 @@ async function dependencies(argv, configPromise, args, packagerInstance) {
3129
config.transformModulePath = args.transformer
3230
? path.resolve(args.transformer)
3331
: config.transformModulePath;
34-
config.transformer.transformModulePath = ASSET_REGISTRY_PATH;
35-
36-
const {serverOptions: packageOpts} = convert.convertNewToOld(config);
3732

3833
const relativePath = path.relative(
39-
packageOpts.projectRoot,
34+
config.projectRoot,
4035
rootModuleAbsolutePath,
4136
);
4237

@@ -53,18 +48,20 @@ async function dependencies(argv, configPromise, args, packagerInstance) {
5348
? fs.createWriteStream(args.output)
5449
: process.stdout;
5550

51+
const {serverOptions} = convert.convertNewToOld(config);
52+
5653
return Promise.resolve(
5754
(packagerInstance
5855
? packagerInstance.getOrderedDependencyPaths(options)
59-
: Metro.getOrderedDependencyPaths(packageOpts, options)
56+
: Metro.getOrderedDependencyPaths(serverOptions, options)
6057
).then(deps => {
6158
deps.forEach(modulePath => {
6259
// Temporary hack to disable listing dependencies not under this directory.
6360
// Long term, we need either
6461
// (a) JS code to not depend on anything outside this directory, or
6562
// (b) Come up with a way to declare this dependency in Buck.
6663
const isInsideProjectRoots =
67-
packageOpts.watchFolders.filter(root => modulePath.startsWith(root))
64+
config.watchFolders.filter(root => modulePath.startsWith(root))
6865
.length > 0;
6966

7067
if (isInsideProjectRoots) {

local-cli/server/runServer.js

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ const morgan = require('morgan');
2121
const path = require('path');
2222
const webSocketProxy = require('./util/webSocketProxy');
2323
const MiddlewareManager = require('./middleware/MiddlewareManager');
24-
const {convertOldToNew} = require('metro-config/src/convertConfig');
2524

26-
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
27-
28-
import type {ConfigT} from 'metro';
25+
import type {ConfigT} from 'metro-config/src/configTypes.flow';
2926

3027
export type Args = {|
3128
+assetExts: $ReadOnlyArray<string>,
@@ -57,23 +54,14 @@ async function runServer(args: Args, config: ConfigT) {
5754

5855
args.watchFolders.forEach(middlewareManager.serveStatic);
5956

57+
config.maxWorkers = args.maxWorkers;
58+
config.server.port = args.port;
59+
config.reporter = reporter;
60+
config.server.enhanceMiddleware = middleware =>
61+
middlewareManager.getConnectInstance().use(middleware);
62+
6063
const serverInstance = await Metro.runServer({
61-
config: convertOldToNew({
62-
config: {
63-
...config,
64-
assetRegistryPath: ASSET_REGISTRY_PATH,
65-
enhanceMiddleware: middleware =>
66-
middlewareManager.getConnectInstance().use(middleware),
67-
transformModulePath: args.transformer
68-
? path.resolve(args.transformer)
69-
: config.getTransformModulePath(),
70-
},
71-
maxWorkers: args.maxWorkers,
72-
port: args.port,
73-
reporter,
74-
}),
75-
76-
hmrEnabled: true,
64+
config,
7765
host: args.host,
7866
secure: args.https,
7967
secureCert: args.cert,

local-cli/server/server.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
const runServer = require('./runServer');
1414

1515
import type {RNConfig} from '../core';
16-
import type {ConfigT} from 'metro';
16+
import type {ConfigT} from 'metro-config/src/configTypes.flow';
1717
import type {Args as RunServerArgs} from './runServer';
1818

1919
/**
@@ -43,7 +43,7 @@ module.exports = {
4343
command: '--projectRoot [string]',
4444
description: 'Specify the main project root',
4545
default: (config: ConfigT) => {
46-
return config.getProjectRoot();
46+
return config.projectRoot;
4747
},
4848
},
4949
{
@@ -52,40 +52,39 @@ module.exports = {
5252
'Specify any additional folders to be added to the watch list',
5353
parse: (val: string) => val.split(','),
5454
default: (config: ConfigT) => {
55-
return config.getWatchFolders();
55+
return config.watchFolders;
5656
},
5757
},
5858
{
5959
command: '--assetExts [list]',
6060
description:
6161
'Specify any additional asset extensions to be used by the packager',
6262
parse: (val: string) => val.split(','),
63-
default: (config: ConfigT) => config.getAssetExts(),
63+
default: (config: ConfigT) => config.resolver.assetExts,
6464
},
6565
{
6666
command: '--sourceExts [list]',
6767
description:
6868
'Specify any additional source extensions to be used by the packager',
6969
parse: (val: string) => val.split(','),
70-
default: (config: ConfigT) => config.getSourceExts(),
70+
default: (config: ConfigT) => config.resolver.sourceExts,
7171
},
7272
{
7373
command: '--platforms [list]',
7474
description:
7575
'Specify any additional platforms to be used by the packager',
7676
parse: (val: string) => val.split(','),
77-
default: (config: ConfigT) => config.getPlatforms(),
77+
default: (config: ConfigT) => config.resolver.platforms,
7878
},
7979
{
8080
command: '--providesModuleNodeModules [list]',
8181
description:
8282
'Specify any npm packages that import dependencies with providesModule',
8383
parse: (val: string) => val.split(','),
8484
default: (config: RNConfig) => {
85-
if (typeof config.getProvidesModuleNodeModules === 'function') {
86-
return config.getProvidesModuleNodeModules();
87-
}
88-
return null;
85+
return config.resolver
86+
? config.resolver.providesModuleNodeModules
87+
: undefined;
8988
},
9089
},
9190
{

0 commit comments

Comments
 (0)