Sha256: 675f7c0258bae037576b24dd39239932d9935951e2dd3afa523d0a60b8043100

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require "open3"

module Jekyll
  module Converters
    class Tailwindcss < Converter
      safe true
      priority :low

      def matches(ext)
        /^\.css$/i.match?(ext)
      end

      def output_ext(ext)
        # At this point, we will have a CSS file
        ext
      end

      def convert(content)
        return content unless /@tailwind/i.match?(content)

        dev_mode = Jekyll.env == "development"
        Jekyll.logger.info "Jekyll Tailwind:", "Generating #{dev_mode ? "" : "minified "}CSS"

        compile_command = ::Tailwindcss::Commands
          .compile_command(debug: dev_mode, config: config_location)
          .join(" ")

        output, error = nil
        Open3.popen3(tailwindcss_env_options, compile_command) do |stdin, stdout, stderr, _wait_thread|
          stdin.write content # write the content of *.tailwindcss to the tailwindcss CLI as input
          stdin.close
          output = stdout.read
          error = stderr.read
        end
        Jekyll.logger.warn "Jekyll Tailwind:", error unless error.nil?

        output
      rescue => e
        Jekyll.logger.error "Jekyll Tailwind:", e.message
        content
      end

      private

      def tailwindcss_env_options
        # Without this ENV you'll get a warning about `Browserslist: caniuse-lite is outdated`
        # Since we're using the CLI, we can't update the data, so we ignore it.
        {"BROWSERSLIST_IGNORE_OLD_DATA" => "1"}
      end

      def config_location
        @config.dig("tailwindcss", "config") || "./tailwind.config.js"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll-tailwindcss-0.5.1 lib/jekyll/converters/tailwindcss.rb