csso-rails 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 840e602c661b0ac531066c2ca19f8ba2023e82bfcd755b6c9dd15229551b3efd
4
- data.tar.gz: f36bc9d64ef72c525eb519e886e202ec2285b750bcfecd6c68e243334c573471
3
+ metadata.gz: b6862690966e276e0608530646f9b634c2bd1a4d444a673128ff054211f58b7d
4
+ data.tar.gz: 989cd8260e016051e4e07e44cb85037337d479b993bf313782189e570678955d
5
5
  SHA512:
6
- metadata.gz: 6b4145642929bebad90c9e26985280aa55170340e6321e1d5810b99a0d5b722f4a712abc16e74d13aeb5e7a89ac82a487bbfb6b12ab596c918573032482b9ca9
7
- data.tar.gz: e3c40080bcdb66176ca65b55501d773360b2980bdaf7b64ed902b4b7e5523a57134e9e5d568ef91158d194e254b8bdf78544abee9eb3c1535a4fdad629540474
6
+ metadata.gz: c4d7a0d8874889644bfb838e008dc101b916bbe5b29dda6f225a6c127a89968568357d537fa55224292fcfa96f937015360c51d79b70013d7a5c5997487e6382
7
+ data.tar.gz: c12bfcecf3aeda0964108ecdf8173d813281e8650819c86c5afb11d2d223be4ec31de04d60b45b9322ea97878c0a09b42f117f6c240007f1bb27e3fcb22952f7
data/bin/ruby_csso CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require 'csso-rails'
4
5
  require 'csso/cli'
data/csso-rails.gemspec CHANGED
@@ -1,41 +1,45 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "csso/version"
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.push File.expand_path('lib', __dir__)
4
+ require 'csso/version'
4
5
 
5
6
  Gem::Specification.new do |s|
6
- s.name = "csso-rails"
7
+ s.name = 'csso-rails'
7
8
  s.version = Csso::VERSION
8
9
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Vasily Fedoseyev"]
10
- s.email = ["vasilyfedoseyev@gmail.com"]
11
- s.homepage = "https://github.com/Vasfed/csso-rails"
12
- s.summary = %q{CSS Stylesheet optimizer/compressor for Rails}
13
- s.description = %q{Invoke the CSSO from Ruby}
10
+ s.authors = ['Vasily Fedoseyev']
11
+ s.email = ['vasilyfedoseyev@gmail.com']
12
+ s.homepage = 'https://github.com/Vasfed/csso-rails'
13
+ s.summary = 'CSS Stylesheet optimizer/compressor for Rails'
14
+ s.description = 'Invoke the CSSO from Ruby'
14
15
  s.license = 'MIT'
15
16
 
16
- s.rubyforge_project = "csso-rails"
17
+ s.rubyforge_project = 'csso-rails'
17
18
 
18
- s.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ s.files = `git ls-files -z`.split("\x0").reject do |f|
19
20
  f.match(Regexp.union(
20
- %r{^\.},
21
- %r{^spec/},
22
- %r{^Appraisals},
23
- %r{^Gemfile},
24
- %r{^gemfiles/},
25
- %r{^Rakefile},
26
- ))
21
+ %r{^gemfiles/},
22
+ %r{lib/csso/csso.js.erb},
23
+ %r{^spec/},
24
+ /^\./,
25
+ /^Appraisals/,
26
+ /^Gemfile/,
27
+ /^Rakefile/
28
+ ))
27
29
  end
28
- s.files += [Csso::CSSO_JS_LIB]
30
+ s.files += [Csso::CSSO_JS_LIB]
29
31
 
30
32
  s.bindir = 'bin'
31
- s.executables = `git ls-files -z -- bin/*`.split("\x0").map{ |f| File.basename(f) }
32
- s.require_paths = ["lib"]
33
+ s.executables = `git ls-files -z -- bin/*`.split("\x0").map do |f|
34
+ File.basename(f)
35
+ end
36
+ s.require_paths = ['lib']
33
37
 
38
+ s.required_ruby_version = '>= 1.9.2' # rubocop:disable Gemspec/RequiredRubyVersion
34
39
  s.add_dependency 'execjs', '>= 1'
35
40
 
36
- s.add_development_dependency 'bundler', '~> 1.15'
37
- s.add_development_dependency 'rake'
38
- s.add_development_dependency 'minitest'
39
- s.add_development_dependency 'mocha'
40
41
  s.add_development_dependency 'appraisal'
42
+ s.add_development_dependency 'bundler'
43
+ s.add_development_dependency 'minitest', '>= 5.0'
44
+ s.add_development_dependency 'rake'
41
45
  end
data/lib/csso/cli.rb CHANGED
@@ -1,40 +1,41 @@
1
- #encofing: utf-8
1
+ # frozen_string_literal: true
2
+
3
+ # encofing: utf-8
2
4
 
3
5
  require 'optparse'
4
6
 
