Class: Packr

Inherits:
Object
  • Object
show all
Defined in:
lib/packr.rb,
lib/packr/words.rb,
lib/packr/base62.rb,
lib/packr/parser.rb,
lib/packr/encoder.rb,
lib/packr/minifier.rb,
lib/packr/privates.rb,
lib/packr/shrinker.rb,
lib/packr/base2/map.rb,
lib/packr/source_map.rb,
lib/packr/file_system.rb,
lib/packr/base2/collection.rb,
lib/packr/base2/regexp_group.rb

Defined Under Namespace

Modules: FileSystem, StringExtension Classes: Base62, Collection, Encoder, Map, Minifier, Parser, Privates, RegexpGroup, Shrinker, SourceMap, Words

Constant Summary collapse

IGNORE =
RegexpGroup::IGNORE
REMOVE =
""
SPACE =
" "
DATA =
Parser.new.
put("STRING1", IGNORE).
put('STRING2', IGNORE).
put("CONDITIONAL", IGNORE). # conditional comments
put("(OPERATOR)\\s*(REGEXP)", "\\1\\2")

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePackr

Returns a new instance of Packr.


64
65
66
67
68
69
# File 'lib/packr.rb', line 64

def initialize
  @minifier = Minifier.new
  @shrinker = Shrinker.new
  @privates = Privates.new
  @base62   = Base62.new
end

Class Method Details

.bundle(*options) ⇒ Object


60
61
62
# File 'lib/packr.rb', line 60

def self.bundle(*options)
  FileSystem.bundle(*options)
end

.encode52(c) ⇒ Object


45
46
47
48
49
50
51
52
53
54
# File 'lib/packr.rb', line 45

def self.encode52(c)
  # Base52 encoding (a-Z)
  encode = lambda do |d|
    (d < 52 ? '' : encode.call((d / 52.0).to_i)) +
        ((d = d % 52) > 25 ? (d + 39).chr : (d + 97).chr)
  end
  encoded = encode.call(c.to_i)
  encoded = encoded[1..-1] + '0' if encoded =~ /^(do|if|in)$/
  encoded
end

.encode62(c) ⇒ Object


40
41
42
43
# File 'lib/packr.rb', line 40

def self.encode62(c)
  (c < 62 ? '' : encode62((c / 62.0).to_i)) +
      ((c = c % 62) > 35 ? (c+29).chr : c.to_s(36))
end

.pack(script, options = {}) ⇒ Object


56
57
58
# File 'lib/packr.rb', line 56

def self.pack(script, options = {})
  new.pack(script, options)
end

Instance Method Details

#pack(script, options = {}) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/packr.rb', line 71

def pack(script, options = {})
  minify     = (options[:minify] != false)
  source_map = SourceMap.new(script, options)
  script     = source_map.source_code
  
  if minify
    script = @minifier.minify(script) { |sections| source_map.remove(sections) }
  end
  
  script = @shrinker.shrink(script, options[:protect]) if minify && options[:shrink_vars]
  script = @privates.encode(script) if minify && options[:private]
  
  source_map.update(script)
  script = @base62.encode(script) if minify && options[:base62]
  
  source_map.(script)
end