Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 2 additions & 34 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,38 +445,6 @@ Starts the server that communicates with connected devices

Specify port to listen on

#### `--projectRoot [path]`

Path to a custom project root

#### `--watchFolders [list]`

Specify any additional folders to be added to the watch list

#### `--assetExts [list]`

Specify any additional asset extensions to be used by the packager

#### `--sourceExts [list]`

Specify any additional source extensions to be used by the packager

#### `--platforms [list]`

Specify any additional platforms to be used by the packager

#### `--providesModuleNodeModules [list]`

Specify any npm packages that import dependencies with providesModule

#### `--max-workers [number]`

Specifies the maximum number of workers the worker-pool will spawn for transforming files. This defaults to the number of the cores available on your machine

#### `--transformer [string]`

Specify a custom transformer to be used

#### `--reset-cache, --resetCache`

Removes cached files
Expand All @@ -501,9 +469,9 @@ Path to custom SSL key

Path to custom SSL cert

#### `--config [string]`
#### `--metroConfig [string]`

Path to the CLI configuration file
Path to the Metro configuration file (to overwrite the one detected automatically)

### `uninstall`

Expand Down
21 changes: 2 additions & 19 deletions packages/cli/src/commands/server/runServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,15 @@ import loadMetroConfig from '../../tools/loadMetroConfig';
import releaseChecker from '../../tools/releaseChecker';

export type Args = {
assetPlugins?: string[];
cert?: string;
customLogReporterPath?: string;
host?: string;
https?: boolean;
maxWorkers?: number;
key?: string;
platforms?: string[];
port?: number;
resetCache?: boolean;
sourceExts?: string[];
transformer?: string;
verbose?: boolean;
watchFolders?: string[];
config?: string;
projectRoot?: string;
metroConfig?: string;
};

async function runServer(_argv: Array<string>, ctx: Config, args: Args) {
Expand All @@ -44,22 +37,12 @@ async function runServer(_argv: Array<string>, ctx: Config, args: Args) {
const reporter = new ReporterImpl(terminal);

const metroConfig = await loadMetroConfig(ctx, {
config: args.config,
maxWorkers: args.maxWorkers,
config: args.metroConfig,
port: args.port,
resetCache: args.resetCache,
watchFolders: args.watchFolders,
projectRoot: args.projectRoot,
sourceExts: args.sourceExts,
reporter,
});

if (args.assetPlugins) {
metroConfig.transformer.assetPlugins = args.assetPlugins.map(plugin =>
require.resolve(plugin),
);
}

const middlewareManager = new MiddlewareManager({
host: args.host,
port: metroConfig.server.port,
Expand Down
59 changes: 2 additions & 57 deletions packages/cli/src/commands/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*
*/

import path from 'path';
import runServer from './runServer';

Expand All @@ -22,60 +21,6 @@ export default {
name: '--host [string]',
default: '',
},
{
name: '--projectRoot [path]',
description: 'Path to a custom project root',
parse: (val: string) => path.resolve(val),
},
{
name: '--watchFolders [list]',
description:
'Specify any additional folders to be added to the watch list',
parse: (val: string) =>
val.split(',').map<string>((folder: string) => path.resolve(folder)),
},
{
name: '--assetPlugins [list]',
description:
'Specify any additional asset plugins to be used by the packager by full filepath',
parse: (val: string) => val.split(','),
},
{
name: '--assetExts [list]',
description:
'Specify any additional asset extensions to be used by the packager',
parse: (val: string) => val.split(','),
},
{
name: '--sourceExts [list]',
description:
'Specify any additional source extensions to be used by the packager',
parse: (val: string) => val.split(','),
},
{
name: '--platforms [list]',
description:
'Specify any additional platforms to be used by the packager',
parse: (val: string) => val.split(','),
},
{
name: '--providesModuleNodeModules [list]',
description:
'Specify any npm packages that import dependencies with providesModule',
parse: (val: string) => val.split(','),
},
{
name: '--max-workers [number]',
description:
'Specifies the maximum number of workers the worker-pool ' +
'will spawn for transforming files. This defaults to the number of the ' +
'cores available on your machine.',
parse: (workers: string) => Number(workers),
},
{
name: '--transformer [string]',
description: 'Specify a custom transformer to be used',
},
{
name: '--reset-cache, --resetCache',
description: 'Removes cached files',
Expand All @@ -102,8 +47,8 @@ export default {
description: 'Path to custom SSL cert',
},
{
name: '--config [string]',
description: 'Path to the CLI configuration file',
name: '--metroConfig [string]',
description: 'Path to a custom Metro configuration file',
parse: (val: string) => path.resolve(val),
},
],
Expand Down
51 changes: 32 additions & 19 deletions packages/cli/src/tools/loadMetroConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,33 @@ export interface MetroConfig {
reporter?: any;
}

/**
* Options that can be used to tweak the default configuration
* that is later passed to Metro
*/
type DefaultConfigOptions = {
port?: number;
reporter?: any;
};

/**
* Options that change the behaviour of Metro built-in `loadConfig`
* function
*
* Details here: https://github.com/facebook/metro/blob/master/packages/metro-config/src/loadConfig.js#L28-L45
*/
export type ConfigOptions = DefaultConfigOptions & {
resetCache?: boolean;
config?: string;
};

/**
* Default configuration
*/
export const getDefaultConfig = (ctx: Config): MetroConfig => {
export const getDefaultConfig = (
ctx: Config,
opts: DefaultConfigOptions,
): MetroConfig => {
return {
resolver: {
resolverMainFields: ['react-native', 'browser', 'main'],
Expand All @@ -79,7 +102,7 @@ export const getDefaultConfig = (ctx: Config): MetroConfig => {
require(path.join(ctx.reactNativePath, 'rn-get-polyfills'))(),
},
server: {
port: Number(process.env.RCT_METRO_PORT) || 8081,
port: Number(process.env.RCT_METRO_PORT) || opts.port || 8081,
},
symbolicator: {
customizeFrame: (frame: {file: string | null}) => {
Expand All @@ -99,32 +122,22 @@ export const getDefaultConfig = (ctx: Config): MetroConfig => {
),
},
watchFolders: getWatchFolders(),
reporter: opts.reporter,
};
};

export interface ConfigOptionsT {
maxWorkers?: number;
port?: number;
projectRoot?: string;
resetCache?: boolean;
watchFolders?: string[];
sourceExts?: string[];
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.
*/
export default function load(
ctx: Config,
options?: ConfigOptionsT,
opts: ConfigOptions = {},
): Promise<MetroConfig> {
const defaultConfig = getDefaultConfig(ctx);
if (options && options.reporter) {
defaultConfig.reporter = options.reporter;
}
return loadConfig({cwd: ctx.root, ...options}, defaultConfig);
const defaultConfig = getDefaultConfig(ctx, opts);
return loadConfig(
{cwd: ctx.root, resetCache: opts.resetCache, config: opts.config},
defaultConfig,
);
}