Remove setupBabel call from main entry point

Summary:
* Internally, we already set up babel before calling into metro-bundler.
* Externally, I moved the setup calls to outside the packager/ folder.

If somebody has a custom integration with RN, they would need to setup babel themselves up until we make the open source split. By the time this is released in open source, an npm version of metro-bundler will be available for use.

Next step: also do this for the worker and remove setupBabel from the metro repo.

Reviewed By: davidaurelio

Differential Revision: D5121429

fbshipit-source-id: e77c6ccc23bef1d13fd74c4727be2d7e09d2d0ca
This commit is contained in:
Christoph Pojer 2017-05-24 11:38:10 -07:00 committed by Facebook Github Bot
parent a3d58ba570
commit 17e0478cf4
6 changed files with 131 additions and 144 deletions

View File

@ -8,7 +8,8 @@
*/
'use strict';
const ReactPackager = require('../../packager/react-packager');
require('../../setupBabel')();
const ReactPackager = require('../../packager');
const denodeify = require('denodeify');
const fs = require('fs');

View File

@ -12,8 +12,9 @@
'use strict';
require('../../setupBabel')();
const InspectorProxy = require('./util/inspectorProxy.js');
const ReactPackager = require('../../packager/react-packager');
const ReactPackager = require('../../packager');
const attachHMRServer = require('./util/attachHMRServer');
const connect = require('connect');

View File

@ -87,7 +87,7 @@ The packager is made of two things:
ReactPackager is how you mainly interact with the API.
```js
var ReactPackager = require('./react-packager');
var ReactPackager = require('path/to/packager/');
```
### ReactPackager.buildBundle(serverOptions, bundleOptions)

View File

@ -11,5 +11,128 @@
'use strict';
require('../setupBabel')();
module.exports = require('./react-packager');
const Logger = require('./src/Logger');
const debug = require('debug');
const invariant = require('fbjs/lib/invariant');
import type {PostProcessModules, PostMinifyProcess} from './src/Bundler';
import type Server from './src/Server';
import type {GlobalTransformCache} from './src/lib/GlobalTransformCache';
import type {Reporter} from './src/lib/reporting';
import type {HasteImpl} from './src/node-haste/Module';
exports.createServer = createServer;
exports.Logger = Logger;
type Options = {
hasteImpl?: HasteImpl,
globalTransformCache: ?GlobalTransformCache,
nonPersistent?: boolean,
postProcessModules?: PostProcessModules,
postMinifyProcess?: PostMinifyProcess,
projectRoots: $ReadOnlyArray<string>,
reporter?: Reporter,
+sourceExts: ?Array<string>,
+transformModulePath: string,
watch?: boolean,
};
type StrictOptions = {...Options, reporter: Reporter};
type PublicBundleOptions = {
+dev?: boolean,
+entryFile: string,
+generateSourceMaps?: boolean,
+inlineSourceMap?: boolean,
+minify?: boolean,
+platform?: string,
+runModule?: boolean,
+sourceMapUrl?: string,
};
/**
* This is a public API, so we don't trust the value and purposefully downgrade
* it as `mixed`. Because it understands `invariant`, Flow ensure that we
* refine these values completely.
*/
function assertPublicBundleOptions(bo: mixed): PublicBundleOptions {
invariant(typeof bo === 'object' && bo != null, 'bundle options must be an object');
invariant(bo.dev === undefined || typeof bo.dev === 'boolean', 'bundle options field `dev` must be a boolean');
const {entryFile} = bo;
invariant(typeof entryFile === 'string', 'bundle options must contain a string field `entryFile`');
invariant(bo.generateSourceMaps === undefined || typeof bo.generateSourceMaps === 'boolean', 'bundle options field `generateSourceMaps` must be a boolean');
invariant(bo.inlineSourceMap === undefined || typeof bo.inlineSourceMap === 'boolean', 'bundle options field `inlineSourceMap` must be a boolean');
invariant(bo.minify === undefined || typeof bo.minify === 'boolean', 'bundle options field `minify` must be a boolean');
invariant(bo.platform === undefined || typeof bo.platform === 'string', 'bundle options field `platform` must be a string');
invariant(bo.runModule === undefined || typeof bo.runModule === 'boolean', 'bundle options field `runModule` must be a boolean');
invariant(bo.sourceMapUrl === undefined || typeof bo.sourceMapUrl === 'string', 'bundle options field `sourceMapUrl` must be a boolean');
return {entryFile, ...bo};
}
exports.buildBundle = function(options: Options, bundleOptions: PublicBundleOptions) {
var server = createNonPersistentServer(options);
const ServerClass = require('./src/Server');
return server.buildBundle({
...ServerClass.DEFAULT_BUNDLE_OPTIONS,
...assertPublicBundleOptions(bundleOptions),
}).then(p => {
server.end();
return p;
});
};
exports.getOrderedDependencyPaths = function(options: Options, depOptions: {
+entryFile: string,
+dev: boolean,
+platform: string,
}) {
var server = createNonPersistentServer(options);
return server.getOrderedDependencyPaths(depOptions)
.then(function(paths) {
server.end();
return paths;
});
};
function enableDebug() {
// react-packager logs debug messages using the 'debug' npm package, and uses
// the following prefix throughout.
// To enable debugging, we need to set our pattern or append it to any
// existing pre-configured pattern to avoid disabling logging for
// other packages
var debugPattern = 'RNP:*';
var existingPattern = debug.load();
if (existingPattern) {
debugPattern += ',' + existingPattern;
}
debug.enable(debugPattern);
}
function createServer(options: StrictOptions): Server {
// the debug module is configured globally, we need to enable debugging
// *before* requiring any packages that use `debug` for logging
if (options.verbose) {
enableDebug();
}
// Some callsites may not be Flowified yet.
invariant(options.reporter != null, 'createServer() requires reporter');
const serverOptions = Object.assign({}, options);
delete serverOptions.verbose;
const ServerClass = require('./src/Server');
return new ServerClass(serverOptions);
}
function createNonPersistentServer(options: Options): Server {
const serverOptions = {
// It's unsound to set-up the reporter here,
// but this allows backward compatibility.
reporter: options.reporter == null
? require('./src/lib/reporting').nullReporter
: options.reporter,
...options,
watch: !options.nonPersistent,
};
return createServer(serverOptions);
}

