Updated Packer - the current version was working incorrectly.

This commit is contained in:
John Resig 2007-05-05 17:37:38 +00:00
parent ed1e3f7e05
commit 003af8e383
2 changed files with 37 additions and 27 deletions

View file

@ -1,5 +1,5 @@
/*
Packer version 3.0 (beta 5) - copyright 2004-2007, Dean Edwards
Packer version 3.0 (beta 8) - copyright 2004-2007, Dean Edwards
http://www.opensource.org/licenses/mit-license
*/
@ -13,14 +13,14 @@ var WORDS = /\w+/g;
var Packer = Base.extend({
minify: function(script) {
script = script.replace(Packer.CONTINUE, "");
script = Packer.clean.exec(script);
script = Packer.data.exec(script);
script = Packer.whitespace.exec(script);
script = Packer.clean.exec(script); // seem to grab a few more bytes on the second pass
script = Packer.clean.exec(script);
return script;
},
pack: function(script, base62, shrink) {
script = this.minify(script);
script = this.minify(script + "\n");
if (shrink) script = this._shrinkVariables(script);
if (base62) script = this._base62Encode(script);
return script;
@ -59,8 +59,13 @@ var Packer = Base.extend({
};
var data = []; // encoded strings and regular expressions
var REGEXP = /^[^'"]\//;
var store = function(string) {
var replacement = "#" + data.length;
if (REGEXP.test(string)) {
replacement = string.charAt(0) + replacement;
string = string.slice(1);
}
data.push(string);
return replacement;
};
@ -74,7 +79,7 @@ var Packer = Base.extend({
// identify blocks, particularly identify function blocks (which define scope)
var BLOCK = /(function\s*[\w$]*\s*\(\s*([^\)]*)\s*\)\s*)?(\{([^{}]*)\})/;
var VAR_ = /var\s+/g;
var VAR_NAME = /var\s+[\w$]{2,}/g; // > 1 char
var VAR_NAME = /var\s+[\w$]+/g;
var COMMA = /\s*,\s*/;
var blocks = []; // store program blocks (anything between braces {})
// encoder for program blocks
@ -93,8 +98,9 @@ var Packer = Base.extend({
// process each identifier
var count = 0, shortId;
forEach (ids, function(id) {
id = rescape(trim(id));
if (id) {
id = trim(id);
if (id && id.length > 1) { // > 1 char
id = rescape(id);
// find the next free short name (check everything in the current scope)
do shortId = encode52(count++);
while (new RegExp("[^\\w$.]" + shortId + "[^\\w$:]").test(block));
@ -106,13 +112,13 @@ var Packer = Base.extend({
}
});
}
var replacement = "~" + blocks.length;
var replacement = "~" + blocks.length + "~";
blocks.push(block);
return replacement;
};
// decoder for program blocks
var ENCODED = /~(\d+)/;
var ENCODED = /~(\d+)~/;
var decode = function(script) {
while (ENCODED.test(script)) {
script = script.replace(global(ENCODED), function(match, index) {
@ -158,16 +164,15 @@ var Packer = Base.extend({
"replace(new RegExp('\\\\b'+e(c)+'\\\\b','g'),k[c]);return p}('%1',%2,%3,'%4'.split('|'),0,{}))",
init: function() {
this.data = reduce(this.data, new RegGrp, function(data, replacement, expression) {
this.data = reduce(this.data, function(data, replacement, expression) {
data.store(this.javascript.exec(expression), replacement);
return data;
}, this);
}, new RegGrp, this);
this.clean = this.data.union(this.clean);
this.whitespace = this.data.union(this.whitespace);
},
clean: {
";;;[^\\n]*": REMOVE, // triple semi-colons treated like line comments
"\\(\\s*;\\s*;\\s*\\)": "(;;)", // for (;;) loops
"throw[^};]+[};]": IGNORE, // a safari 1.3 bug
";+\\s*([};])": "$1"
@ -178,17 +183,16 @@ var Packer = Base.extend({
"STRING1": IGNORE,
'STRING2': IGNORE,
"CONDITIONAL": IGNORE, // conditional comments
"(COMMENT1)\\n\\s*(REGEXP)?": "\n$2",
"(COMMENT1)\\n\\s*(REGEXP)?": "\n$3",
"(COMMENT2)\\s*(REGEXP)?": " $3",
"COMMENT1$": REMOVE,
"([\\[(\\^=,{}:;&|!*?])\\s*(REGEXP)": "$1$2"
},
javascript: new RegGrp({
COMMENT1: /\/\/[^\n]*/.source,
COMMENT1: /(\/\/|;;;)[^\n]*/.source,
COMMENT2: /\/\*[^*]*\*+([^\/][^*]*\*+)*\//.source,
CONDITIONAL: /\/\*@|@\*\/|\/\/@[^\n]*\n/.source,
REGEXP: /\/(\\\/|[^*\/])(\\.|[^\/\n\\])*\//.source,
REGEXP: /\/(\\[\/\\]|[^*\/])(\\.|[^\/\n\\])*\/[gim]*/.source,
STRING1: /'(\\.|[^'\\])*'/.source,
STRING2: /"(\\.|[^"\\])*"/.source
}),