| 1 |
|
| 2 |
|
| 3 |
require "pathname" |
| 4 |
|
| 5 |
module AutoprefixerRails |
| 6 |
|
| 7 |
class Sprockets |
| 8 |
def self.register_processor(processor) |
| 9 |
@processor = processor |
| 10 |
end |
| 11 |
|
| 12 |
|
| 13 |
def self.call(input) |
| 14 |
filename = input[:filename] |
| 15 |
source = input[:data] |
| 16 |
run(filename, source) |
| 17 |
end |
| 18 |
|
| 19 |
|
| 20 |
def self.run(filename, css) |
| 21 |
output = filename.chomp(File.extname(filename)) + ".css" |
| 22 |
result = @processor.process(css, from: filename, to: output) |
| 23 |
|
| 24 |
result.warnings.each do |warning| |
| 25 |
warn "autoprefixer: #{warning}" |
| 26 |
end |
| 27 |
|
| 28 |
result.css |
| 29 |
end |
| 30 |
|
| 31 |
|
| 32 |
def self.install(env) |
| 33 |
if ::Sprockets::VERSION.to_f < 4 |
| 34 |
env.register_postprocessor("text/css", |
| 35 |
::AutoprefixerRails::Sprockets) |
| 36 |
else |
| 37 |
env.register_bundle_processor("text/css", |
| 38 |
::AutoprefixerRails::Sprockets) |
| 39 |
end |
| 40 |
end |
| 41 |
|
| 42 |
|
| 43 |
def self.uninstall(env) |
| 44 |
if ::Sprockets::VERSION.to_f < 4 |
| 45 |
env.unregister_postprocessor("text/css", |
| 46 |
::AutoprefixerRails::Sprockets) |
| 47 |
else |
| 48 |
env.unregister_bundle_processor("text/css", |
| 49 |
::AutoprefixerRails::Sprockets) |
| 50 |
end |
| 51 |
end |
| 52 |
|
| 53 |
|
| 54 |
def initialize(filename) |
| 55 |
@filename = filename |
| 56 |
@source = yield |
| 57 |
end |
| 58 |
|
| 59 |
|
| 60 |
def render(*) |
| 61 |
self.class.run(@filename, @source) |
| 62 |
end |
| 63 |
end |
| 64 |
end |