[react-packager] Start converting options to query params

This commit is contained in:
Amjad Masad 2015-03-04 18:37:32 -08:00
parent 41743e5987
commit 5bd8155f0b
2 changed files with 46 additions and 23 deletions

View File

@ -73,7 +73,16 @@ describe('processRequest', function() {
pit('returns JS bundle source on request of *.bundle',function() { pit('returns JS bundle source on request of *.bundle',function() {
return makeRequest( return makeRequest(
requestHandler, requestHandler,
'mybundle.includeRequire.runModule.bundle' 'mybundle.bundle?runModule=true'
).then(function(response) {
expect(response).toEqual('this is the source');
});
});
pit('returns JS bundle source on request of *.bundle (compat)',function() {
return makeRequest(
requestHandler,
'mybundle.runModule.bundle'
).then(function(response) { ).then(function(response) {
expect(response).toEqual('this is the source'); expect(response).toEqual('this is the source');
}); });
@ -82,7 +91,7 @@ describe('processRequest', function() {
pit('returns sourcemap on request of *.map', function() { pit('returns sourcemap on request of *.map', function() {
return makeRequest( return makeRequest(
requestHandler, requestHandler,
'mybundle.includeRequire.runModule.bundle.map' 'mybundle.map?runModule=true'
).then(function(response) { ).then(function(response) {
expect(response).toEqual('"this is the source map"'); expect(response).toEqual('"this is the source map"');
}); });
@ -91,8 +100,8 @@ describe('processRequest', function() {
pit('watches all files in projectRoot', function() { pit('watches all files in projectRoot', function() {
return makeRequest( return makeRequest(
requestHandler, requestHandler,
'mybundle.includeRequire.runModule.bundle' 'mybundle.bundle?runModule=true'
).then(function(response) { ).then(function() {
expect(watcherFunc.mock.calls[0][0]).toEqual('all'); expect(watcherFunc.mock.calls[0][0]).toEqual('all');
expect(watcherFunc.mock.calls[0][1]).not.toBe(null); expect(watcherFunc.mock.calls[0][1]).not.toBe(null);
}); });
@ -114,8 +123,8 @@ describe('processRequest', function() {
pit('invalides files in package when file is updated', function() { pit('invalides files in package when file is updated', function() {
return makeRequest( return makeRequest(
requestHandler, requestHandler,
'mybundle.includeRequire.runModule.bundle' 'mybundle.bundle?runModule=true'
).then(function(response) { ).then(function() {
var onFileChange = watcherFunc.mock.calls[0][1]; var onFileChange = watcherFunc.mock.calls[0][1];
onFileChange('all','path/file.js', options.projectRoots[0]); onFileChange('all','path/file.js', options.projectRoots[0]);
expect(invalidatorFunc.mock.calls[0][0]).toEqual('root/path/file.js'); expect(invalidatorFunc.mock.calls[0][0]).toEqual('root/path/file.js');
@ -150,7 +159,7 @@ describe('processRequest', function() {
requestHandler = server.processRequest.bind(server); requestHandler = server.processRequest.bind(server);
return makeRequest(requestHandler,'mybundle.includeRequire.runModule.bundle') return makeRequest(requestHandler, 'mybundle.bundle?runModule=true')
.then(function(response) { .then(function(response) {
expect(response).toEqual('this is the first source'); expect(response).toEqual('this is the first source');
expect(packageFunc.mock.calls.length).toBe(1); expect(packageFunc.mock.calls.length).toBe(1);
@ -159,7 +168,7 @@ describe('processRequest', function() {
}) })
.then(function() { .then(function() {
expect(packageFunc.mock.calls.length).toBe(2); expect(packageFunc.mock.calls.length).toBe(2);
return makeRequest(requestHandler,'mybundle.includeRequire.runModule.bundle') return makeRequest(requestHandler, 'mybundle.bundle?runModule=true')
.then(function(response) { .then(function(response) {
expect(response).toEqual('this is the rebuilt source'); expect(response).toEqual('this is the rebuilt source');
}); });

View File

@ -77,7 +77,7 @@ Server.prototype._rebuildPackages = function() {
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 = getOptionsFromPath(url.parse(key).pathname); 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: dev});
@ -102,7 +102,7 @@ Server.prototype._buildPackage = function(options) {
}; };
Server.prototype.buildPackageFromUrl = function(reqUrl) { Server.prototype.buildPackageFromUrl = function(reqUrl) {
var options = getOptionsFromPath(url.parse(reqUrl).pathname); var options = getOptionsFromUrl(reqUrl);
return this._buildPackage(options); return this._buildPackage(options);
}; };
@ -145,21 +145,26 @@ Server.prototype._processDebugRequest = function(reqUrl, res) {
}; };
Server.prototype.processRequest = function(req, res, next) { Server.prototype.processRequest = function(req, res, next) {
var urlObj = url.parse(req.url, true);
var pathname = urlObj.pathname;
var requestType; var requestType;
if (req.url.match(/\.bundle$/)) { if (pathname.match(/\.bundle$/)) {
requestType = 'bundle'; requestType = 'bundle';
} else if (req.url.match(/\.map$/)) { } else if (pathname.match(/\.map$/)) {
requestType = 'map'; requestType = 'map';
} else if (req.url.match(/^\/debug/)) { } else if (pathname.match(/^\/debug/)) {
this._processDebugRequest(req.url, res); this._processDebugRequest(req.url, res);
return; return;
} else { } else {
return next(); next();
return;
} }
var startReqEventId = Activity.startEvent('request:' + req.url); var startReqEventId = Activity.startEvent('request:' + req.url);
var options = getOptionsFromPath(url.parse(req.url).pathname); var options = getOptionsFromUrl(req.url);
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; var dev = this._dev;
building.then( building.then(
@ -178,16 +183,25 @@ Server.prototype.processRequest = function(req, res, next) {
).done(); ).done();
}; };
function getOptionsFromPath(pathname) { function getOptionsFromUrl(reqUrl) {
var parts = pathname.split('.'); // `true` to parse the query param as an object.
// Remove the leading slash. var urlObj = url.parse(reqUrl, true);
var main = parts[0].slice(1) + '.js';
var match = urlObj.pathname.match(/^\/?([^\.]+)\..*(bundle|map)$/);
if (!(match && match[1])) {
throw new Error('Invalid url format, expected "/path/to/file.bundle"');
}
var main = match[1] + '.js';
return { return {
runModule: parts.slice(1).some(function(part) { sourceMapUrl: urlObj.pathname.replace(/\.bundle$/, '.map'),
main: main,
runModule: urlObj.query.runModule === 'true' ||
urlObj.query.runModule === '1' ||
// Backwards compatibility.
urlObj.pathname.split('.').some(function(part) {
return part === 'runModule'; return part === 'runModule';
}), }),
main: main,
sourceMapUrl: parts.slice(0, -1).join('.') + '.map'
}; };
} }