mirror of https://github.com/status-im/metro.git
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:
parent
f0e629b8bf
commit
5db8ac9d48
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue