Skip to content

Commit

Permalink
Remove setupBabel call from main entry point
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cpojer authored and facebook-github-bot committed May 24, 2017
1 parent a3d58ba commit 17e0478
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 144 deletions.
3 changes: 2 additions & 1 deletion local-cli/dependencies/dependencies.js
Expand Up @@ -8,7 +8,8 @@
*/ */
'use strict'; 'use strict';


const ReactPackager = require('../../packager/react-packager'); require('../../setupBabel')();
const ReactPackager = require('../../packager');


const denodeify = require('denodeify'); const denodeify = require('denodeify');
const fs = require('fs'); const fs = require('fs');
Expand Down
3 changes: 2 additions & 1 deletion local-cli/server/runServer.js
Expand Up @@ -12,8 +12,9 @@


'use strict'; 'use strict';


require('../../setupBabel')();
const InspectorProxy = require('./util/inspectorProxy.js'); const InspectorProxy = require('./util/inspectorProxy.js');
const ReactPackager = require('../../packager/react-packager'); const ReactPackager = require('../../packager');


const attachHMRServer = require('./util/attachHMRServer'); const attachHMRServer = require('./util/attachHMRServer');
const connect = require('connect'); const connect = require('connect');
Expand Down
2 changes: 1 addition & 1 deletion packager/README.md
Expand Up @@ -87,7 +87,7 @@ The packager is made of two things:
ReactPackager is how you mainly interact with the API. ReactPackager is how you mainly interact with the API.


```js ```js
var ReactPackager = require('./react-packager'); var ReactPackager = require('path/to/packager/');
``` ```


### ReactPackager.buildBundle(serverOptions, bundleOptions) ### ReactPackager.buildBundle(serverOptions, bundleOptions)
Expand Down
127 changes: 125 additions & 2 deletions packager/index.js
Expand Up @@ -11,5 +11,128 @@


'use strict'; 'use strict';


require('../setupBabel')(); const Logger = require('./src/Logger');
module.exports = require('./react-packager');
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);
}
138 changes: 0 additions & 138 deletions packager/react-packager.js

This file was deleted.

2 changes: 1 addition & 1 deletion setupBabel.js
Expand Up @@ -14,7 +14,7 @@ const escapeRegExp = require('lodash/escapeRegExp');
const path = require('path'); const path = require('path');


const BABEL_ENABLED_PATHS = [ const BABEL_ENABLED_PATHS = [
'packager/react-packager.js', 'packager/index.js',
'packager/src', 'packager/src',
'packager/transformer.js', 'packager/transformer.js',
'local-cli', 'local-cli',
Expand Down

0 comments on commit 17e0478

Please sign in to comment.