View File

@ -1,138 +0,0 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
'use strict';
const Logger = require('./src/Logger');
const debug = require('debug');
const invariant = require('fbjs/lib/invariant');
import type {PostProcessModules, PostMinifyProcess} from './src/Bundler';
import type Server from './src/Server';
import type {GlobalTransformCache} from './src/lib/GlobalTransformCache';
import type {Reporter} from './src/lib/reporting';
import type {HasteImpl} from './src/node-haste/Module';
exports.createServer = createServer;
exports.Logger = Logger;
type Options = {
hasteImpl?: HasteImpl,
globalTransformCache: ?GlobalTransformCache,
nonPersistent?: boolean,
postProcessModules?: PostProcessModules,
postMinifyProcess?: PostMinifyProcess,
projectRoots: $ReadOnlyArray<string>,
reporter?: Reporter,
+sourceExts: ?Array<string>,
+transformModulePath: string,
watch?: boolean,
};
type StrictOptions = {...Options, reporter: Reporter};
type PublicBundleOptions = {
+dev?: boolean,
+entryFile: string,
+generateSourceMaps?: boolean,
+inlineSourceMap?: boolean,
+minify?: boolean,
+platform?: string,
+runModule?: boolean,
+sourceMapUrl?: string,
};
/**
* This is a public API, so we don't trust the value and purposefully downgrade
* it as `mixed`. Because it understands `invariant`, Flow ensure that we
* refine these values completely.
*/
function assertPublicBundleOptions(bo: mixed): PublicBundleOptions {
invariant(typeof bo === 'object' && bo != null, 'bundle options must be an object');
invariant(bo.dev === undefined || typeof bo.dev === 'boolean', 'bundle options field `dev` must be a boolean');
const {entryFile} = bo;
invariant(typeof entryFile === 'string', 'bundle options must contain a string field `entryFile`');
invariant(bo.generateSourceMaps === undefined || typeof bo.generateSourceMaps === 'boolean', 'bundle options field `generateSourceMaps` must be a boolean');
invariant(bo.inlineSourceMap === undefined || typeof bo.inlineSourceMap === 'boolean', 'bundle options field `inlineSourceMap` must be a boolean');
invariant(bo.minify === undefined || typeof bo.minify === 'boolean', 'bundle options field `minify` must be a boolean');
invariant(bo.platform === undefined || typeof bo.platform === 'string', 'bundle options field `platform` must be a string');
invariant(bo.runModule === undefined || typeof bo.runModule === 'boolean', 'bundle options field `runModule` must be a boolean');
invariant(bo.sourceMapUrl === undefined || typeof bo.sourceMapUrl === 'string', 'bundle options field `sourceMapUrl` must be a boolean');
return {entryFile, ...bo};
}
exports.buildBundle = function(options: Options, bundleOptions: PublicBundleOptions) {
var server = createNonPersistentServer(options);
const ServerClass = require('./src/Server');
return server.buildBundle({
...ServerClass.DEFAULT_BUNDLE_OPTIONS,
...assertPublicBundleOptions(bundleOptions),
}).then(p => {
server.end();
return p;
});
};
exports.getOrderedDependencyPaths = function(options: Options, depOptions: {
+entryFile: string,
+dev: boolean,
+platform: string,
}) {
var server = createNonPersistentServer(options);
return server.getOrderedDependencyPaths(depOptions)
.then(function(paths) {
server.end();
return paths;
});
};
function enableDebug() {
// react-packager logs debug messages using the 'debug' npm package, and uses
// the following prefix throughout.
// To enable debugging, we need to set our pattern or append it to any
// existing pre-configured pattern to avoid disabling logging for
// other packages
var debugPattern = 'RNP:*';
var existingPattern = debug.load();
if (existingPattern) {
debugPattern += ',' + existingPattern;
}
debug.enable(debugPattern);
}
function createServer(options: StrictOptions): Server {
// the debug module is configured globally, we need to enable debugging
// *before* requiring any packages that use `debug` for logging
if (options.verbose) {
enableDebug();
}
// Some callsites may not be Flowified yet.
invariant(options.reporter != null, 'createServer() requires reporter');
const serverOptions = Object.assign({}, options);
delete serverOptions.verbose;
const ServerClass = require('./src/Server');
return new ServerClass(serverOptions);
}
function createNonPersistentServer(options: Options): Server {
const serverOptions = {
// It's unsound to set-up the reporter here,
// but this allows backward compatibility.
reporter: options.reporter == null
? require('./src/lib/reporting').nullReporter
: options.reporter,
...options,
watch: !options.nonPersistent,
};
return createServer(serverOptions);
}

View File

@ -14,7 +14,7 @@ const escapeRegExp = require('lodash/escapeRegExp');
const path = require('path');
const BABEL_ENABLED_PATHS = [
'packager/react-packager.js',
'packager/index.js',
'packager/src',
'packager/transformer.js',
'local-cli',