packr 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README +22 -0
  2. data/lib/packr.rb +14 -1
  3. data/test/packr_test.rb +11 -0
  4. metadata +2 -2
data/README CHANGED
@@ -45,6 +45,28 @@ Be careful with that - it will overwrite the contents of the file with
45
45
  the packed version. Be a good kid and use version control in case something
46
46
  goes wrong and you lose all your source code!
47
47
 
48
+ If you want PackR's variable-shrinking routine to preserve certain variable names,
49
+ you can tell it to do so by instantiating it and telling it which names you want to
50
+ preserve:
51
+
52
+ my_packr = Packr.new
53
+ my_packr.protect_vars(:some, :variable, :names)
54
+
55
+ my_packr.pack('var func = function(foo, bar, some) { return some(foo + bar); }', :shrink_vars => true)
56
+ #=> "var func=function(a,b,some){return some(a+b)}"
57
+
58
+ If you want to protect the same variables for all scripts your program compresses,
59
+ tell the +Packr+ class to protect them:
60
+
61
+ Packr.protect_vars(:some, :variable, :names)
62
+
63
+ Packr.pack('var func = function(foo, bar, some) { return some(foo + bar); }', :shrink_vars => true)
64
+ #=> "var func=function(a,b,some){return some(a+b)}"
65
+
66
+ By default, PackR always protects the variable <tt>$super</tt> so you can compress
67
+ Prototype code that uses class inheritance. The constant +PROTECTED_NAMES+ in the
68
+ +Packr+ source code controls which variables are protected by default.
69
+
48
70
  === Automated packing
49
71
 
50
72
  When installed as a Rails plugin, PackR also comes with a +rake+ task to let you
@@ -8,7 +8,14 @@ require File.dirname(__FILE__) + '/packr/words'
8
8
 
9
9
  class Packr
10
10
 
11
+ PROTECTED_NAMES = %w($super)
12
+
11
13
  class << self
14
+ def protect_vars(*args)
15
+ @packr ||= self.new
16
+ @packr.protect_vars(*args)
17
+ end
18
+
12
19
  def minify(script)
13
20
  @packr ||= self.new
14
21
  @packr.minify(script)
@@ -83,6 +90,12 @@ class Packr
83
90
  @data = RegexpGroup.new(@data)
84
91
  @whitespace = @data.union(WHITESPACE)
85
92
  @clean = @data.union(CLEAN)
93
+ @protected_names = PROTECTED_NAMES
94
+ end
95
+
96
+ def protect_vars(*args)
97
+ args = args.map { |arg| arg.to_s.strip }.select { |arg| arg =~ /^[a-z\_\$][a-z0-9\_\$]*$/i }
98
+ @protected_names = (@protected_names + args).uniq
86
99
  end
87
100
 
88
101
  def minify(script)
@@ -183,7 +196,7 @@ private
183
196
  count = 0
184
197
  ids.each do |id|
185
198
  id = id.strip
186
- if id and id.length > 1 # > 1 char
199
+ if id and id.length > 1 and !@protected_names.include?(id) # > 1 char
187
200
  id = id.rescape
188
201
  # find the next free short name (check everything in the current scope)
189
202
  short_id = encode52.call(count)
@@ -54,4 +54,15 @@ class PackrTest < Test::Unit::TestCase
54
54
  actual_words = actual.scan(/'[\w\|]+'/)[-2].gsub(/^'(.*?)'$/, '\1').split("|").sort
55
55
  assert expected_words.eql?(actual_words)
56
56
  end
57
+
58
+ def test_protected_names
59
+ expected = 'var func=function(a,b,$super,c){return $super(a+c)}'
60
+ actual = Packr.pack('var func = function(foo, bar, $super, baz) { return $super( foo + baz ); }', :shrink_vars => true)
61
+ assert_equal expected, actual
62
+ packr = Packr.new
63
+ packr.protect_vars *(%w(other) + [:method, :names] + ['some random stuff', 24])
64
+ expected = 'var func=function(a,other,$super,b,names){return $super()(other.apply(names,a))}'
65
+ actual = packr.pack('var func = function(foo, other, $super, bar, names) { return $super()(other.apply(names, foo)); }', :shrink_vars => true)
66
+ assert_equal expected, actual
67
+ end
57
68
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: packr
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.1
7
- date: 2007-12-09 00:00:00 +00:00
6
+ version: 1.0.2
7
+ date: 2007-12-13 00:00:00 +00:00
8
8
  summary: A Ruby port of Dean Edwards' JavaScript compressor
9
9
  require_paths:
10
10
  - lib