5
7
  module Csso
8
+ # :nodoc
6
9
  module CLI
7
- def self.run!(argv=ARGV)
10
+ def self.run!(argv = ARGV)
8
11
  maniac = false
9
- opts = OptionParser.new do |opts|
12
+ opts = OptionParser.new do |opts| # rubocop:disable Lint/ShadowingOuterLocalVariable
10
13
  opts.version = Csso::VERSION
11
14
  opts.banner = "CSS Optimizer (ruby bindings by vasfed) version #{opts.version}"
12
- opts.separator ""
13
- opts.separator "Usage:"
15
+ opts.separator ''
16
+ opts.separator 'Usage:'
14
17
  opts.separator " #{opts.program_name} [options] FILE [FILE2 [FILE3 [...]]"
15
18
  opts.separator " #{opts.program_name} [options] < some_file.css"
16
- opts.separator ""
17
- opts.separator "All input files are concatenated and fed to stdout after processing."
18
- opts.separator ""
19
+ opts.separator ''
20
+ opts.separator 'All input files are concatenated and fed to stdout after processing.'
21
+ opts.separator ''
19
22
 
20
- opts.separator "Options:"
21
- opts.on("-m", "--[no-]maniac", "\"Maniac mode\" optimizes input multiple times until optimization stops to give any results.") do |v|
23
+ opts.separator 'Options:'
24
+ opts.on('-m', '--[no-]maniac', '"Maniac mode" optimizes input multiple times until optimization stops to give any results.') do |v|
22
25
  maniac = v
23
26
  end
24
27
 
25
- opts.on_tail("-v", "--version", "Print version information") do
28
+ opts.on_tail('-v', '--version', 'Print version information') do
26
29
  return puts opts.ver
27
30
  end
28
- opts.on_tail("-h", "--help", "Show this message") do
31
+ opts.on_tail('-h', '--help', 'Show this message') do
29
32
  return puts opts.help
30
33
  end
31
34
  end
32
35
 
33
36
  opts.parse!(argv)
34
37
 
35
- if $stdin.tty? && argv.empty?
36
- return puts opts.help
37
- end
38
+ return puts opts.help if $stdin.tty? && argv.empty?
38
39
 
39
40
  ARGV.replace(argv)
40
41
  css = ARGF.read
@@ -1,10 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Csso
4
+ # sprockets-compatible compressor
2
5
  class Compressor
3
6
  def self.call(input)
4
7
  require 'csso'
5
- #TODO: settings?
8
+ # TODO: settings?
6
9
  if input[:metadata] && input[:metadata][:map]
