[react-packager] Implement bundle minification

This commit is contained in:
Amjad Masad 2015-03-02 22:52:17 -08:00
parent ee00675dab
commit c289844144
2 changed files with 49 additions and 0 deletions

View File

@ -2,6 +2,7 @@
var _ = require('underscore');
var base64VLQ = require('./base64-vlq');
var UglifyJS = require('uglify-js');
module.exports = Package;
@ -27,6 +28,7 @@ Package.prototype.addModule = function(
};
Package.prototype.finalize = function(options) {
options = options || {};
if (options.runMainModule) {
var runCode = ';require("' + this._mainModuleId + '");';
this.addModule(
@ -53,6 +55,38 @@ Package.prototype.getSource = function(options) {
return this._source;
};
Package.prototype.getMinifiedSourceAndMap = function() {
var source = this.getSource({inlineSourceMap: false});
try {
return UglifyJS.minify(source, {
fromString: true,
outSourceMap: 'bundle.js',
inSourceMap: this.getSourceMap(),
});
} catch(e) {
// Sometimes, when somebody is using a new syntax feature that we
// don't yet have transform for, the untransformed line is sent to
// uglify, and it chokes on it. This code tries to print the line
// and the module for easier debugging
var errorMessage = 'Error while minifying JS\n';
if (e.line) {
errorMessage += 'Transformed code line: "' +
source.split('\n')[e.line - 1] + '"\n';
}
if (e.pos) {
var fromIndex = source.lastIndexOf('__d(\'', e.pos);
if (fromIndex > -1) {
fromIndex += '__d(\''.length;
var toIndex = source.indexOf('\'', fromIndex);
errorMessage += 'Module name (best guess): ' +
source.substring(fromIndex, toIndex) + '\n';
}
}
errorMessage += e.toString();
throw new Error(errorMessage);
}
};
Package.prototype.getSourceMap = function(options) {
options = options || {};
var mappings = this._getMappings();

View File

@ -42,6 +42,21 @@ describe('Package', function() {
'\/\/@ sourceMappingURL=test_url',
].join('\n'));
});
it('should get minified source', function() {
var minified = {
code: 'minified',
map: 'map',
};
require('uglify-js').minify = function() {
return minified;
};
ppackage.addModule('transformed foo;', 'source foo', 'foo path');
ppackage.finalize();
expect(ppackage.getMinifiedSourceAndMap()).toBe(minified);
});
});
describe('sourcemap package', function() {