From f0daaf3568511c8e22c3ac75ec7fc7c6ffd22a7c Mon Sep 17 00:00:00 2001 From: Ives van Hoorne Date: Wed, 25 Jul 2018 05:44:38 -0700 Subject: [PATCH] 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 --- local-cli/bundle/buildBundle.js | 66 +++++--------------------- local-cli/bundle/bundle.js | 4 +- local-cli/dependencies/dependencies.js | 29 ++++------- local-cli/server/runServer.js | 28 ++++++----- 4 files changed, 39 insertions(+), 88 deletions(-) diff --git a/local-cli/bundle/buildBundle.js b/local-cli/bundle/buildBundle.js index 66d86462171df4..88c036ca49c6b1 100644 --- a/local-cli/bundle/buildBundle.js +++ b/local-cli/bundle/buildBundle.js @@ -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, 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(); - - const providesModuleNodeModules = - typeof config.getProvidesModuleNodeModules === 'function' - ? config.getProvidesModuleNodeModules() - : defaultProvidesModuleNodeModules; - - 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(), - }); + : config.transformModulePath; + + config.transformModulePath = transformModulePath; + config.transformer.assetRegistryPath = ASSET_REGISTRY_PATH; + + const {serverOptions} = convert.convertNewToOld(config); + + const server = new Server(serverOptions); try { const bundle = await output.build(server, requestOpts); diff --git a/local-cli/bundle/bundle.js b/local-cli/bundle/bundle.js index 96d995db46cdc5..c6a2b07ac2a11d 100644 --- a/local-cli/bundle/bundle.js +++ b/local-cli/bundle/bundle.js @@ -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 = { diff --git a/local-cli/dependencies/dependencies.js b/local-cli/dependencies/dependencies.js index baa6ec4a7a4da4..32b0bf3146e16b 100644 --- a/local-cli/dependencies/dependencies.js +++ b/local-cli/dependencies/dependencies.js @@ -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, diff --git a/local-cli/server/runServer.js b/local-cli/server/runServer.js index c7ea9c64a3d339..e8d741fa63ede8 100644 --- a/local-cli/server/runServer.js +++ b/local-cli/server/runServer.js @@ -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,