[react-packager] Add minify option as query param

This commit is contained in:
Amjad Masad 2015-03-05 11:45:10 -08:00
parent e979f2324a
commit ab2537816f
2 changed files with 60 additions and 16 deletions

View File

@ -7,6 +7,7 @@ var UglifyJS = require('uglify-js');
module.exports = Package;
function Package(sourceMapUrl) {
this._finalized = false;
this._modules = [];
this._sourceMapUrl = sourceMapUrl;
}
@ -40,23 +41,56 @@ Package.prototype.finalize = function(options) {
Object.freeze(this._modules);
Object.seal(this._modules);
this._finalized = true;
};
Package.prototype.getSource = function(options) {
if (!this._source) {
options = options || {};
this._source = _.pluck(this._modules, 'transformedCode').join('\n');
if (options.inlineSourceMap) {
var sourceMap = this.getSourceMap({excludeSource: true});
this._source += '\nRAW_SOURCE_MAP = ' + JSON.stringify(sourceMap) + ';';
Package.prototype._assertFinalized = function() {
if (!this._finalized) {
throw new Error('Package need to be finalized before getting any source');
}
this._source += '\n\/\/@ sourceMappingURL=' + this._sourceMapUrl;
};
Package.prototype._getSource = function() {
if (this._source == null) {
this._source = _.pluck(this._modules, 'transformedCode').join('\n');
}
return this._source;
};
Package.prototype._getInlineSourceMap = function() {
if (this._inlineSourceMap == null) {
var sourceMap = this.getSourceMap({excludeSource: true});
this._inlineSourceMap = '\nRAW_SOURCE_MAP = ' +
JSON.stringify(sourceMap) + ';';
}
return this._inlineSourceMap;
};
Package.prototype.getSource = function(options) {
this._assertFinalized();
options = options || {};
if (options.minify) {
return this.getMinifiedSourceAndMap().code;
}
var source = this._getSource();
if (options.inlineSourceMap) {
source += this._getInlineSourceMap();
}
source += '\n\/\/@ sourceMappingURL=' + this._sourceMapUrl;
return source;
};
Package.prototype.getMinifiedSourceAndMap = function() {
var source = this.getSource({inlineSourceMap: false});
this._assertFinalized();
var source = this._getSource();
try {
return UglifyJS.minify(source, {
fromString: true,
@ -88,6 +122,8 @@ Package.prototype.getMinifiedSourceAndMap = function() {
};
Package.prototype.getSourceMap = function(options) {
this._assertFinalized();
options = options || {};
var mappings = this._getMappings();
var map = {
@ -102,7 +138,6 @@ Package.prototype.getSourceMap = function(options) {
return map;
};
Package.prototype._getMappings = function() {
var modules = this._modules;

View File

@ -74,7 +74,10 @@ Server.prototype._rebuildPackages = function() {
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: options.dev});
p.getSource({
inlineSourceMap: options.dev,
minify: options.minify,
});
return p;
});
});
@ -164,7 +167,10 @@ Server.prototype.processRequest = function(req, res, next) {
building.then(
function(p) {
if (requestType === 'bundle') {
res.end(p.getSource({inlineSourceMap: options.dev}));
res.end(p.getSource({
inlineSourceMap: options.dev,
minify: options.minify,
}));
Activity.endEvent(startReqEventId);
} else if (requestType === 'map') {
res.end(JSON.stringify(p.getSourceMap()));
@ -190,10 +196,9 @@ 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' ||
dev: getBoolOptionFromQuery(urlObj.query, 'dev'),
minify: getBoolOptionFromQuery(urlObj.query, 'minify'),
runModule: getBoolOptionFromQuery(urlObj.query, 'runModule') ||
// Backwards compatibility.
urlObj.pathname.split('.').some(function(part) {
return part === 'runModule';
@ -201,6 +206,10 @@ function getOptionsFromUrl(reqUrl) {
};
}
function getBoolOptionFromQuery(query, opt) {
return query[opt] === 'true' || query[opt] === '1';
}
function handleError(res, error) {
res.writeHead(500, {
'Content-Type': 'application/json; charset=UTF-8',