[react-packager] Add minify option as query param
This commit is contained in:
parent
e979f2324a
commit
ab2537816f
|
@ -7,6 +7,7 @@ var UglifyJS = require('uglify-js');
|
||||||
module.exports = Package;
|
module.exports = Package;
|
||||||
|
|
||||||
function Package(sourceMapUrl) {
|
function Package(sourceMapUrl) {
|
||||||
|
this._finalized = false;
|
||||||
this._modules = [];
|
this._modules = [];
|
||||||
this._sourceMapUrl = sourceMapUrl;
|
this._sourceMapUrl = sourceMapUrl;
|
||||||
}
|
}
|
||||||
|
@ -40,23 +41,56 @@ Package.prototype.finalize = function(options) {
|
||||||
|
|
||||||
Object.freeze(this._modules);
|
Object.freeze(this._modules);
|
||||||
Object.seal(this._modules);
|
Object.seal(this._modules);
|
||||||
|
this._finalized = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
Package.prototype.getSource = function(options) {
|
Package.prototype._assertFinalized = function() {
|
||||||
if (!this._source) {
|
if (!this._finalized) {
|
||||||
options = options || {};
|
throw new Error('Package need to be finalized before getting any source');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Package.prototype._getSource = function() {
|
||||||
|
if (this._source == null) {
|
||||||
this._source = _.pluck(this._modules, 'transformedCode').join('\n');
|
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) + ';';
|
|
||||||
}
|
|
||||||
this._source += '\n\/\/@ sourceMappingURL=' + this._sourceMapUrl;
|
|
||||||
}
|
}
|
||||||
return this._source;
|
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() {
|
Package.prototype.getMinifiedSourceAndMap = function() {
|
||||||
var source = this.getSource({inlineSourceMap: false});
|
this._assertFinalized();
|
||||||
|
|
||||||
|
var source = this._getSource();
|
||||||
try {
|
try {
|
||||||
return UglifyJS.minify(source, {
|
return UglifyJS.minify(source, {
|
||||||
fromString: true,
|
fromString: true,
|
||||||
|
@ -88,6 +122,8 @@ Package.prototype.getMinifiedSourceAndMap = function() {
|
||||||
};
|
};
|
||||||
|
|
||||||
Package.prototype.getSourceMap = function(options) {
|
Package.prototype.getSourceMap = function(options) {
|
||||||
|
this._assertFinalized();
|
||||||
|
|
||||||
options = options || {};
|
options = options || {};
|
||||||
var mappings = this._getMappings();
|
var mappings = this._getMappings();
|
||||||
var map = {
|
var map = {
|
||||||
|
@ -102,7 +138,6 @@ Package.prototype.getSourceMap = function(options) {
|
||||||
return map;
|
return map;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
Package.prototype._getMappings = function() {
|
Package.prototype._getMappings = function() {
|
||||||
var modules = this._modules;
|
var modules = this._modules;
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,10 @@ Server.prototype._rebuildPackages = function() {
|
||||||
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: options.dev});
|
p.getSource({
|
||||||
|
inlineSourceMap: options.dev,
|
||||||
|
minify: options.minify,
|
||||||
|
});
|
||||||
return p;
|
return p;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -164,7 +167,10 @@ Server.prototype.processRequest = function(req, res, next) {
|
||||||
building.then(
|
building.then(
|
||||||
function(p) {
|
function(p) {
|
||||||
if (requestType === 'bundle') {
|
if (requestType === 'bundle') {
|
||||||
res.end(p.getSource({inlineSourceMap: options.dev}));
|
res.end(p.getSource({
|
||||||
|
inlineSourceMap: options.dev,
|
||||||
|
minify: options.minify,
|
||||||
|
}));
|
||||||
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()));
|
||||||
|
@ -190,10 +196,9 @@ 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' ||
|
dev: getBoolOptionFromQuery(urlObj.query, 'dev'),
|
||||||
urlObj.query.dev === '1',
|
minify: getBoolOptionFromQuery(urlObj.query, 'minify'),
|
||||||
runModule: urlObj.query.runModule === 'true' ||
|
runModule: getBoolOptionFromQuery(urlObj.query, 'runModule') ||
|
||||||
urlObj.query.runModule === '1' ||
|
|
||||||
// Backwards compatibility.
|
// Backwards compatibility.
|
||||||
urlObj.pathname.split('.').some(function(part) {
|
urlObj.pathname.split('.').some(function(part) {
|
||||||
return part === 'runModule';
|
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) {
|
function handleError(res, error) {
|
||||||
res.writeHead(500, {
|
res.writeHead(500, {
|
||||||
'Content-Type': 'application/json; charset=UTF-8',
|
'Content-Type': 'application/json; charset=UTF-8',
|
||||||
|
|
Loading…
Reference in New Issue