Convert internal react-native-cli

Summary: In this diff I change the internal react-native cli to use our new configuration. Another change here is that Metro starts using the new configuration already as its entry points (like `metro#runMetro`).

Reviewed By: rafeca

Differential Revision: D8728612

fbshipit-source-id: 9f43dee31ebaccd35cf6274d5c4dec0a227a6eec
This commit is contained in:
Ives van Hoorne 2018-07-25 05:44:38 -07:00 committed by Facebook Github Bot
parent c3d31cd1de
commit f0daaf3568
4 changed files with 37 additions and 86 deletions

View File

@ -13,10 +13,9 @@
const log = require('../util/log').out('bundle');
/* $FlowFixMe(site=react_native_oss) */
const Server = require('metro/src/Server');
const {Terminal} = require('metro-core');
const TerminalReporter = require('metro/src/lib/TerminalReporter');
const {defaults} = require('metro');
const {convert} = require('metro-config');
/* $FlowFixMe(site=react_native_oss) */
const outputBundle = require('metro/src/shared/output/bundle');
const path = require('path');
@ -25,12 +24,7 @@ const saveAssets = require('./saveAssets');
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
import type {RequestOptions, OutputOptions} from './types.flow';
import type {ConfigT} from 'metro';
const defaultAssetExts = defaults.assetExts;
const defaultSourceExts = defaults.sourceExts;
const defaultPlatforms = defaults.platforms;
const defaultProvidesModuleNodeModules = defaults.providesModuleNodeModules;
import type {ConfigT} from 'metro-config/src/configTypes.flow';
async function buildBundle(
args: OutputOptions & {
@ -41,12 +35,13 @@ async function buildBundle(
transformer: string,
minify: boolean,
},
config: ConfigT,
configPromise: Promise<ConfigT>,
output = outputBundle,
) {
// This is used by a bazillion of npm modules we don't control so we don't
// have other choice than defining it as an env variable here.
process.env.NODE_ENV = args.dev ? 'development' : 'production';
const config = await configPromise;
let sourceMapUrl = args.sourcemapOutput;
if (sourceMapUrl && !args.sourcemapUseAbsolutePath) {
@ -61,51 +56,16 @@ async function buildBundle(
platform: args.platform,
};
const assetExts = (config.getAssetExts && config.getAssetExts()) || [];
const sourceExts = (config.getSourceExts && config.getSourceExts()) || [];
const platforms = (config.getPlatforms && config.getPlatforms()) || [];
const transformModulePath = args.transformer
? path.resolve(args.transformer)
: config.getTransformModulePath();
: config.transformModulePath;
const providesModuleNodeModules =
typeof config.getProvidesModuleNodeModules === 'function'
? config.getProvidesModuleNodeModules()
: defaultProvidesModuleNodeModules;
config.transformModulePath = transformModulePath;
config.transformer.assetRegistryPath = ASSET_REGISTRY_PATH;
const terminal = new Terminal(process.stdout);
const server = new Server({
asyncRequireModulePath: config.getAsyncRequireModulePath(),
assetExts: defaultAssetExts.concat(assetExts),
assetRegistryPath: ASSET_REGISTRY_PATH,
blacklistRE: config.getBlacklistRE(),
cacheStores: config.cacheStores,
cacheVersion: config.cacheVersion,
dynamicDepsInPackages: config.dynamicDepsInPackages,
enableBabelRCLookup: config.getEnableBabelRCLookup(),
extraNodeModules: config.extraNodeModules,
getModulesRunBeforeMainModule: config.getModulesRunBeforeMainModule,
getPolyfills: config.getPolyfills,
getResolverMainFields: config.getResolverMainFields,
getRunModuleStatement: config.getRunModuleStatement,
getTransformOptions: config.getTransformOptions,
hasteImplModulePath: config.hasteImplModulePath,
maxWorkers: args.maxWorkers,
platforms: defaultPlatforms.concat(platforms),
postMinifyProcess: config.postMinifyProcess,
postProcessBundleSourcemap: config.postProcessBundleSourcemap,
projectRoot: config.getProjectRoot(),
providesModuleNodeModules: providesModuleNodeModules,
reporter: new TerminalReporter(terminal),
resetCache: args.resetCache,
resolveRequest: config.resolveRequest,
sourceExts: sourceExts.concat(defaultSourceExts),
transformModulePath: transformModulePath,
watch: false,
watchFolders: config.getWatchFolders(),
workerPath: config.getWorkerPath && config.getWorkerPath(),
});
const {serverOptions} = convert.convertNewToOld(config);
const server = new Server(serverOptions);
try {
const bundle = await output.build(server, requestOpts);

View File

@ -16,11 +16,11 @@ const outputBundle = require('metro/src/shared/output/bundle');
/**
* Builds the bundle starting to look for dependencies at the given entry path.
*/
function bundleWithOutput(argv, config, args, output) {
function bundleWithOutput(argv, configPromise, args, output) {
if (!output) {
output = outputBundle;
}
return buildBundle(args, config, output);
return buildBundle(args, configPromise, output);
}
module.exports = {

View File

@ -10,6 +10,7 @@
'use strict';
const Metro = require('metro');
const {convert} = require('metro-config');
const denodeify = require('denodeify');
const fs = require('fs');
@ -17,36 +18,22 @@ const path = require('path');
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
function dependencies(argv, config, args, packagerInstance) {
async function dependencies(argv, configPromise, args, packagerInstance) {
const rootModuleAbsolutePath = args.entryFile;
const config = await configPromise;
if (!fs.existsSync(rootModuleAbsolutePath)) {
return Promise.reject(
new Error(`File ${rootModuleAbsolutePath} does not exist`),
);
}
const transformModulePath = args.transformer
config.cacheStores = [];
config.transformModulePath = args.transformer
? path.resolve(args.transformer)
: typeof config.getTransformModulePath === 'function'
? config.getTransformModulePath()
: undefined;
: config.transformModulePath;
config.transformer.transformModulePath = ASSET_REGISTRY_PATH;
const packageOpts = {
assetRegistryPath: ASSET_REGISTRY_PATH,
cacheStores: [],
projectRoot: config.getProjectRoot(),
blacklistRE: config.getBlacklistRE(),
dynamicDepsInPackages: config.dynamicDepsInPackages,
getPolyfills: config.getPolyfills,
getTransformOptions: config.getTransformOptions,
hasteImplModulePath: config.hasteImplModulePath,
postMinifyProcess: config.postMinifyProcess,
transformModulePath: transformModulePath,
extraNodeModules: config.extraNodeModules,
verbose: config.verbose,
watchFolders: config.getWatchFolders(),
workerPath: config.getWorkerPath(),
};
const {serverOptions: packageOpts} = convert.convertNewToOld(config);
const relativePath = path.relative(
packageOpts.projectRoot,

View File

@ -21,6 +21,7 @@ const morgan = require('morgan');
const path = require('path');
const webSocketProxy = require('./util/webSocketProxy');
const MiddlewareManager = require('./middleware/MiddlewareManager');
const {convertOldToNew} = require('metro-config/src/convertConfig');
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
@ -57,20 +58,23 @@ async function runServer(args: Args, config: ConfigT) {
args.watchFolders.forEach(middlewareManager.serveStatic);
const serverInstance = await Metro.runServer({
config: {
...config,
assetRegistryPath: ASSET_REGISTRY_PATH,
enhanceMiddleware: middleware =>
middlewareManager.getConnectInstance().use(middleware),
transformModulePath: args.transformer
? path.resolve(args.transformer)
: config.getTransformModulePath(),
},
config: convertOldToNew({
config: {
...config,
assetRegistryPath: ASSET_REGISTRY_PATH,
enhanceMiddleware: middleware =>
middlewareManager.getConnectInstance().use(middleware),
transformModulePath: args.transformer
? path.resolve(args.transformer)
: config.getTransformModulePath(),
},
maxWorkers: args.maxWorkers,
port: args.port,
reporter,
}),
hmrEnabled: true,
host: args.host,
maxWorkers: args.maxWorkers,
port: args.port,
reporter,
secure: args.https,
secureCert: args.cert,
secureKey: args.key,