Fix how post-compile.js unescapes properties to avoid extra work in pre-compile.js.

Former-commit-id: f604f706af358288877763681243f4816d5cbe9e
This commit is contained in:
John-David Dalton
2012-09-03 20:48:19 -07:00
parent e4fc8dd6fe
commit 0c92d3cbb2
3 changed files with 39 additions and 57 deletions

View File

@@ -26,31 +26,29 @@
* @returns {String} Returns the processed source.
*/
function postprocess(source) {
// exit early if snippet isn't found
// move vars exposed by Closure Compiler into the IIFE
source = source.replace(/^([^(\n]+)\s*(\(function[^)]+\){)/, '$2$1');
// unescape properties (i.e. foo["bar"] => foo.bar)
source = source.replace(/(\w)\["([^."]+)"\]/g, function(match, left, right) {
return /\W/.test(right) ? match : (left + '.' + right);
});
// correct AMD module definition for AMD build optimizers
source = source.replace(/("function")\s*==\s*(typeof define)\s*&&\s*\(?\s*("object")\s*==\s*(typeof define\.amd)\s*&&\s*(define\.amd)\s*\)?/, '$2==$1&&$4==$3&&$5');
// add trailing semicolon
source = source.replace(/[\s;]*$/, ';');
// exit early if version snippet isn't found
var snippet = /VERSION\s*[=:]\s*([\'"])(.*?)\1/.exec(source);
if (!snippet) {
return source;
}
// set the version
var license = (
snippet ? licenseTemplate[/call\(this\)[;\s]*$/.test(source) ? 'underscore' : 'lodash'] : ''
).replace('@VERSION', snippet[2]);
// move vars exposed by Closure Compiler into the IIFE
source = source.replace(/^([^(\n]+)\s*(\(function[^)]+\){)/, '$2$1');
// unescape properties (i.e. foo["bar"] => foo.bar)
source = source.replace(/(\w)\["([^."]+)"\]/g, '$1.$2');
// correct AMD module definition for AMD build optimizers
source = source.replace(/("function")\s*==\s*(typeof define)\s*&&\s*\(?\s*("object")\s*==\s*(typeof define\.amd)\s*&&\s*(define\.amd)\s*\)?/, '$2==$1&&$4==$3&&$5');
// add license
source = license + '\n;' + source;
// add trailing semicolon
return source.replace(/[\s;]*$/, ';');
return licenseTemplate[/call\(this\);?$/.test(source) ? 'underscore' : 'lodash']
.replace('@VERSION', snippet[2]) + '\n;' + source;
}
/*--------------------------------------------------------------------------*/