Sha256: 1dc7d971d381db14c331643176b829f15bd38eb381f549efa8f78725b58faf04

Contents?: true

Size: 912 Bytes

Versions: 6

Compression:

Stored size: 912 Bytes

Contents

# frozen_string_literal: true

require 'oj'

module Proscenium
  class Middleware
    class ParcelCss < Base
      def attempt
        benchmark :parcelcss do
          results = build("#{cli} #{cli_options.join ' '} #{root}#{@request.path}")
          render_response css_module? ? Oj.load(results, mode: :strict)['code'] : results
        end
      end

      private

      def cli
        Gem.bin_path 'proscenium', 'parcel_css'
      end

      def cli_options
        options = ['--nesting', '--custom-media', '--targets', "'>= 0.25%'"]

        if css_module?
          hash = Digest::SHA1.hexdigest(@request.path)[..7]
          options += ['--css-modules', '--css-modules-pattern', "'[local]#{hash}'"]
        end

        Rails.env.production? ? options << '-m' : options
      end

      def css_module?
        @css_module ||= /\.module\.css$/i.match?(@request.path_info)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
proscenium-0.1.0.alpha2-x86_64-linux lib/proscenium/middleware/parcel_css.rb
proscenium-0.1.0.alpha2-x86_64-darwin lib/proscenium/middleware/parcel_css.rb
proscenium-0.1.0.alpha2-arm64-darwin lib/proscenium/middleware/parcel_css.rb
proscenium-0.1.0.alpha1-x86_64-linux lib/proscenium/middleware/parcel_css.rb
proscenium-0.1.0.alpha1-x86_64-darwin lib/proscenium/middleware/parcel_css.rb
proscenium-0.1.0.alpha1-arm64-darwin lib/proscenium/middleware/parcel_css.rb