2017-01-06 14:02:37 +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.
|
|
|
|
*
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Logger = require('./src/Logger');
|
|
|
|
|
|
|
|
const debug = require('debug');
|
2017-01-13 17:23:45 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2017-01-06 14:02:37 +00:00
|
|
|
|
2017-05-03 10:45:04 +00:00
|
|
|
import type {PostProcessModules, PostMinifyProcess} from './src/Bundler';
|
2017-05-04 12:06:27 +00:00
|
|
|
import type Server from './src/Server';
|
2017-04-06 11:37:31 +00:00
|
|
|
import type {GlobalTransformCache} from './src/lib/GlobalTransformCache';
|
2017-01-06 14:02:37 +00:00
|
|
|
import type {Reporter} from './src/lib/reporting';
|
2017-02-27 18:48:17 +00:00
|
|
|
import type {HasteImpl} from './src/node-haste/Module';
|
2017-01-06 14:02:37 +00:00
|
|
|
|
|
|
|
exports.createServer = createServer;
|
|
|
|
exports.Logger = Logger;
|
|
|
|
|
|
|
|
type Options = {
|
2017-02-27 18:48:17 +00:00
|
|
|
hasteImpl?: HasteImpl,
|
2017-01-26 11:41:09 +00:00
|
|
|
globalTransformCache: ?GlobalTransformCache,
|
2017-01-26 11:41:13 +00:00
|
|
|
nonPersistent?: boolean,
|
2017-04-28 16:27:19 +00:00
|
|
|
postProcessModules?: PostProcessModules,
|
2017-05-03 10:45:04 +00:00
|
|
|
postMinifyProcess?: PostMinifyProcess,
|
2017-05-17 11:55:59 +00:00
|
|
|
projectRoots: $ReadOnlyArray<string>,
|
2017-01-06 14:02:37 +00:00
|
|
|
reporter?: Reporter,
|
2017-05-04 12:06:27 +00:00
|
|
|
+sourceExts: ?Array<string>,
|
2017-05-11 23:37:03 +00:00
|
|
|
+transformModulePath: string,
|
2017-01-06 14:02:37 +00:00
|
|
|
watch?: boolean,
|
|
|
|
};
|
|
|
|
|
2017-04-28 16:27:19 +00:00
|
|
|
type StrictOptions = {...Options, reporter: Reporter};
|
2017-01-09 18:10:09 +00:00
|
|
|
|
2017-03-14 18:01:21 +00:00
|
|
|
type PublicBundleOptions = {
|
|
|
|
+dev?: boolean,
|
|
|
|
+entryFile: string,
|
|
|
|
+generateSourceMaps?: boolean,
|
|
|
|
+inlineSourceMap?: boolean,
|
|
|
|
+minify?: boolean,
|
|
|
|
+platform?: string,
|
|
|
|
+runModule?: boolean,
|
|
|
|
+sourceMapUrl?: string,
|
|
|
|
};
|
|
|
|
|
2017-03-14 18:01:19 +00:00
|
|
|
/**
|
2017-03-14 18:01:21 +00:00
|
|
|
* 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.
|
2017-03-14 18:01:19 +00:00
|
|
|
*/
|
2017-03-14 18:01:21 +00:00
|
|
|
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) {
|
2017-01-06 14:02:37 +00:00
|
|
|
var server = createNonPersistentServer(options);
|
2017-03-14 18:01:21 +00:00
|
|
|
const ServerClass = require('./src/Server');
|
|
|
|
return server.buildBundle({
|
|
|
|
...ServerClass.DEFAULT_BUNDLE_OPTIONS,
|
|
|
|
...assertPublicBundleOptions(bundleOptions),
|
|
|
|
}).then(p => {
|
|
|
|
server.end();
|
|
|
|
return p;
|
|
|
|
});
|
2017-01-06 14:02:37 +00:00
|
|
|
};
|
|
|
|
|
2017-05-03 10:26:52 +00:00
|
|
|
exports.getOrderedDependencyPaths = function(options: Options, depOptions: {
|
|
|
|
+entryFile: string,
|
|
|
|
+dev: boolean,
|
|
|
|
+platform: string,
|
|
|
|
}) {
|
2017-01-06 14:02:37 +00:00
|
|
|
var server = createNonPersistentServer(options);
|
2017-05-03 10:26:52 +00:00
|
|
|
return server.getOrderedDependencyPaths(depOptions)
|
2017-01-06 14:02:37 +00:00
|
|
|
.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);
|
|
|
|
}
|
|
|
|
|
2017-03-14 18:01:19 +00:00
|
|
|
function createServer(options: StrictOptions): Server {
|
2017-01-06 14:02:37 +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();
|
|
|
|
}
|
|
|
|
|
2017-01-09 18:10:09 +00:00
|
|
|
// Some callsites may not be Flowified yet.
|
|
|
|
invariant(options.reporter != null, 'createServer() requires reporter');
|
2017-01-06 14:02:37 +00:00
|
|
|
const serverOptions = Object.assign({}, options);
|
|
|
|
delete serverOptions.verbose;
|
2017-03-14 18:01:19 +00:00
|
|
|
const ServerClass = require('./src/Server');
|
|
|
|
return new ServerClass(serverOptions);
|
2017-01-06 14:02:37 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 18:01:19 +00:00
|
|
|
function createNonPersistentServer(options: Options): Server {
|
2017-01-06 14:02:37 +00:00
|
|
|
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);
|
|
|
|
}
|