multi_js 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - rbx-18mode
4
+ - rbx-19mode
5
+ - jruby-18mode
6
+ - jruby-19mode
7
+ - 1.8.7
8
+ - 1.9.2
9
+ - 1.9.3
10
+ - ruby-head
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'closure-compiler', :require => nil
4
+ # gem 'yui-compressor', :require => nil
5
+ gem 'uglifier', :require => nil
6
+
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 sterebooster
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # MultiJs [![Build Status](https://secure.travis-ci.org/stereobooster/multi_js.png?branch=master)](https://secure.travis-ci.org/#!/stereobooster/multi_js) [![Dependency Status](https://gemnasium.com/stereobooster/multi_js.png?travis)](https://gemnasium.com/stereobooster/multi_js) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/stereobooster/multi_js)
2
+
3
+ There are lot of Ruby libraries to minify JS.
4
+ Instead of choosing a single implementation and forcing users of your library to be
5
+ stuck with it, you can use MultiJs instead, which will simply choose the
6
+ best available JS minifier. Here's how to use it:
7
+
8
+ ```ruby
9
+ require 'multi_js'
10
+
11
+ MultiJs.compile('(function(){var long_name=1}())') #=> '(function(){var a=1}())'
12
+ ```
13
+
14
+ The `use` method, which sets the MultiJs adapter, takes either a symbol or a
15
+ class (to allow for custom JS minifier) that responds to `.min` at the class level.
16
+
17
+ MultiJs tries to have intelligent defaulting. That is, if you have any of the
18
+ supported engines already loaded, it will utilize them before attempting to
19
+ load any.
20
+ It will load libraries in following order:
21
+
22
+ - [closure-compiler](https://github.com/documentcloud/closure-compiler)
23
+ - [yui-compressor](https://github.com/sstephenson/ruby-yui-compressor)
24
+
25
+ If no other library is available, MultiJs falls back to [uglifier](https://github.com/lautis/uglifier).
26
+
27
+ ## TODO
28
+
29
+ - benchmark against most popular JS frameworks (speed, size)
30
+ - generate benchmark report (html or md)
31
+ - write test suite
32
+ - generate comparison table (html or md) with the help of test suite
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ desc "Run all examples"
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
9
+ task :test => :spec
10
+
11
+ namespace :doc do
12
+ require 'rdoc/task'
13
+ require File.expand_path('../lib/multi_js/version', __FILE__)
14
+ RDoc::Task.new do |rdoc|
15
+ rdoc.rdoc_dir = 'rdoc'
16
+ rdoc.title = "multi_js #{MultiJs::VERSION}"
17
+ rdoc.main = 'README.md'
18
+ rdoc.rdoc_files.include('README.md', 'LICENSE.md', 'lib/**/*.rb')
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ require 'closure-compiler' unless defined?(::Closure)
2
+
3
+ module MultiJs
4
+ module Adapters
5
+ class Closure
6
+ ParseError = ::Closure::Error
7
+
8
+ def self.compile(text, options={}) #:nodoc:
9
+ compressor = ::Closure::Compiler.new options
10
+ compressor.compile text
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'uglifier' unless defined?(::Uglifier)
2
+
3
+ module MultiJs
4
+ module Adapters
5
+ class Uglifier
6
+ ParseError = ::Uglifier::Error
7
+
8
+ def self.compile(text, options={}) #:nodoc:
9
+ compressor = ::Uglifier.new options
10
+ compressor.compile text
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'yui/compressor' unless defined?(::YUI::JavaScriptCompressor)
2
+
3
+ module MultiJs
4
+ module Adapters
5
+ class YuiCompressor
6
+ ParseError = ::YUI::Compressor::RuntimeError
7
+
8
+ def self.compile(text, options={}) #:nodoc:
9
+ compressor = ::YUI::JavaScriptCompressor.new options
10
+ compressor.compress text
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module MultiJs
2
+ VERSION = "0.0.1"
3
+ end
data/lib/multi_js.rb ADDED
@@ -0,0 +1,107 @@
1
+ require "multi_js/version"
2
+
3
+ module MultiJs
4
+ class ParseError < StandardError
5
+ def initialize(message="", backtrace=[])
6
+ super(message)
7
+ self.set_backtrace(backtrace)
8
+ end
9
+ end
10
+
11
+ @adapter = nil
12
+
13
+ REQUIREMENT_MAP = [
14
+ ["closure-compiler", :closure],
15
+ ["yui/compressor", :yui_compressor],
16
+ ["uglifier", :uglifier],
17
+ ]
18
+
19
+ class << self
20
+
21
+ # The default adapter based on what you currently
22
+ # have loaded and installed. First checks to see
23
+ # if any adapters are already loaded, then checks
24
+ # to see which are installed if none are loaded.
25
+ def default_adapter
26
+ return :closure if defined?(::Closure)
27
+ return :yui_compressor if defined?(::YUI::JavaScriptCompressor)
28
+ return :uglifier if defined?(::Uglifier)
29
+
30
+ REQUIREMENT_MAP.each do |(library, adapter)|
31
+ begin
32
+ require library
33
+ return adapter
34
+ rescue LoadError
35
+ next
36
+ end
37
+ end
38
+
39
+ :uglifier
40
+ end
41
+ # :nodoc:
42
+ alias :default_engine :default_adapter
43
+
44
+ # Get the current adapter class.
45
+ def adapter
46
+ return @adapter if @adapter
47
+ self.use self.default_adapter
48
+ @adapter
49
+ end
50
+ # :nodoc:
51
+ alias :engine :adapter
52
+
53
+ # Set the adapter utilizing a symbol, string, or class.
54
+ # Supported by default are:
55
+ #
56
+ # * <tt>:closure</tt>
57
+ # * <tt>:yui_compressor</tt>
58
+ # * <tt>:uglifier</tt>
59
+ def use(new_adapter)
60
+ @adapter = load_adapter(new_adapter)
61
+ end
62
+ alias :adapter= :use
63
+ # :nodoc:
64
+ alias :engine= :use
65
+
66
+ def load_adapter(new_adapter)
67
+ case new_adapter
68
+ when String, Symbol
69
+ require "multi_js/adapters/#{new_adapter}"
70
+ self::Adapters.const_get(:"#{new_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
71
+ when NilClass, FalseClass
72
+ default_adapter = self.default_adapter
73
+ require "multi_js/adapters/#{default_adapter}"
74
+ self::Adapters.const_get(:"#{default_adapter.to_s.split('_').map{|s| s.capitalize}.join('')}")
75
+ when Class
76
+ new_adapter
77
+ else
78
+ raise "Did not recognize your adapter specification. Please specify either a symbol or a class."
79
+ end
80
+ end
81
+
82
+ def current_adapter(options)
83
+ if new_adapter = (options || {}).delete(:adapter)
84
+ load_adapter(new_adapter)
85
+ else
86
+ adapter
87
+ end
88
+ end
89
+
90
+ # Minify CSS
91
+ def compile(string, options={})
92
+ string = string.read if string.respond_to?(:read)
93
+
94
+ adapter = current_adapter(options)
95
+ if defined?(adapter::ParseError)
96
+ begin
97
+ adapter.compile(string, options)
98
+ rescue adapter::ParseError => exception
99
+ raise ::MultiJs::ParseError.new(exception.message, exception.backtrace)
100
+ end
101
+ else
102
+ adapter.compile(string, options)
103
+ end
104
+ end
105
+
106
+ end
107
+ end
data/multi_js.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'multi_js/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "multi_js"
8
+ gem.version = MultiJs::VERSION
9
+ gem.authors = ["sterebooster"]
10
+ gem.email = ["stereobooster@gmail.com"]
11
+ gem.description = %q{A generic swappable back-end for JS minifiers}
12
+ gem.summary = %q{A generic swappable back-end for JS minifiers}
13
+ gem.homepage = "https://github.com/stereobooster/multi_js"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_dependency 'uglifier'
22
+
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'rdoc'
25
+ gem.add_development_dependency 'rspec'
26
+ gem.add_development_dependency 'simplecov'
27
+ end
@@ -0,0 +1,21 @@
1
+ shared_examples_for "an adapter" do |adapter|
2
+
3
+ before do
4
+ begin
5
+ MultiJs.use adapter
6
+ rescue LoadError
7
+ pending "Adapter #{adapter} couldn't be loaded (not installed?)"
8
+ end
9
+ end
10
+
11
+ describe '.compile' do
12
+ it 'minify' do
13
+ js = "(function(){var long_name=1}())"
14
+ MultiJs.compile(js).length.should be < js.length
15
+ end
16
+
17
+ it 'throws exception if parse error occurred' do
18
+ lambda { MultiJs.compile('error statement') }.should raise_error MultiJs::ParseError
19
+ end
20
+ end
21
+ end
data/spec/helper.rb ADDED
@@ -0,0 +1,23 @@
1
+ def jruby?
2
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
3
+ end
4
+
5
+ def macruby?
6
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == 'macruby'
7
+ end
8
+
9
+ unless ENV['CI'] || macruby?
10
+ require 'simplecov'
11
+ SimpleCov.start do
12
+ add_filter 'spec'
13
+ end
14
+ end
15
+
16
+ require 'multi_js'
17
+ require 'rspec'
18
+
19
+ class MockDecoder
20
+ def self.compile(string, options={})
21
+ 'js code'
22
+ end
23
+ end
@@ -0,0 +1,67 @@
1
+ require 'helper'
2
+ require 'adapter_shared_example'
3
+ require 'stringio'
4
+
5
+ describe 'MultiJs' do
6
+ context 'adapters' do
7
+ before do
8
+ MultiJs.use nil
9
+ end
10
+ context 'when no other implementations are available' do
11
+
12
+ CONSTS = [:Closure, :YUI, :Uglifier]
13
+
14
+ before do
15
+ @old_map = MultiJs::REQUIREMENT_MAP
16
+ @old_consts = {}
17
+ CONSTS.each do |c|
18
+ @old_consts[c] = Object.const_get c if Object.const_defined?(c)
19
+ end
20
+
21
+ MultiJs::REQUIREMENT_MAP.each_with_index do |(library, adapter), index|
22
+ MultiJs::REQUIREMENT_MAP[index] = ["foo/#{library}", adapter]
23
+ end
24
+
25
+ CONSTS.each do |c|
26
+ Object.send :remove_const, c if @old_consts[c]
27
+ end
28
+ end
29
+
30
+ after do
31
+ @old_map.each_with_index do |(library, adapter), index|
32
+ MultiJs::REQUIREMENT_MAP[index] = [library, adapter]
33
+ end
34
+
35
+ CONSTS.each do |c|
36
+ Object.const_set c, @old_consts[c] if @old_consts[c]
37
+ end
38
+ end
39
+
40
+ it 'defaults to vendored implemention if no other implementions are available' do
41
+ MultiJs.default_adapter.should eq :uglifier
42
+ end
43
+ end
44
+
45
+ it 'defaults to the best not vendored lib if any other lib available' do
46
+ require 'closure-compiler'
47
+ MultiJs.adapter.name.should eq 'MultiJs::Adapters::Closure'
48
+ end
49
+
50
+ it 'is settable via a symbol' do
51
+ MultiJs.use :closure
52
+ MultiJs.adapter.name.should eq 'MultiJs::Adapters::Closure'
53
+ end
54
+
55
+ it 'is settable via a class' do
56
+ MultiJs.use MockDecoder
57
+ MultiJs.adapter.name.should eq 'MockDecoder'
58
+ end
59
+ end
60
+
61
+ %w(closure uglifier).each do |adapter|
62
+ context adapter do
63
+ it_behaves_like "an adapter", adapter
64
+ end
65
+ end
66
+ end
67
+
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multi_js
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - sterebooster
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: uglifier
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rdoc
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: A generic swappable back-end for JS minifiers
95
+ email:
96
+ - stereobooster@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .travis.yml
103
+ - Gemfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - lib/multi_js.rb
108
+ - lib/multi_js/adapters/closure.rb
109
+ - lib/multi_js/adapters/uglifier.rb
110
+ - lib/multi_js/adapters/yui_compressor.rb
111
+ - lib/multi_js/version.rb
112
+ - multi_js.gemspec
113
+ - spec/adapter_shared_example.rb
114
+ - spec/helper.rb
115
+ - spec/multi_js_spec.rb
116
+ homepage: https://github.com/stereobooster/multi_js
117
+ licenses:
118
+ - MIT
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 1.8.24
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: A generic swappable back-end for JS minifiers
141
+ test_files:
142
+ - spec/adapter_shared_example.rb
143
+ - spec/helper.rb
144
+ - spec/multi_js_spec.rb