2015-03-23 18:48:02 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-09 14:56:00 +00:00
|
|
|
require('../babelRegisterOnly')([/react-packager\/src/]);
|
[react-packager] Rewrite dependency graph (support node_modules, speed, fix bugs etc)
Summary:
@public
Fixes #773, #1055
The resolver was getting a bit unwieldy because a lot has changed since the initial writing (porting node-haste).
This also splits up a large complex file into the following:
* Makes use of classes: Module, AssetModule, Package, and AssetModule_DEPRECATED (`image!` modules)
* DependencyGraph is lazy for everything that isn't haste modules and packages (need to read ahead of time)
* Lazy makes it fast, easier to reason about, and easier to add new loaders
* Has a centralized filesystem wrapper: fast-fs (ffs)
* ffs is async and lazy for any read operation and sync for directory/file lookup which makes it fast
* we can easily drop in different adapters for ffs to be able to build up the tree: watchman, git ls-files, etc
* use es6 for classes and easier to read promise-based code
Follow up diffs will include:
* Using new types (Module, AssetModule etc) in the rest of the codebase (currently we convert to plain object which is a bit of a hack)
* using watchman to build up the fs
* some caching at the object creation level (we are recreating Modules and Packages many times, we can cache them)
* A plugin system for loaders (e.g. @tadeuzagallo wants to add a native module loader)
Test Plan:
* ./runJestTests.sh react-packager
* ./runJestTests.sh PackagerIntegration
* Export open source and run the e2e test
* reset cache
* ./fbrnios.sh run and click around
2015-06-20 01:01:21 +00:00
|
|
|
|
2015-05-05 21:05:19 +00:00
|
|
|
useGracefulFs();
|
|
|
|
|
2015-10-20 18:51:15 +00:00
|
|
|
var debug = require('debug');
|
|
|
|
var omit = require('underscore').omit;
|
2015-02-20 04:10:52 +00:00
|
|
|
var Activity = require('./src/Activity');
|
|
|
|
|
|
|
|
exports.middleware = function(options) {
|
2015-10-20 18:51:15 +00:00
|
|
|
var server = createServer(options);
|
2015-02-20 04:10:52 +00:00
|
|
|
return server.processRequest.bind(server);
|
|
|
|
};
|
|
|
|
|
2015-08-19 00:36:35 +00:00
|
|
|
exports.Activity = Activity;
|
2015-08-12 18:47:02 +00:00
|
|
|
|
|
|
|
// Renamed "package" to "bundle". But maintain backwards
|
|
|
|
// compat.
|
|
|
|
exports.buildPackage =
|
|
|
|
exports.buildBundle = function(options, bundleOptions) {
|
2015-10-20 18:51:15 +00:00
|
|
|
var server = createNonPersistentServer(options);
|
2015-08-12 18:47:02 +00:00
|
|
|
return server.buildBundle(bundleOptions)
|
2015-05-23 00:12:10 +00:00
|
|
|
.then(function(p) {
|
|
|
|
server.end();
|
|
|
|
return p;
|
|
|
|
});
|
|
|
|
};
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-08-12 18:47:02 +00:00
|
|
|
exports.buildPackageFromUrl =
|
|
|
|
exports.buildBundleFromUrl = function(options, reqUrl) {
|
2015-10-20 18:51:15 +00:00
|
|
|
var server = createNonPersistentServer(options);
|
2015-08-12 18:47:02 +00:00
|
|
|
return server.buildBundleFromUrl(reqUrl)
|
2015-02-20 04:10:52 +00:00
|
|
|
.then(function(p) {
|
|
|
|
server.end();
|
|
|
|
return p;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-09-29 05:40:56 +00:00
|
|
|
exports.getDependencies = function(options, bundleOptions) {
|
2015-10-20 18:51:15 +00:00
|
|
|
var server = createNonPersistentServer(options);
|
2015-09-29 05:40:56 +00:00
|
|
|
return server.getDependencies(bundleOptions)
|
2015-02-20 04:10:52 +00:00
|
|
|
.then(function(r) {
|
|
|
|
server.end();
|
|
|
|
return r.dependencies;
|
|
|
|
});
|
|
|
|
};
|
2015-05-05 21:05:19 +00:00
|
|
|
|
2015-08-25 16:47:49 +00:00
|
|
|
exports.createClientFor = function(options) {
|
2015-10-20 18:51:15 +00:00
|
|
|
if (options.verbose) {
|
|
|
|
enableDebug();
|
|
|
|
}
|
|
|
|
startSocketInterface();
|
|
|
|
return (
|
|
|
|
require('./src/SocketInterface')
|
|
|
|
.getOrCreateSocketFor(omit(options, ['verbose']))
|
|
|
|
);
|
2015-08-25 16:47:49 +00:00
|
|
|
};
|
|
|
|
|
2015-05-05 21:05:19 +00:00
|
|
|
function useGracefulFs() {
|
|
|
|
var fs = require('fs');
|
|
|
|
var gracefulFs = require('graceful-fs');
|
2015-08-20 19:57:05 +00:00
|
|
|
gracefulFs.gracefulify(fs);
|
2015-05-05 21:05:19 +00:00
|
|
|
}
|
2015-05-23 00:12:10 +00:00
|
|
|
|
2015-10-20 18:51:15 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-05-23 00:12:10 +00:00
|
|
|
function createServer(options) {
|
2015-10-20 18:51:15 +00:00
|
|
|
// 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) {
|
2015-05-23 00:12:10 +00:00
|
|
|
Activity.disable();
|
|
|
|
// Don't start the filewatcher or the cache.
|
|
|
|
if (options.nonPersistent == null) {
|
|
|
|
options.nonPersistent = true;
|
|
|
|
}
|
|
|
|
|
2015-10-20 18:51:15 +00:00
|
|
|
return createServer(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
2015-05-23 00:12:10 +00:00
|
|
|
}
|