[react-packager] Make dev a query param option

This commit is contained in:
Amjad Masad 2015-03-04 20:56:56 -08:00
parent 4f2c336ac3
commit 78d03b89b7
7 changed files with 53 additions and 70 deletions

View File

@ -29,10 +29,6 @@ var options = parseCommandLine([{
}, {
command: 'root',
description: 'add another root(s) to be used by the packager in this project',
}, {
command: 'dev',
default: true,
description: 'produce development packages with extra warnings enabled',
}]);
if (!options.projectRoots) {
@ -97,7 +93,6 @@ function openStackFrameInEditor(req, res, next) {
function getAppMiddleware(options) {
return ReactPackager.middleware({
dev: options.dev,
projectRoots: options.projectRoots,
blacklistRE: blacklist(false),
cacheVersion: '2',
@ -106,7 +101,7 @@ function getAppMiddleware(options) {
}
function runServer(
options, /* {string projectRoot, bool web, bool dev} */
options, /* {[]string projectRoot, bool web} */
readyCallback
) {
var app = connect()

View File

@ -24,7 +24,6 @@ describe('HasteDependencyResolver', function() {
var depResolver = new HasteDependencyResolver({
projectRoot: '/root',
dev: false,
});
// Is there a better way? How can I mock the prototype instead?
@ -36,7 +35,7 @@ describe('HasteDependencyResolver', function() {
return q();
});
return depResolver.getDependencies('/root/index.js')
return depResolver.getDependencies('/root/index.js', { dev: false })
.then(function(result) {
expect(result.mainModuleId).toEqual('index');
expect(result.dependencies).toEqual([
@ -85,7 +84,6 @@ describe('HasteDependencyResolver', function() {
var depResolver = new HasteDependencyResolver({
projectRoot: '/root',
dev: true,
});
// Is there a better way? How can I mock the prototype instead?
@ -97,7 +95,7 @@ describe('HasteDependencyResolver', function() {
return q();
});
return depResolver.getDependencies('/root/index.js')
return depResolver.getDependencies('/root/index.js', { dev: true })
.then(function(result) {
expect(result.mainModuleId).toEqual('index');
expect(result.dependencies).toEqual([
@ -147,7 +145,6 @@ describe('HasteDependencyResolver', function() {
var depResolver = new HasteDependencyResolver({
projectRoot: '/root',
polyfillModuleNames: ['some module'],
dev: false,
});
// Is there a better way? How can I mock the prototype instead?
@ -159,7 +156,7 @@ describe('HasteDependencyResolver', function() {
return q();
});
return depResolver.getDependencies('/root/index.js')
return depResolver.getDependencies('/root/index.js', { dev: false })
.then(function(result) {
expect(result.mainModuleId).toEqual('index');
expect(result.dependencies).toEqual([
@ -218,7 +215,6 @@ describe('HasteDependencyResolver', function() {
it('should ', function() {
var depResolver = new HasteDependencyResolver({
projectRoot: '/root',
dev: false,
});
var depGraph = depResolver._depGraph;

View File

@ -32,10 +32,6 @@ var validateOpts = declareOpts({
type: 'array',
default: [],
},
dev: {
type: 'boolean',
default: true,
},
nonPersistent: {
type: 'boolean',
default: false,
@ -62,20 +58,20 @@ function HasteDependencyResolver(options) {
fileWatcher: this._fileWatcher
});
this._polyfillModuleNames = [
opts.dev
? path.join(__dirname, 'polyfills/prelude_dev.js')
: path.join(__dirname, 'polyfills/prelude.js'),
path.join(__dirname, 'polyfills/require.js'),
path.join(__dirname, 'polyfills/polyfills.js'),
path.join(__dirname, 'polyfills/console.js'),
path.join(__dirname, 'polyfills/error-guard.js'),
].concat(
opts.polyfillModuleNames || []
);
this._polyfillModuleNames = opts.polyfillModuleNames || [];
}
HasteDependencyResolver.prototype.getDependencies = function(main) {
var getDependenciesValidateOpts = declareOpts({
dev: {
type: 'boolean',
default: true,
},
});
HasteDependencyResolver.prototype.getDependencies = function(main, options) {
var opts = getDependenciesValidateOpts(options);
var depGraph = this._depGraph;
var self = this;
@ -84,7 +80,7 @@ HasteDependencyResolver.prototype.getDependencies = function(main) {
var dependencies = depGraph.getOrderedDependencies(main);
var mainModuleId = dependencies[0].id;
self._prependPolyfillDependencies(dependencies);
self._prependPolyfillDependencies(dependencies, opts.dev);
return {
mainModuleId: mainModuleId,
@ -94,22 +90,30 @@ HasteDependencyResolver.prototype.getDependencies = function(main) {
};
HasteDependencyResolver.prototype._prependPolyfillDependencies = function(
dependencies
dependencies,
isDev
) {
var polyfillModuleNames = this._polyfillModuleNames;
if (polyfillModuleNames.length > 0) {
var polyfillModules = polyfillModuleNames.map(
function(polyfillModuleName, idx) {
return new ModuleDescriptor({
path: polyfillModuleName,
id: polyfillModuleName,
dependencies: polyfillModuleNames.slice(0, idx),
isPolyfill: true
});
}
);
dependencies.unshift.apply(dependencies, polyfillModules);
}
var polyfillModuleNames = [
isDev
? path.join(__dirname, 'polyfills/prelude_dev.js')
: path.join(__dirname, 'polyfills/prelude.js'),
path.join(__dirname, 'polyfills/require.js'),
path.join(__dirname, 'polyfills/polyfills.js'),
path.join(__dirname, 'polyfills/console.js'),
path.join(__dirname, 'polyfills/error-guard.js'),
].concat(this._polyfillModuleNames);
var polyfillModules = polyfillModuleNames.map(
function(polyfillModuleName, idx) {
return new ModuleDescriptor({
path: polyfillModuleName,
id: polyfillModuleName,
dependencies: polyfillModuleNames.slice(0, idx),
isPolyfill: true
});
}
);
dependencies.unshift.apply(dependencies, polyfillModules);
};
HasteDependencyResolver.prototype.wrapModule = function(module, code) {

View File

@ -34,10 +34,6 @@ var validateOpts = declareOpts({
type: 'boolean',
default: false,
},
dev: {
type: 'boolean',
default: true,
},
transformModulePath: {
type:'string',
required: false,

View File

@ -36,10 +36,6 @@ var validateOpts = declareOpts({
type: 'boolean',
default: false,
},
dev: {
type: 'boolean',
default: true,
},
transformModulePath: {
type:'string',
required: false,
@ -59,7 +55,6 @@ function Packager(options) {
projectRoots: opts.projectRoots,
blacklistRE: opts.blacklistRE,
polyfillModuleNames: opts.polyfillModuleNames,
dev: opts.dev,
nonPersistent: opts.nonPersistent,
moduleFormat: opts.moduleFormat
});
@ -69,7 +64,6 @@ function Packager(options) {
blacklistRE: opts.blacklistRE,
cacheVersion: opts.cacheVersion,
resetCache: opts.resetCache,
dev: opts.dev,
transformModulePath: opts.transformModulePath,
nonPersistent: opts.nonPersistent,
});
@ -82,14 +76,14 @@ Packager.prototype.kill = function() {
]);
};
Packager.prototype.package = function(main, runModule, sourceMapUrl) {
Packager.prototype.package = function(main, runModule, sourceMapUrl, isDev) {
var transformModule = this._transformModule.bind(this);
var ppackage = new Package(sourceMapUrl);
var findEventId = Activity.startEvent('find dependencies');
var transformEventId;
return this.getDependencies(main)
return this.getDependencies(main, isDev)
.then(function(result) {
Activity.endEvent(findEventId);
transformEventId = Activity.startEvent('transform');
@ -119,8 +113,8 @@ Packager.prototype.invalidateFile = function(filePath) {
this._transformer.invalidateFile(filePath);
};
Packager.prototype.getDependencies = function(main) {
return this._resolver.getDependencies(main);
Packager.prototype.getDependencies = function(main, isDev) {
return this._resolver.getDependencies(main, { dev: isDev });
};
Packager.prototype._transformModule = function(module) {

View File

@ -35,10 +35,6 @@ var validateOpts = declareOpts({
type: 'boolean',
default: false,
},
dev: {
type: 'boolean',
default: true,
},
transformModulePath: {
type:'string',
required: false,
@ -51,7 +47,6 @@ var validateOpts = declareOpts({
function Server(options) {
var opts = validateOpts(options);
this._dev = opts.dev;
this._projectRoots = opts.projectRoots;
this._packages = Object.create(null);
this._packager = new Packager(opts);
@ -73,14 +68,13 @@ Server.prototype._onFileChange = function(type, filepath, root) {
};
Server.prototype._rebuildPackages = function() {
var dev = this._dev;
var buildPackage = this._buildPackage.bind(this);
var packages = this._packages;
Object.keys(packages).forEach(function(key) {
var options = getOptionsFromUrl(key);
packages[key] = buildPackage(options).then(function(p) {
// Make a throwaway call to getSource to cache the source string.
p.getSource({inlineSourceMap: dev});
p.getSource({inlineSourceMap: options.dev});
return p;
});
});
@ -97,7 +91,8 @@ Server.prototype._buildPackage = function(options) {
return this._packager.package(
options.main,
options.runModule,
options.sourceMapUrl
options.sourceMapUrl,
options.dev
);
};
@ -166,11 +161,10 @@ Server.prototype.processRequest = function(req, res, next) {
var building = this._packages[req.url] || this._buildPackage(options);
this._packages[req.url] = building;
var dev = this._dev;
building.then(
building.then(
function(p) {
if (requestType === 'bundle') {
res.end(p.getSource({inlineSourceMap: dev}));
res.end(p.getSource({inlineSourceMap: options.dev}));
Activity.endEvent(startReqEventId);
} else if (requestType === 'map') {
res.end(JSON.stringify(p.getSourceMap()));
@ -196,6 +190,8 @@ function getOptionsFromUrl(reqUrl) {
return {
sourceMapUrl: urlObj.pathname.replace(/\.bundle$/, '.map'),
main: main,
dev: urlObj.query.dev === 'true' ||
urlObj.query.dev === '1',
runModule: urlObj.query.runModule === 'true' ||
urlObj.query.runModule === '1' ||
// Backwards compatibility.

View File

@ -42,6 +42,8 @@ module.exports = function(descriptor) {
var schema = Joi.object().keys(joiKeys);
return function(opts) {
opts = opts || {};
var res = Joi.validate(opts, schema, {
abortEarly: true,
allowUnknown: false,