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
}),

View file

@ -1,4 +1,4 @@
// timestamp: Tue, 24 Apr 2007 09:57:15
// timestamp: Tue, 01 May 2007 19:13:00
/*
base2.js - copyright 2007, Dean Edwards
http://www.opensource.org/licenses/mit-license
@ -141,6 +141,12 @@ var assertType = function(object, type, message) {
}
};
var copy = function(object) {
var fn = new Function;
fn.prototype = object;
return new fn;
};
var format = function(string) {
// replace %n with arguments[n]
// e.g. format("%1 %2%3 %2a %1%3", "she", "se", "lls");
@ -416,12 +422,12 @@ var Enumerable = Module.extend({
},
filter: function(object, test, context) {
return this.reduce(object, new Array2, function(result, value, key) {
return this.reduce(object, function(result, value, key) {
if (test.call(context, value, key, object)) {
result[result.length] = value;
}
return result;
});
}, new Array2);
},
invoke: function(object, method) {
@ -448,7 +454,7 @@ var Enumerable = Module.extend({
});
},
reduce: function(object, result, block, context) {
reduce: function(object, block, result, context) {
this.forEach (object, function(value, key) {
result = block.call(context, result, value, key, object);
});
@ -475,10 +481,10 @@ var IArray = Module.extend({
combine: function(keys, values) {
// combine two arrays to make a hash
if (!values) values = keys;
return this.reduce(keys, {}, function(object, key, index) {
return this.reduce(keys, function(object, key, index) {
object[key] = values[index];
return object;
});
}, {});
},
copy: function(array) {
@ -904,20 +910,20 @@ var Namespace = Base.extend({
var namespace = "var base=" + base + ";";
var imports = ("base2,lang," + this.imports).split(",");
_private.imports = Enumerable.reduce(imports, namespace, function(namespace, name) {
_private.imports = Enumerable.reduce(imports, function(namespace, name) {
if (base2[name]) namespace += base2[name].namespace;
return namespace;
});
}, namespace);
var namespace = format("base2.%1=%1;", this.name);
var exports = this.exports.split(",");
_private.exports = Enumerable.reduce(exports, namespace, function(namespace, name) {
_private.exports = Enumerable.reduce(exports, function(namespace, name) {
if (name) {
this.namespace += format("var %2=%1.%2;", this.name, name);
namespace += format("if(!%1.%2)%1.%2=%2;base2.%2=%1.%2;", this.name, name);
}
return namespace;
}, this);
}, namespace, this);
if (this.name != "base2") {
base2.namespace += format("var %1=base2.%1;", this.name);
@ -949,7 +955,7 @@ eval(this.exports);
var lang = new Namespace(this, {
name: "lang",
version: base2.version,
exports: "K,assert,assertType,assignID,instanceOf,extend,format,forEach,match,rescape,slice,trim",
exports: "K,assert,assertType,assignID,copy,instanceOf,extend,format,forEach,match,rescape,slice,trim",
init: function() {
this.extend = extend;