Skip to content

Commit

Permalink
Convert internal react-native-cli
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Ives van Hoorne authored and facebook-github-bot committed Jul 25, 2018
1 parent c3d31cd commit f0daaf3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 88 deletions.
66 changes: 13 additions & 53 deletions local-cli/bundle/buildBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 & {
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions local-cli/bundle/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
29 changes: 8 additions & 21 deletions local-cli/dependencies/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,30 @@
'use strict';

const Metro = require('metro');
const {convert} = require('metro-config');

const denodeify = require('denodeify');
const fs = require('fs');
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,
Expand Down
28 changes: 16 additions & 12 deletions local-cli/server/runServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f0daaf3

Please sign in to comment.