Class: AutoprefixerRails::Sprockets

Inherits:
Object
  • Object
show all
Defined in:
lib/autoprefixer-rails/sprockets.rb

Overview

Register autoprefixer postprocessor in Sprockets and fix common issues

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Sprockets

Sprockets 2 API new and render



54
55
56
57
# File 'lib/autoprefixer-rails/sprockets.rb', line 54

def initialize(filename)
  @filename = filename
  @source   = yield
end

Class Method Details

.call(input) ⇒ Object

Sprockets 3 and 4 API



13
14
15
16
17
# File 'lib/autoprefixer-rails/sprockets.rb', line 13

def self.call(input)
  filename = input[:filename]
  source   = input[:data]
  run(filename, source)
end

.install(env) ⇒ Object

Register postprocessor in Sprockets depend on issues with other gems



32
33
34
35
36
37
38
39
40
# File 'lib/autoprefixer-rails/sprockets.rb', line 32

def self.install(env)
  if ::Sprockets::VERSION.to_f < 4
    env.register_postprocessor("text/css",
                               ::AutoprefixerRails::Sprockets)
  else
    env.register_bundle_processor("text/css",
                                  ::AutoprefixerRails::Sprockets)
  end
end

.register_processor(processor) ⇒ Object



8
9
10
# File 'lib/autoprefixer-rails/sprockets.rb', line 8

def self.register_processor(processor)
  @processor = processor
end

.run(filename, css) ⇒ Object

Add prefixes to ‘css`



20
21
22
23
24
25
26
27
28
29
# File 'lib/autoprefixer-rails/sprockets.rb', line 20

def self.run(filename, css)
  output = filename.chomp(File.extname(filename)) + ".css"
  result = @processor.process(css, from: filename, to: output)

  result.warnings.each do |warning|
    warn "autoprefixer: #{warning}"
  end

  result.css
end

.uninstall(env) ⇒ Object

Register postprocessor in Sprockets depend on issues with other gems



43
44
45
46
47
48
49
50
51
# File 'lib/autoprefixer-rails/sprockets.rb', line 43

def self.uninstall(env)
  if ::Sprockets::VERSION.to_f < 4
    env.unregister_postprocessor("text/css",
                                 ::AutoprefixerRails::Sprockets)
  else
    env.unregister_bundle_processor("text/css",
                                    ::AutoprefixerRails::Sprockets)
  end
end

Instance Method Details

#renderObject

Sprockets 2 API new and render



60
61
62
# File 'lib/autoprefixer-rails/sprockets.rb', line 60

def render(*)
  self.class.run(@filename, @source)
end