mirror of
https://github.com/status-im/metro.git
synced 2025-01-09 18:45:41 +00:00
4a5cbbbec8
Summary:As far as we agreed to merge `rnpm` into react-native core, we need to align our dependencies to exclude duplications. One of the steps forward would be to use the same utilities library. According to the thread on fb, everybody is fine with replacing underscore by lodash (which we use internally for rnpm). So, here we go! cc mkonicek davidaurelio grabbou **Test plan** ``` $ npm test ``` ![image](https://cloud.githubusercontent.com/assets/2273613/13173972/ee34c922-d700-11e5-971b-68ff7322b6d6.png) **Code formatting** Changes are aligned with the current [style guide](https://github.com/facebook/react-native/blob/master/CONTRIBUTING.md#style-guide). Closes https://github.com/facebook/react-native/pull/6030 Differential Revision: D3016271 Pulled By: davidaurelio fb-gh-sync-id: c4f6776a7de7470283d3ca5a8b56e423247f5e45 shipit-source-id: c4f6776a7de7470283d3ca5a8b56e423247f5e45
145 lines
3.8 KiB
JavaScript
145 lines
3.8 KiB
JavaScript
/**
|
|
* 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.
|
|
*/
|
|
'use strict';
|
|
|
|
require('../babelRegisterOnly')([/react-packager\/src/]);
|
|
|
|
require('fast-path').replace();
|
|
useGracefulFs();
|
|
|
|
var debug = require('debug');
|
|
var Activity = require('./src/Activity');
|
|
|
|
exports.createServer = createServer;
|
|
exports.middleware = function(options) {
|
|
var server = createServer(options);
|
|
return server.processRequest.bind(server);
|
|
};
|
|
|
|
exports.Activity = Activity;
|
|
|
|
// Renamed "package" to "bundle". But maintain backwards
|
|
// compat.
|
|
exports.buildPackage =
|
|
exports.buildBundle = function(options, bundleOptions) {
|
|
var server = createNonPersistentServer(options);
|
|
return server.buildBundle(bundleOptions)
|
|
.then(function(p) {
|
|
server.end();
|
|
return p;
|
|
});
|
|
};
|
|
|
|
exports.buildPrepackBundle = function(options, bundleOptions) {
|
|
var server = createNonPersistentServer(options);
|
|
return server.buildPrepackBundle(bundleOptions)
|
|
.then(function(p) {
|
|
server.end();
|
|
return p;
|
|
});
|
|
};
|
|
|
|
exports.buildPackageFromUrl =
|
|
exports.buildBundleFromUrl = function(options, reqUrl) {
|
|
var server = createNonPersistentServer(options);
|
|
return server.buildBundleFromUrl(reqUrl)
|
|
.then(function(p) {
|
|
server.end();
|
|
return p;
|
|
});
|
|
};
|
|
|
|
exports.getDependencies = function(options, bundleOptions) {
|
|
var server = createNonPersistentServer(options);
|
|
return server.getDependencies(bundleOptions)
|
|
.then(function(r) {
|
|
server.end();
|
|
return r.dependencies;
|
|
});
|
|
};
|
|
|
|
exports.createClientFor = function(options) {
|
|
if (options.verbose) {
|
|
enableDebug();
|
|
}
|
|
startSocketInterface();
|
|
return (
|
|
require('./src/SocketInterface')
|
|
.getOrCreateSocketFor(omit(options, ['verbose']))
|
|
);
|
|
};
|
|
|
|
function useGracefulFs() {
|
|
var fs = require('fs');
|
|
var gracefulFs = require('graceful-fs');
|
|
gracefulFs.gracefulify(fs);
|
|
}
|
|
|
|
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 = 'ReactNativePackager:*';
|
|
var existingPattern = debug.load();
|
|
if (existingPattern) {
|
|
debugPattern += ',' + existingPattern;
|
|
}
|
|
debug.enable(debugPattern);
|
|
}
|
|
|
|
function createServer(options) {
|
|
// the debug module is configured globally, we need to enable debugging
|
|
// *before* requiring any packages that use `debug` for logging
|
|
if (options.verbose) {
|
|
enableDebug();
|
|
}
|
|
|
|
startSocketInterface();
|
|
var Server = require('./src/Server');
|
|
return new Server(omit(options, ['verbose']));
|
|
}
|
|
|
|
function createNonPersistentServer(options) {
|
|
Activity.disable();
|
|
// Don't start the filewatcher or the cache.
|
|
if (options.nonPersistent == null) {
|
|
options.nonPersistent = true;
|
|
}
|
|
|
|
return createServer(options);
|
|
}
|
|
|
|
function omit(obj, blacklistedKeys) {
|
|
return Object.keys(obj).reduce((clone, key) => {
|
|
if (blacklistedKeys.indexOf(key) === -1) {
|
|
clone[key] = obj[key];
|
|
}
|
|
|
|
return clone;
|
|
}, {});
|
|
}
|
|
|
|
// we need to listen on a socket as soon as a server is created, but only once.
|
|
// This file also serves as entry point when spawning a socket server; in that
|
|
// case we need to start the server immediately.
|
|
var didStartSocketInterface = false;
|
|
function startSocketInterface() {
|
|
if (didStartSocketInterface) {
|
|
return;
|
|
}
|
|
didStartSocketInterface = true;
|
|
require('./src/SocketInterface').listenOnServerMessages();
|
|
}
|
|
|
|
if (require.main === module) { // used as entry point
|
|
startSocketInterface();
|
|
}
|