mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-28 20:25:33 +08:00
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:
committed by
Facebook Github Bot
parent
c3d31cd1de
commit
f0daaf3568
@@ -13,10 +13,9 @@
|
|||||||
const log = require('../util/log').out('bundle');
|
const log = require('../util/log').out('bundle');
|
||||||
/* $FlowFixMe(site=react_native_oss) */
|
/* $FlowFixMe(site=react_native_oss) */
|
||||||
const Server = require('metro/src/Server');
|
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) */
|
/* $FlowFixMe(site=react_native_oss) */
|
||||||
const outputBundle = require('metro/src/shared/output/bundle');
|
const outputBundle = require('metro/src/shared/output/bundle');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
@@ -25,12 +24,7 @@ const saveAssets = require('./saveAssets');
|
|||||||
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
|
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
|
||||||
|
|
||||||
import type {RequestOptions, OutputOptions} from './types.flow';
|
import type {RequestOptions, OutputOptions} from './types.flow';
|
||||||
import type {ConfigT} from 'metro';
|
import type {ConfigT} from 'metro-config/src/configTypes.flow';
|
||||||
|
|
||||||
const defaultAssetExts = defaults.assetExts;
|
|
||||||
const defaultSourceExts = defaults.sourceExts;
|
|
||||||
const defaultPlatforms = defaults.platforms;
|
|
||||||
const defaultProvidesModuleNodeModules = defaults.providesModuleNodeModules;
|
|
||||||
|
|
||||||
async function buildBundle(
|
async function buildBundle(
|
||||||
args: OutputOptions & {
|
args: OutputOptions & {
|
||||||
@@ -41,12 +35,13 @@ async function buildBundle(
|
|||||||
transformer: string,
|
transformer: string,
|
||||||
minify: boolean,
|
minify: boolean,
|
||||||
},
|
},
|
||||||
config: ConfigT,
|
configPromise: Promise<ConfigT>,
|
||||||
output = outputBundle,
|
output = outputBundle,
|
||||||
) {
|
) {
|
||||||
// This is used by a bazillion of npm modules we don't control so we don't
|
// 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.
|
// have other choice than defining it as an env variable here.
|
||||||
process.env.NODE_ENV = args.dev ? 'development' : 'production';
|
process.env.NODE_ENV = args.dev ? 'development' : 'production';
|
||||||
|
const config = await configPromise;
|
||||||
|
|
||||||
let sourceMapUrl = args.sourcemapOutput;
|
let sourceMapUrl = args.sourcemapOutput;
|
||||||
if (sourceMapUrl && !args.sourcemapUseAbsolutePath) {
|
if (sourceMapUrl && !args.sourcemapUseAbsolutePath) {
|
||||||
@@ -61,51 +56,16 @@ async function buildBundle(
|
|||||||
platform: args.platform,
|
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
|
const transformModulePath = args.transformer
|
||||||
? path.resolve(args.transformer)
|
? path.resolve(args.transformer)
|
||||||
: config.getTransformModulePath();
|
: config.transformModulePath;
|
||||||
|
|
||||||
const providesModuleNodeModules =
|
config.transformModulePath = transformModulePath;
|
||||||
typeof config.getProvidesModuleNodeModules === 'function'
|
config.transformer.assetRegistryPath = ASSET_REGISTRY_PATH;
|
||||||
? config.getProvidesModuleNodeModules()
|
|
||||||
: defaultProvidesModuleNodeModules;
|
|
||||||
|
|
||||||
const terminal = new Terminal(process.stdout);
|
const {serverOptions} = convert.convertNewToOld(config);
|
||||||
const server = new Server({
|
|
||||||
asyncRequireModulePath: config.getAsyncRequireModulePath(),
|
const server = new Server(serverOptions);
|
||||||
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(),
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const bundle = await output.build(server, requestOpts);
|
const bundle = await output.build(server, requestOpts);
|
||||||
|
|||||||
@@ -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.
|
* 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) {
|
if (!output) {
|
||||||
output = outputBundle;
|
output = outputBundle;
|
||||||
}
|
}
|
||||||
return buildBundle(args, config, output);
|
return buildBundle(args, configPromise, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const Metro = require('metro');
|
const Metro = require('metro');
|
||||||
|
const {convert} = require('metro-config');
|
||||||
|
|
||||||
const denodeify = require('denodeify');
|
const denodeify = require('denodeify');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
@@ -17,36 +18,22 @@ const path = require('path');
|
|||||||
|
|
||||||
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
|
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 rootModuleAbsolutePath = args.entryFile;
|
||||||
|
const config = await configPromise;
|
||||||
if (!fs.existsSync(rootModuleAbsolutePath)) {
|
if (!fs.existsSync(rootModuleAbsolutePath)) {
|
||||||
return Promise.reject(
|
return Promise.reject(
|
||||||
new Error(`File ${rootModuleAbsolutePath} does not exist`),
|
new Error(`File ${rootModuleAbsolutePath} does not exist`),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const transformModulePath = args.transformer
|
config.cacheStores = [];
|
||||||
|
config.transformModulePath = args.transformer
|
||||||
? path.resolve(args.transformer)
|
? path.resolve(args.transformer)
|
||||||
: typeof config.getTransformModulePath === 'function'
|
: config.transformModulePath;
|
||||||
? config.getTransformModulePath()
|
config.transformer.transformModulePath = ASSET_REGISTRY_PATH;
|
||||||
: undefined;
|
|
||||||
|
|
||||||
const packageOpts = {
|
const {serverOptions: packageOpts} = convert.convertNewToOld(config);
|
||||||
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 relativePath = path.relative(
|
const relativePath = path.relative(
|
||||||
packageOpts.projectRoot,
|
packageOpts.projectRoot,
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ const morgan = require('morgan');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const webSocketProxy = require('./util/webSocketProxy');
|
const webSocketProxy = require('./util/webSocketProxy');
|
||||||
const MiddlewareManager = require('./middleware/MiddlewareManager');
|
const MiddlewareManager = require('./middleware/MiddlewareManager');
|
||||||
|
const {convertOldToNew} = require('metro-config/src/convertConfig');
|
||||||
|
|
||||||
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
|
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
|
||||||
|
|
||||||
@@ -57,20 +58,23 @@ async function runServer(args: Args, config: ConfigT) {
|
|||||||
args.watchFolders.forEach(middlewareManager.serveStatic);
|
args.watchFolders.forEach(middlewareManager.serveStatic);
|
||||||
|
|
||||||
const serverInstance = await Metro.runServer({
|
const serverInstance = await Metro.runServer({
|
||||||
config: {
|
config: convertOldToNew({
|
||||||
...config,
|
config: {
|
||||||
assetRegistryPath: ASSET_REGISTRY_PATH,
|
...config,
|
||||||
enhanceMiddleware: middleware =>
|
assetRegistryPath: ASSET_REGISTRY_PATH,
|
||||||
middlewareManager.getConnectInstance().use(middleware),
|
enhanceMiddleware: middleware =>
|
||||||
transformModulePath: args.transformer
|
middlewareManager.getConnectInstance().use(middleware),
|
||||||
? path.resolve(args.transformer)
|
transformModulePath: args.transformer
|
||||||
: config.getTransformModulePath(),
|
? path.resolve(args.transformer)
|
||||||
},
|
: config.getTransformModulePath(),
|
||||||
|
},
|
||||||
|
maxWorkers: args.maxWorkers,
|
||||||
|
port: args.port,
|
||||||
|
reporter,
|
||||||
|
}),
|
||||||
|
|
||||||
hmrEnabled: true,
|
hmrEnabled: true,
|
||||||
host: args.host,
|
host: args.host,
|
||||||
maxWorkers: args.maxWorkers,
|
|
||||||
port: args.port,
|
|
||||||
reporter,
|
|
||||||
secure: args.https,
|
secure: args.https,
|
||||||
secureCert: args.cert,
|
secureCert: args.cert,
|
||||||
secureKey: args.key,
|
secureKey: args.key,
|
||||||
|
|||||||
Reference in New Issue
Block a user