From 2f78d19df2a7f50e17252bfb9af10fa61592ac98 Mon Sep 17 00:00:00 2001 From: Christoph Nakazawa Date: Fri, 15 Sep 2017 04:55:29 -0700 Subject: [PATCH] Use the Config type from Metro Reviewed By: davidaurelio Differential Revision: D5832633 fbshipit-source-id: 8e4e0e6a2907e485a5627c87161af2d0672b4f56 --- local-cli/bundle/buildBundle.js | 2 +- local-cli/core/index.js | 4 +- local-cli/server/runServer.js | 2 +- local-cli/server/server.js | 2 +- local-cli/util/Config.js | 183 ++++---------------------------- package.json | 2 +- 6 files changed, 25 insertions(+), 170 deletions(-) diff --git a/local-cli/bundle/buildBundle.js b/local-cli/bundle/buildBundle.js index 8974bb93c13..07f09cde1a0 100644 --- a/local-cli/bundle/buildBundle.js +++ b/local-cli/bundle/buildBundle.js @@ -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( diff --git a/local-cli/core/index.js b/local-cli/core/index.js index aab8fc89478..4f4dfb917d6 100644 --- a/local-cli/core/index.js +++ b/local-cli/core/index.js @@ -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}; diff --git a/local-cli/server/runServer.js b/local-cli/server/runServer.js index b8c46b4c23a..9167b43e02b 100644 --- a/local-cli/server/runServer.js +++ b/local-cli/server/runServer.js @@ -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. */ diff --git a/local-cli/server/server.js b/local-cli/server/server.js index f70652fa359..f0fc12f119d 100644 --- a/local-cli/server/server.js +++ b/local-cli/server/server.js @@ -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'; /** diff --git a/local-cli/util/Config.js b/local-cli/util/Config.js index ecde7953eb9..5feea3d7a64 100644 --- a/local-cli/util/Config.js +++ b/local-cli/util/Config.js @@ -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, - - /** - * 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, - - /** - * 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, - - getProjectRoots(): Array, - - /** - * Specify any additional node modules that should be processed for - * providesModule declarations. - */ - getProvidesModuleNodeModules?: () => Array, - - /** - * 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, - - /** - * 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, - - /** - * 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); }, }; diff --git a/package.json b/package.json index 06091392bda..d6dba4359ac 100644 --- a/package.json +++ b/package.json @@ -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",