Move generic Closure Compiler fixes into build/minify.js.

Former-commit-id: 8492f60a67cbf7a2d7d3118f9653e6997ab1d15b
This commit is contained in:
John-David Dalton
2013-01-21 01:13:51 -08:00
parent 27f1e5e2f2
commit 607abf89f7
6 changed files with 54 additions and 44 deletions

View File

@@ -2174,12 +2174,6 @@
if (!isDebug) {
outputPath || (outputPath = path.join(cwd, basename + '.min.js'));
// convert the IIFE into a function call so Closure Compiler (advanced) won't strip it
if (!isIIFE) {
source = source
.replace(/\(function/, 'iife$&')
.replace(/\(this\)\)(;\s*)$/, ', this)$1');
}
minify(source, {
'filePath': filePath,
'isMapped': isMapped,
@@ -2188,16 +2182,6 @@
'modes': isIIFE && ['simple', 'hybrid'],
'outputPath': outputPath,
'onComplete': function(data) {
// restore IIFE
if (!isIIFE) {
data.source = data.source
.replace(/iife\(/, '(')
.replace(/, *this\)([\s;]*(\n\/\/.+)?)$/, '(this))$1');
}
// inject "use strict" directive
if (isStrict) {
data.source = data.source.replace(/^([\s\S]*?function[^{]+{\s*)([^"'])/, '$1"use strict";$2');
}
if (isCustom) {
data.source = addCommandsToHeader(data.source, options);
}

View File

@@ -303,32 +303,46 @@
*/
function closureCompile(source, mode, callback) {
var filePath = this.filePath,
outputPath = this.outputPath,
isAdvanced = mode == 'advanced',
isMapped = this.isMapped,
mapPath = getMapPath(outputPath),
options = closureOptions.slice();
options = closureOptions.slice(),
outputPath = this.outputPath;
// remove copyright header to make other modifications easier
var license = (/^(?:\s*\/\/.*\s*|\s*\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/\s*)*/.exec(source) || [''])[0];
if (license) {
source = source.replace(license, '');
}
var hasIIFE = /^;?\(function[^{]+{\s*/.test(source),
isStrict = hasIIFE && /^;?\(function[^{]+{\s*["']use strict["']/.test(source);
// to avoid stripping the IIFE, convert it to a function call
if (hasIIFE && isAdvanced) {
source = source
.replace(/\(function/, '__iife__$&')
.replace(/\(this\)\)([\s;]*(\n\/\/.+)?)$/, ', this)$1');
}
options.push('--compilation_level=' + optimizationModes[mode]);
if (isMapped) {
options.push('--create_source_map=' + mapPath, '--source_map_format=V3');
}
// the standard error stream, standard output stream, and the Closure Compiler process
var error = '',
output = '',
compiler = spawn('java', ['-jar', closurePath].concat(options));
var compiler = spawn('java', ['-jar', closurePath].concat(options));
if (!this.isSilent) {
console.log('Compressing ' + path.basename(outputPath, '.js') + ' using the Closure Compiler (' + mode + ')...');
}
compiler.stdout.on('data', function(data) {
// append the data to the output stream
output += data;
var error = '';
compiler.stderr.on('data', function(data) {
error += data;
});
compiler.stderr.on('data', function(data) {
// append the error message to the error stream
error += data;
var output = '';
compiler.stdout.on('data', function(data) {
output += data;
});
compiler.on('exit', function(status) {
@@ -337,6 +351,21 @@
var exception = new Error(error);
exception.status = status;
}
// restore IIFE and move exposed vars inside the IIFE
if (hasIIFE && isAdvanced) {
output = output
.replace(/__iife__\(/, '(')
.replace(/,\s*this\)([\s;]*(\n\/\/.+)?)$/, '(this))$1')
.replace(/^((?:var (?:\w+=(?:!0|!1|null)[,;])+)?)([\s\S]*?function[^{]+{)/, '$2$1');
}
// inject "use strict" directive
if (isStrict) {
output = output.replace(/^[\s\S]*?function[^{]+{/, '$&"use strict";');
}
// restore copyright header
if (license) {
output = license + output;
}
if (isMapped) {
var mapOutput = fs.readFileSync(mapPath, 'utf8');
fs.unlinkSync(mapPath);
@@ -534,8 +563,13 @@
this.uglified.gzip = result;
}
// minify the already Closure Compiler simple optimized source using UglifyJS
if (this.modes.indexOf('hybrid') > -1) {
uglify.call(this, this.compiled.simple.source, 'hybrid (simple)', onSimpleHybrid.bind(this));
var modes = this.modes;
if (modes.indexOf('hybrid') > -1) {
if (modes.indexOf('simple') > -1) {
uglify.call(this, this.compiled.simple.source, 'hybrid (simple)', onSimpleHybrid.bind(this));
} else if (modes.indexOf('advanced') > -1) {
onSimpleHybridGzip.call(this);
}
} else {
onComplete.call(this);
}

View File

@@ -24,22 +24,14 @@
* @returns {String} Returns the processed source.
*/
function postprocess(source) {
// remove old copyright/license header
// remove copyright header
source = source.replace(/^\/\**[\s\S]+?\*\/\n/, '');
// move vars exposed by the Closure Compiler into the IIFE
source = source.replace(/^((?:var (?:\w+=(?:!0|!1|null)[,;])+)?)([\s\S]*?function[^)]+\){)/, '$2$1');
// correct overly aggressive Closure Compiler advanced optimizations
source = source
.replace(/prototype\s*=\s*{\s*valueOf\s*:\s*1\s*}/, 'prototype={valueOf:1,y:1}')
.replace(/(document[^&]+&&)\s*\w+/, '$1!({toString:0}+"")');
// unescape properties (e.g. foo["bar"] => foo.bar)
source = source.replace(/(\w)\["([^."]+)"\]/g, function(match, left, right) {
return /\W/.test(right) ? match : (left + '.' + right);
});
// flip `typeof` expressions to help optimize Safari and
// correct the AMD module definition for AMD build optimizers
// (e.g. from `"number" == typeof x` to `typeof x == "number")
@@ -56,7 +48,7 @@
if (!snippet) {
return source;
}
// add new copyright/license header
// add new copyright header
var version = snippet[2];
source = licenseTemplate.replace('<%= VERSION %>', version) + '\n;' + source;

2
lodash.min.js vendored
View File

@@ -38,4 +38,4 @@ return e=arguments,o=this,0<c?i||(i=setTimeout(r,c)):(clearTimeout(i),i=null,a=f
},r.unescape=function(n){return null==n?"":(n+"").replace(ft,g)},r.uniqueId=function(n){var t=++et;return(null==n?"":n+"")+t},r.all=q,r.any=z,r.detect=F,r.foldl=M,r.foldr=P,r.include=R,r.inject=M,fr(r,function(n,t){r.prototype[t]||(r.prototype[t]=function(){var t=[this.__wrapped__];return St.apply(t,arguments),n.apply(r,t)})}),r.first=C,r.last=function(n,t,r){if(n){var e=0,u=n.length;if(typeof t=="function"){var o=u;for(t=a(t,r);o--&&t(n[o],o,n);)e++}else if(e=t,null==e||r)return n[u-1];return v(n,Dt(0,u-e))
}},r.take=C,r.head=C,fr(r,function(n,t){r.prototype[t]||(r.prototype[t]=function(t,e){var u=n(this.__wrapped__,t,e);return null==t||e&&typeof t!="function"?u:new r(u)})}),r.VERSION="1.0.0-rc.3",r.prototype.toString=function(){return this.__wrapped__+""},r.prototype.value=W,r.prototype.valueOf=W,ir(["join","pop","shift"],function(n){var t=tt[n];r.prototype[n]=function(){return t.apply(this.__wrapped__,arguments)}}),ir(["push","reverse","sort","unshift"],function(n){var t=tt[n];r.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this
}}),ir(["concat","slice","splice"],function(n){var t=tt[n];r.prototype[n]=function(){return new r(t.apply(this.__wrapped__,arguments))}}),Ht&&ir(["pop","shift","splice"],function(n){var t=tt[n],e="splice"==n;r.prototype[n]=function(){var n=this.__wrapped__,u=t.apply(n,arguments);return 0===n.length&&delete n[0],e?new r(u):u}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=r,define(function(){return r})):X?typeof module=="object"&&module&&module.exports==X?(module.exports=r)._=r:X._=r:n._=r
}(this));
})(this);

View File

@@ -330,7 +330,7 @@
/**
* Creates a function that, when called, invokes `func` with the `this` binding
* of `thisArg` and prepends any `partailArgs` to the arguments passed to the
* of `thisArg` and prepends any `partialArgs` to the arguments passed to the
* bound function.
*
* @private

View File

@@ -29,4 +29,4 @@ o in n&&(u[o]=n[o])}return u},u.pluck=F,u.range=function(n,t,r){n=+n||0,r=+r||1,
},u.reduce=R,u.reduceRight=T,u.result=function(n,t){var r=n?n[t]:G;return m(r)?n[t]():r},u.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:kt(n).length},u.some=q,u.sortedIndex=I,u.template=function(n,t,r){n||(n=""),r=g({},r,u.templateSettings);var e=0,o="__p+='",i=r.variable;n.replace(RegExp((r.escape||nt).source+"|"+(r.interpolate||nt).source+"|"+(r.evaluate||nt).source+"|$","g"),function(t,r,u,i,f){o+=n.slice(e,f).replace(rt,a),o+=r?"'+_['escape']("+r+")+'":i?"';"+i+";__p+='":u?"'+((__t=("+u+"))==null?'':__t)+'":"",e=f+t.length
}),o+="';\n",i||(i="obj",o="with("+i+"||{}){"+o+"}"),o="function("+i+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+o+"return __p}";try{var f=Function("_","return "+o)(u)}catch(c){throw c.source=o,c}return t?f(t):(f.source=o,f)},u.unescape=function(n){return n==G?"":(n+"").replace(Y,s)},u.uniqueId=function(n){var t=++Q+"";return n?n+t:t},u.all=x,u.any=q,u.detect=O,u.foldl=R,u.foldr=T,u.include=A,u.inject=R,u.first=B,u.last=function(n,t,r){if(n){var e=0,u=n.length;
if(typeof t=="function"){var o=u;for(t=i(t,r);o--&&t(n[o],o,n);)e++}else if(e=t,e==G||r)return n[u-1];return l(n,gt(0,u-e))}},u.take=B,u.head=B,u.chain=function(n){return n=new u(n),n.__chain__=V,n},u.VERSION="1.0.0-rc.3",U(u),u.prototype.chain=function(){return this.__chain__=V,this},u.prototype.value=function(){return this.__wrapped__},e("pop push reverse shift sort splice unshift".split(" "),function(n){var t=L[n];u.prototype[n]=function(){var n=this.__wrapped__;return t.apply(n,arguments),Et&&0===n.length&&delete n[0],this
}}),e(["concat","join","slice"],function(n){var t=L[n];u.prototype[n]=function(){var n=t.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new u(n),n.__chain__=V),n}}),J?typeof module=="object"&&module&&module.exports==J?(module.exports=u)._=u:J._=u:n._=u}(this));
}}),e(["concat","join","slice"],function(n){var t=L[n];u.prototype[n]=function(){var n=t.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new u(n),n.__chain__=V),n}}),J?typeof module=="object"&&module&&module.exports==J?(module.exports=u)._=u:J._=u:n._=u})(this);