Use the Config type from Metro

Reviewed By: davidaurelio

Differential Revision: D5832633

fbshipit-source-id: 8e4e0e6a2907e485a5627c87161af2d0672b4f56
This commit is contained in:
Christoph Nakazawa 2017-09-15 04:55:29 -07:00 committed by Facebook Github Bot
parent 327a0dd84e
commit 2f78d19df2
6 changed files with 25 additions and 170 deletions

View File

@ -46,7 +46,7 @@ const defaultProvidesModuleNodeModules = require('metro-bundler/src/defaults').p
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
import type {RequestOptions, OutputOptions} from './types.flow';
import type {ConfigT} from '../util/Config';
import type {ConfigT} from 'metro-bundler';
function saveBundle(output, bundle, args) {
return Promise.resolve(

View File

@ -29,7 +29,7 @@ const minimist = require('minimist');
const path = require('path');
import type {CommandT} from '../commands';
import type {ConfigT} from '../util/Config';
import type {ConfigT} from 'metro-bundler';
export type RNConfig = {
...ConfigT,
@ -108,7 +108,7 @@ const defaultRNConfig = {
function getCliConfig(): RNConfig {
const cliArgs = minimist(process.argv.slice(2));
const config = cliArgs.config != null
? Config.loadFile(path.resolve(__dirname, cliArgs.config))
? Config.load(path.resolve(__dirname, cliArgs.config))
: Config.findOptional(__dirname);
return {...defaultRNConfig, ...config};

View File

@ -56,7 +56,7 @@ const TransformCaching = require('metro-bundler/src/lib/TransformCaching');
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
import type {ConfigT} from '../util/Config';
import type {ConfigT} from 'metro-bundler';
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */

View File

@ -15,7 +15,7 @@ const path = require('path');
const runServer = require('./runServer');
import type {RNConfig} from '../core';
import type {ConfigT} from '../util/Config';
import type {ConfigT} from 'metro-bundler';
import type {Args as RunServerArgs} from './runServer';
/**

View File

@ -10,151 +10,22 @@
*/
'use strict';
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
const blacklist = require('metro-bundler/src/blacklist');
const findSymlinkedModules = require('./findSymlinkedModules');
const fs = require('fs');
const getPolyfills = require('../../rn-get-polyfills');
const invariant = require('fbjs/lib/invariant');
const path = require('path');
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
const {providesModuleNodeModules} = require('metro-bundler/src/defaults');
const {Config: MetroConfig} = require('metro-bundler');
const RN_CLI_CONFIG = 'rn-cli.config.js';
import type {
GetTransformOptions,
PostMinifyProcess,
PostProcessModules,
PostProcessBundleSourcemap
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
} from 'metro-bundler/src/Bundler';
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
import type {HasteImpl} from 'metro-bundler/src/node-haste/Module';
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
import type {TransformVariants} from 'metro-bundler/src/ModuleGraph/types.flow';
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
import type {PostProcessModules as PostProcessModulesForBuck} from 'metro-bundler/src/ModuleGraph/types.flow.js';
import type {ConfigT as MetroConfigT} from 'metro-bundler';
/**
* Configuration file of the CLI.
*/
export type ConfigT = {
extraNodeModules: {[id: string]: string},
/**
* Specify any additional asset file extensions to be used by the packager.
* For example, if you want to include a .ttf file, you would return ['ttf']
* from here and use `require('./fonts/example.ttf')` inside your app.
*/
getAssetExts: () => Array<string>,
/**
* Returns a regular expression for modules that should be ignored by the
* packager on a given platform.
*/
getBlacklistRE(): RegExp,
/**
* Specify whether or not to enable Babel's behavior for looking up .babelrc
* files. If false, only the .babelrc file (if one exists) in the main project
* root is used.
*/
getEnableBabelRCLookup(): boolean,
/**
* Specify any additional polyfill modules that should be processed
* before regular module loading.
*/
getPolyfillModuleNames: () => Array<string>,
/**
* Specify any additional platforms to be used by the packager.
* For example, if you want to add a "custom" platform, and use modules
* ending in .custom.js, you would return ['custom'] here.
*/
getPlatforms: () => Array<string>,
getProjectRoots(): Array<string>,
/**
* Specify any additional node modules that should be processed for
* providesModule declarations.
*/
getProvidesModuleNodeModules?: () => Array<string>,
/**
* Specify any additional source file extensions to be used by the packager.
* For example, if you want to include a .ts file, you would return ['ts']
* from here and use `require('./module/example')` to require the file with
* path 'module/example.ts' inside your app.
*/
getSourceExts: () => Array<string>,
/**
* Returns the path to a custom transformer. This can also be overridden
* with the --transformer commandline argument.
*/
getTransformModulePath: () => string,
getTransformOptions: GetTransformOptions,
/**
* Returns the path to the worker that is used for transformation.
*/
getWorkerPath: () => ?string,
/**
* An optional list of polyfills to include in the bundle. The list defaults
* to a set of common polyfills for Number, String, Array, Object...
*/
getPolyfills: ({platform: ?string}) => $ReadOnlyArray<string>,
/**
* An optional function that can modify the code and source map of bundle
* after the minifaction took place. (Function applied per module).
*/
postMinifyProcess: PostMinifyProcess,
/**
* An optional function that can modify the module array before the bundle is
* finalized.
*/
postProcessModules: PostProcessModules,
/**
* An optional function that can modify the code and source map of the bundle
* before it is written. Applied once for the entire bundle, only works if
* output is a plainBundle.
*/
postProcessBundleSourcemap: PostProcessBundleSourcemap,
/**
* Same as `postProcessModules` but for the Buck worker. Eventually we do want
* to unify both variants.
*/
postProcessModulesForBuck: PostProcessModulesForBuck,
/**
* A module that exports:
* - a `getHasteName(filePath)` method that returns `hasteName` for module at
* `filePath`, or undefined if `filePath` is not a haste module.
*/
hasteImpl?: HasteImpl,
transformVariants: () => TransformVariants,
};
export type ConfigT = MetroConfigT;
function getProjectPath() {
if (__dirname.match(/node_modules[\/\\]react-native[\/\\]local-cli[\/\\]util$/)) {
@ -176,41 +47,27 @@ const resolveSymlinksForRoots = roots =>
[...roots]
);
const getProjectRoots = () => {
const root = process.env.REACT_NATIVE_APP_ROOT;
if (root) {
return resolveSymlinksForRoots([path.resolve(root)]);
}
return resolveSymlinksForRoots([getProjectPath()]);
};
/**
* Module capable of getting the configuration out of a given file.
*
* The function will return all the default configuration, as specified by the
* `DEFAULTS` param overriden by those found on `rn-cli.config.js` files, if any. If no
* `DEFAULT` param overriden by those found on `rn-cli.config.js` files, if any. If no
* default config is provided and no configuration can be found in the directory
* hierarchy, an error will be thrown.
*/
const Config = {
DEFAULTS: ({
extraNodeModules: Object.create(null),
getAssetExts: () => [],
getBlacklistRE: () => blacklist(),
getEnableBabelRCLookup: () => false,
getPlatforms: () => [],
getPolyfillModuleNames: () => [],
getProjectRoots: () => {
const root = process.env.REACT_NATIVE_APP_ROOT;
if (root) {
return resolveSymlinksForRoots([path.resolve(root)]);
}
return resolveSymlinksForRoots([getProjectPath()]);
},
getProvidesModuleNodeModules: () => providesModuleNodeModules.slice(),
getSourceExts: () => [],
getTransformModulePath: () => require.resolve('metro-bundler/src/transformer.js'),
getTransformOptions: async () => ({}),
DEFAULT: ({
...MetroConfig.DEFAULT,
getProjectRoots,
getPolyfills,
postMinifyProcess: x => x,
postProcessModules: modules => modules,
postProcessModulesForBuck: modules => modules,
postProcessBundleSourcemap: ({code, map, outFileName}) => ({code, map}),
transformVariants: () => ({default: {}}),
getWorkerPath: () => null,
}: ConfigT),
find(startDir: string): ConfigT {
@ -224,20 +81,18 @@ const Config = {
`Can't find "${RN_CLI_CONFIG}" file in any parent folder of "${startDir}"`,
);
const projectPath = path.dirname(configPath);
return {config: this.loadFile(configPath, startDir), projectPath};
return {config: this.load(configPath, startDir), projectPath};
},
findOptional(startDir: string): ConfigT {
const configPath = findConfigPath(startDir);
return configPath
? this.loadFile(configPath, startDir)
: {...Config.DEFAULTS};
? this.load(configPath, startDir)
: {...Config.DEFAULT};
},
loadFile(pathToConfig: string): ConfigT {
//$FlowFixMe: necessary dynamic require
const config: {} = require(pathToConfig);
return {...Config.DEFAULTS, ...config};
load(configFile: string): ConfigT {
return MetroConfig.load(configFile, Config.DEFAULT);
},
};

View File

@ -162,7 +162,7 @@
"graceful-fs": "^4.1.3",
"inquirer": "^3.0.6",
"lodash": "^4.16.6",
"metro-bundler": "^0.17.0",
"metro-bundler": "^0.18.0",
"mime": "^1.3.4",
"minimist": "^1.2.0",
"mkdirp": "^0.5.1",