Add Config object and loading mechanism

Summary:
This copies the basic loading mechanism and default config from the RN local cli into Metro and exposes it under `metro-bundler`, and switches internal scripts over to it.

davidaurelio: I changed the packager-worker-for-buck to hardcode the rn.cli file, like we do in other files. I would like to pull the "find" mechanism that traverses up to find a config into Metro at some point, but for now I'd prefer to keep it lean until we need it. Let me know if that doesn't work for you.

The next diff will switch the RN cli over to these functions also.

Reviewed By: davidaurelio

Differential Revision: D5832596

fbshipit-source-id: a3e167644d96c8831e5a83378e8ba143e62426db
This commit is contained in:
Christoph Nakazawa 2017-09-15 01:35:57 -07:00 committed by Facebook Github Bot
parent 37a7a3b018
commit 34b1fb6abf
2 changed files with 53 additions and 3 deletions

View File

@ -11,15 +11,20 @@
*/
'use strict';
const blacklist = require('./blacklist');
const path = require('path');
const {providesModuleNodeModules} = require('./defaults');
import type {
GetTransformOptions,
PostMinifyProcess,
PostProcessModules,
PostProcessBundleSourcemap,
} from './Bundler';
import type {HasteImpl} from './node-haste/Module';
import type {TransformVariants} from './ModuleGraph/types.flow';
import type {PostProcessModules as PostProcessModulesForBuck} from './ModuleGraph/types.flow.js';
import type {TransformVariants} from './ModuleGraph/types.flow';
import type {HasteImpl} from './node-haste/Module';
export type ConfigT = {
extraNodeModules: {[id: string]: string},
@ -124,3 +129,46 @@ export type ConfigT = {
transformVariants: () => TransformVariants,
};
const DEFAULT = ({
extraNodeModules: Object.create(null),
getAssetExts: () => [],
getBlacklistRE: () => blacklist(),
getEnableBabelRCLookup: () => false,
getPlatforms: () => [],
getPolyfillModuleNames: () => [],
getProjectRoots: () => {
// We assume the default project path is two levels up from
// node_modules/metro-bundler/
return [path.resolve(__dirname, '../..')];
},
getProvidesModuleNodeModules: () => providesModuleNodeModules.slice(),
getSourceExts: () => [],
getTransformModulePath: () =>
require.resolve('metro-bundler/src/transformer.js'),
getTransformOptions: async () => ({}),
getPolyfills: () => [],
postMinifyProcess: x => x,
postProcessModules: modules => modules,
postProcessModulesForBuck: modules => modules,
postProcessBundleSourcemap: ({code, map, outFileName}) => ({code, map}),
transformVariants: () => ({default: {}}),
getWorkerPath: () => null,
}: ConfigT);
const normalize = (initialConfig: ConfigT, defaults?: ConfigT): ConfigT => {
return {
...(defaults || DEFAULT),
...initialConfig,
};
};
const load = (configFile: string, defaults?: ConfigT) =>
// $FlowFixMe dynamic require
normalize(require(configFile), defaults);
module.exports = {
DEFAULT,
load,
normalize,
};

View File

@ -12,6 +12,7 @@
'use strict';
const Config = require('./Config');
const Logger = require('./Logger');
const TransformCaching = require('./lib/TransformCaching');
@ -21,13 +22,14 @@ const invariant = require('fbjs/lib/invariant');
const {fromRawMappings, compactMapping} = require('./Bundler/source-map');
import type {ConfigT as MetroConfig} from './Config';
import type Server, {Options as ServerOptions} from './Server';
import type {TransformCache} from './lib/TransformCaching';
import type {ConfigT as MetroConfig} from './Config';
exports.createBlacklist = blacklist;
exports.sourceMaps = {fromRawMappings, compactMapping};
exports.createServer = createServer;
exports.Config = Config;
exports.Logger = Logger;
export type ConfigT = MetroConfig;