[react-packager] Allow entry point extensions like .ios.js

This commit is contained in:
Amjad Masad 2015-03-20 17:42:54 -07:00
parent 81d024924c
commit ce6354604c
2 changed files with 30 additions and 15 deletions

View File

@ -51,7 +51,7 @@ describe('processRequest', function() {
Packager = require('../../Packager');
FileWatcher = require('../../FileWatcher');
Packager.prototype.package = function() {
Packager.prototype.package = jest.genMockFunction().mockImpl(function() {
return q({
getSource: function() {
return 'this is the source';
@ -60,7 +60,7 @@ describe('processRequest', function() {
return 'this is the source map';
},
});
};
});
FileWatcher.prototype.on = function(eventType, callback) {
@ -106,6 +106,21 @@ describe('processRequest', function() {
});
});
pit('works with .ios.js extension', function() {
return makeRequest(
requestHandler,
'index.ios.includeRequire.bundle'
).then(function(response) {
expect(response).toEqual('this is the source');
expect(Packager.prototype.package).toBeCalledWith(
'index.ios.js',
true,
'index.ios.includeRequire.map',
true
);
});
});
pit('watches all files in projectRoot', function() {
return makeRequest(
requestHandler,

View File

@ -6,7 +6,6 @@ var declareOpts = require('../lib/declareOpts');
var FileWatcher = require('../FileWatcher');
var Packager = require('../Packager');
var Activity = require('../Activity');
var setImmediate = require('timers').setImmediate;
var q = require('q');
var _ = require('underscore');
@ -236,23 +235,24 @@ Server.prototype.processRequest = function(req, res, next) {
function getOptionsFromUrl(reqUrl) {
// `true` to parse the query param as an object.
var urlObj = url.parse(reqUrl, true);
var pathname = urlObj.pathname;
var match = urlObj.pathname.match(/^\/?([^\.]+)\..*(bundle|map)$/);
if (!(match && match[1])) {
throw new Error('Invalid url format, expected "/path/to/file.bundle"');
// Backwards compatibility. Options used to be as added as '.' to the
// entry module name. We can safely remove these options.
var entryFile = pathname.replace(/^\//, '').split('.').filter(function(part) {
if (part === 'includeRequire' || part === 'runModule' ||
part === 'bundle' || part === 'map') {
return false;
}
var main = match[1] + '.js';
return true;
}).join('.') + '.js';
return {
sourceMapUrl: urlObj.pathname.replace(/\.bundle$/, '.map'),
main: main,
sourceMapUrl: pathname.replace(/\.bundle$/, '.map'),
main: entryFile,
dev: getBoolOptionFromQuery(urlObj.query, 'dev', true),
minify: getBoolOptionFromQuery(urlObj.query, 'minify'),
runModule: getBoolOptionFromQuery(urlObj.query, 'runModule') ||
// Backwards compatibility.
urlObj.pathname.split('.').some(function(part) {
return part === 'runModule';
}),
runModule: getBoolOptionFromQuery(urlObj.query, 'runModule', true),
};
}