[react-packager] Make dev a query param option

This commit is contained in:
Amjad Masad 2015-03-04 20:56:56 -08:00
parent 5bd8155f0b
commit a4d041ce62
7 changed files with 53 additions and 70 deletions

View File

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

View File

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

View File

@ -32,10 +32,6 @@ var validateOpts = declareOpts({
type: 'array', type: 'array',
default: [], default: [],
}, },
dev: {
type: 'boolean',
default: true,
},
nonPersistent: { nonPersistent: {
type: 'boolean', type: 'boolean',
default: false, default: false,
@ -62,20 +58,20 @@ function HasteDependencyResolver(options) {
fileWatcher: this._fileWatcher fileWatcher: this._fileWatcher
}); });
this._polyfillModuleNames = [
opts.dev this._polyfillModuleNames = opts.polyfillModuleNames || [];
? 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 || []
);
} }
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 depGraph = this._depGraph;
var self = this; var self = this;
@ -84,7 +80,7 @@ HasteDependencyResolver.prototype.getDependencies = function(main) {
var dependencies = depGraph.getOrderedDependencies(main); var dependencies = depGraph.getOrderedDependencies(main);
var mainModuleId = dependencies[0].id; var mainModuleId = dependencies[0].id;
self._prependPolyfillDependencies(dependencies); self._prependPolyfillDependencies(dependencies, opts.dev);
return { return {
mainModuleId: mainModuleId, mainModuleId: mainModuleId,
@ -94,10 +90,19 @@ HasteDependencyResolver.prototype.getDependencies = function(main) {
}; };
HasteDependencyResolver.prototype._prependPolyfillDependencies = function( HasteDependencyResolver.prototype._prependPolyfillDependencies = function(
dependencies dependencies,
isDev
) { ) {
var polyfillModuleNames = this._polyfillModuleNames; var polyfillModuleNames = [
if (polyfillModuleNames.length > 0) { 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( var polyfillModules = polyfillModuleNames.map(
function(polyfillModuleName, idx) { function(polyfillModuleName, idx) {
return new ModuleDescriptor({ return new ModuleDescriptor({
@ -109,7 +114,6 @@ HasteDependencyResolver.prototype._prependPolyfillDependencies = function(
} }
); );
dependencies.unshift.apply(dependencies, polyfillModules); dependencies.unshift.apply(dependencies, polyfillModules);
}
}; };
HasteDependencyResolver.prototype.wrapModule = function(module, code) { HasteDependencyResolver.prototype.wrapModule = function(module, code) {

View File

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

View File

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

View File

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

View File

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