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

Instance Method Summary collapse

Constructor Details

#initialize(processor) ⇒ Sprockets

Returns a new instance of Sprockets.



6
7
8
# File 'lib/autoprefixer-rails/sprockets.rb', line 6

def initialize(processor)
  @processor = processor
end

Instance Method Details

#install(assets) ⇒ Object

Register postprocessor in Sprockets depend on issues with other gems



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

def install(assets)
  if ignore_syntax_error?
    register(assets) { |context, css| process_only_css(context, css) }
  else
    register(assets) { |context, css| process(context, css) }
  end
end

#process(context, css) ⇒ Object

Add prefixes for ‘css`



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

def process(context, css)
  root   = Pathname.new(context.root_path)
  input  = context.pathname.relative_path_from(root).to_s
  output = input.chomp(File.extname(input)) + '.css'

  @processor.process(css, from: input, to: output).css
end

#process_only_css(context, content) ⇒ Object

Add prefixes only if in ‘content` will be CSS.



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

def process_only_css(context, content)
  begin
    process(context, content)
  rescue ExecJS::ProgramError => e
    if e.message =~ /Can't parse CSS/
      content
    else
      raise e
    end
  end
end