Skip to content
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

chore: convert loadMetroConfig to TS #704

Merged
merged 4 commits into from
Sep 10, 2019
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
1 change: 1 addition & 0 deletions packages/cli/src/commands/bundle/buildBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import chalk from 'chalk';
import type {CommandLineArgs} from './bundleCommandLineArgs';
import type {ConfigT} from 'types';
import saveAssets from './saveAssets';
// $FlowFixMe - converted to typescript
import loadMetroConfig from '../../tools/loadMetroConfig';
import {logger} from '@react-native-community/cli-tools';

Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/commands/server/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {ConfigT} from 'types';
import messageSocket from './messageSocket';
import webSocketProxy from './webSocketProxy';
import MiddlewareManager from './middleware/MiddlewareManager';
// $FlowFixMe - converted to TS
import loadMetroConfig from '../../tools/loadMetroConfig';
// $FlowFixMe - converted to TS
import releaseChecker from '../../tools/releaseChecker';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
/**
* Configuration file of Metro.
* @flow
*/
import path from 'path';
// @ts-ignore - no typed definition for the package
import {createBlacklist} from 'metro';
// @ts-ignore - no typed definition for the package
import {loadConfig} from 'metro-config';
import {existsSync} from 'fs';
import {type ConfigT} from 'types';
// $FlowFixMe - converted to TS
import {Config} from '@react-native-community/cli-types';
import findSymlinkedModules from './findSymlinkedModules';

const resolveSymlinksForRoots = roots =>
roots.reduce<string[]>(
function resolveSymlinksForRoots(roots: string[]): string[] {
return roots.reduce<string[]>(
(arr, rootPath) => arr.concat(findSymlinkedModules(rootPath, roots)),
[...roots],
);
}

const getWatchFolders = () => {
function getWatchFolders(): string[] {
const root = process.env.REACT_NATIVE_APP_ROOT;
if (root) {
return resolveSymlinksForRoots([path.resolve(root)]);
}
return [];
};
return root ? resolveSymlinksForRoots([path.resolve(root)]) : [];
}

const getBlacklistRE = () => createBlacklist([/.*\/__fixtures__\/.*/]);
const getBlacklistRE: () => RegExp = () =>
createBlacklist([/.*\/__fixtures__\/.*/]);

const INTERNAL_CALLSITES_REGEX = new RegExp(
[
Expand All @@ -33,13 +32,39 @@ const INTERNAL_CALLSITES_REGEX = new RegExp(
].join('|'),
);

export interface DefaultConfigOption {
resolver: {
resolverMainFields: string[];
blacklistRE: RegExp;
platforms: string[];
providesModuleNodeModules: string[];
hasteImplModulePath: string | undefined;
};
serializer: {
getModulesRunBeforeMainModule: () => string[];
getPolyfills: () => any;
};
server: {
port: number;
};
symbolicator: {
customizeFrame: (frame: {file: string | null}) => {collapse: boolean};
};
transformer: {
babelTransformerPath: string;
assetRegistryPath: string;
};
watchFolders: string[];
reporter?: any;
}

/**
* Default configuration
*
* @todo(grabbou): As a separate PR, haste.platforms should be added before "native".
* Otherwise, a.native.js will not load on Windows or other platforms
*/
export const getDefaultConfig = (ctx: ConfigT) => {
export const getDefaultConfig = (ctx: Config): DefaultConfigOption => {
const hasteImplPath = path.join(ctx.reactNativePath, 'jest/hasteImpl.js');
return {
resolver: {
Expand All @@ -64,7 +89,7 @@ export const getDefaultConfig = (ctx: ConfigT) => {
port: Number(process.env.RCT_METRO_PORT) || 8081,
},
symbolicator: {
customizeFrame: (frame: {+file: ?string}) => {
customizeFrame: (frame: {file: string | null}) => {
const collapse = Boolean(
frame.file && INTERNAL_CALLSITES_REGEX.test(frame.file),
);
Expand All @@ -84,23 +109,23 @@ export const getDefaultConfig = (ctx: ConfigT) => {
};
};

export type ConfigOptionsT = {|
maxWorkers?: number,
port?: number,
projectRoot?: string,
resetCache?: boolean,
watchFolders?: string[],
sourceExts?: string[],
reporter?: any,
config?: string,
|};
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: ConfigT, options?: ConfigOptionsT) {
export default function load(ctx: Config, options?: ConfigOptionsT) {
const defaultConfig = getDefaultConfig(ctx);
if (options && options.reporter) {
/**
Expand Down