Sha256: 7bdf3ade1a2f122f5293031b1983c18e176a719b9d70d7dc4dff66e74d37e7d5

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require "pathname"

module PostCssV2
  class Engine
    def initialize(source)
      @script = Pathname.new(source + "/node_modules/.bin/postcss")
      unless @script.exist?
        Jekyll.logger.error "PostCSS v2:",
                            "PostCSS not found.
                             Make sure postcss and postcss-cli
                             are installed in your Jekyll source."
        Jekyll.logger.error "PostCSS v2:",
                            "Couldn't find #{@script}"
        exit 1
      end

      config = Pathname.new(source + "/postcss.config.js")
      unless config.exist?
        Jekyll.logger.error "PostCSS v2:",
                            "postcss.config.js not found.
                             Make sure it exists in your Jekyll source."
        Jekyll.logger.error "PostCSS v2:",
                            "Couldn't find #{config}"
        exit 1
      end
    end

    def process(page)
      file_path = Pathname.new(page.site.dest + page.url)
      postcss_command = `#{@script} #{file_path} -r`
      Jekyll.logger.info "PostCSS v2:",
                         "Rewrote #{page.url} #{postcss_command}"
    end
  end
end

Jekyll::Hooks.register :pages, :post_write do |page|
  if %r!\.css$! =~ page.url
    engine = PostCssV2::Engine.new(page.site.source)
    engine.process(page)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll-postcss-v2-1.0.0 lib/jekyll-postcss-v2/hook.rb