7
- css, map_json = Csso.optimize_with_sourcemap(input[:data],
10
+ css, map_json = Csso.optimize_with_sourcemap(
11
+ input[:data],
8
12
  # Sprockets::PathUtils.split_subpath(input[:load_path], input[:filename])
9
13
  # sprockets seems to ignore filenames here, so we may save some mem:
10
14
  'uri'
@@ -21,19 +25,19 @@ module Csso
21
25
 
22
26
  # sprockets 2:
23
27
 
24
- def initialize path, &block
28
+ def initialize(_path, &block)
25
29
  @block = block
26
30
  end
27
31
 
28
- def render context, opts={}
29
- self.class.call({data: @block.call})[:data]
32
+ def render(_context, _opts = {})
33
+ self.class.call(data: @block.call)[:data]
30
34
  end
31
35
 
36
+ # for old sprockets
32
37
  class Legacy
33
- def self.compress data
38
+ def self.compress(data)
34
39
  Compressor.call(data: data)[:data]
35
40
  end
36
41
  end
37
-
38
42
  end
39
43
  end
data/lib/csso/js_lib.rb CHANGED
@@ -1,26 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'execjs'
2
4
 
3
5
  module Csso
6
+ # low-level wrapper around the js lib
4
7
  class JsLib
5
-
6
- def initialize verbose=false
8
+ def initialize(verbose = false)
7
9
  if verbose && ExecJS.runtime.is_a?(ExecJS::ExternalRuntime)
8
10
  warn "You're using ExecJS::ExternalRuntime, did you forget to add therubyracer or other execjs runtime to gemfile?"
9
11
  end
10
12
 
11
- lib = File.read(File.expand_path('../../' + CSSO_JS_LIB, File.dirname(__FILE__)))
12
- unless @csso = ExecJS.runtime.compile(lib)
13
- raise 'cannot compile or what?'
14
- end
13
+ lib = File.read(File.expand_path("../../#{CSSO_JS_LIB}", File.dirname(__FILE__)))
14
+ raise 'cannot compile or what?' unless (@csso = ExecJS.runtime.compile(lib))
15
15
  end
16
16
 
17
- def compress css, structural_optimization=true
18
- @csso.call("do_compression", css, structural_optimization)
19
- end
17
+ def compress(css, structural_optimization = true)
18
+ # TODO: raise ArgumentError, "expect css to be a String" unless css.is_a?(String)
19
+ return nil unless css.is_a?(String)
20
20
 
21
- def compress_with_sourcemap css, filename, structural_optimization=true
22
- @csso.call("do_compression_with_map", css, filename, structural_optimization)
21
+ @csso.call('do_compression', css, structural_optimization)
23
22
  end
24
23
 
24
+ def compress_with_sourcemap(css, filename, structural_optimization = true)
25
+ return nil unless css.is_a?(String)
26
+
27
+ @csso.call('do_compression_with_map', css, filename, structural_optimization)
28
+ end
25
29
  end
26
30
  end
data/lib/csso/railtie.rb CHANGED
@@ -1,20 +1,18 @@
1
- require "action_controller/railtie"
1
+ # frozen_string_literal: true
2
2
 
3
- module Csso
3
+ require 'action_controller/railtie'
4
4
 
5
+ module Csso
6
+ # hook into rails init
5
7
  class Railtie < ::Rails::Railtie
6
- initializer "csso.environment", :after => "sprockets.environment" do |app|
7
- #NB: app.assets may be nil, sprockets-rails creates env in after_initialize
8
+ initializer 'csso.environment', after: 'sprockets.environment' do |app|
9
+ # NB: app.assets may be nil, sprockets-rails creates env in after_initialize
8
10
  Csso.install(app.assets)
9
11
  end
10
12
 
11
13
  # saas-rails-3.2.4(and may be others) sets itself as default, ignoring config? => override :(
12
- initializer "csso.setup", :after => :setup_compression, :group => :all do |app|
13
- if app.config.assets.enabled
14
- app.config.assets.css_compressor = :csso
15
- end
14
+ initializer 'csso.setup', after: :setup_compression, group: :all do |app|
15
+ app.config.assets.css_compressor = :csso if app.config.assets.enabled
16
16
  end
17
-
18
17
  end
19
-
20
18
  end
data/lib/csso/version.rb CHANGED
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Csso
2
- VERSION = '0.8.2'
4
+ VERSION = '0.9.0'
3
5
  CSSO_JS_LIB = 'vendor/csso/csso.js'
4
6
  end
data/lib/csso-rails.rb CHANGED
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'csso'
2
4
  require 'csso/railtie' if defined?(Rails)
data/lib/csso.rb CHANGED
@@ -1,16 +1,20 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'csso/version'
2
4
 
5
+ # base interface
3
6
  module Csso
4
7
  autoload :JsLib, 'csso/js_lib'
5
8
  autoload :Compressor, 'csso/compressor'
6
9
 
7
10
  def self.js_api
8
- @csso ||= Csso::JsLib.new
11
+ @js_api ||= Csso::JsLib.new
9
12
  end
10
13
 
11
14
  def self.install(sprockets)
12
15
  unless sprockets
13
- raise "no compatible sprockets found" unless Sprockets.respond_to? :register_compressor
16
+ raise 'no compatible sprockets found' unless Sprockets.respond_to? :register_compressor
17
+
14
18
  Sprockets.register_compressor('text/css', :csso, Compressor)
15
19
  return
16
20
  end
@@ -23,33 +27,32 @@ module Csso
23
27
  end
24
28
  end
25
29
 
26
- def self.optimize(css, maniac_mode=false, structural_optimization=true)
30
+ def self.optimize(css, maniac_mode = false, structural_optimization = true)
27
31
  if maniac_mode
28
32
  maniac_mode = 4 unless maniac_mode.is_a?(Integer) && maniac_mode > 0
29
- begin
33
+ loop do
30
34
  prev_css = css
31
- css = Optimizer.new.optimize(css, structural_optimization)
35
+ css = Csso.js_api.compress(css, structural_optimization)
32
36
  maniac_mode -= 1
33
- end while maniac_mode > 0 && prev_css != css
37
+ break if maniac_mode <= 0 || prev_css == css
38
+ end
34
39
  css
35
40
  else
36
- Optimizer.new.optimize(css, structural_optimization)
41
+ Csso.js_api.compress(css, structural_optimization)
37
42
  end
38
43
  end
39
44
 
40
- def self.optimize_with_sourcemap css, filename, structural_optimization=true
45
+ def self.optimize_with_sourcemap(css, filename, structural_optimization = true)
41
46
  return nil unless css.is_a?(String)
42
47
  return css if css.size <= 3
48
+
43
49
  Csso.js_api.compress_with_sourcemap(css, filename, structural_optimization)
44
50
  end
45
51
 
46
-
52
+ # deprecated
47
53
  class Optimizer
48
- def optimize(css, structural_optimization=true)
49
- return nil unless css.is_a?(String)
50
- return css if css.size <= 3
54
+ def optimize(css, structural_optimization = true)
51
55
  Csso.js_api.compress(css, structural_optimization)
52
56
  end
53
57
  end
54
-
55
58
  end