class Spina::TailwindPurger

This TailwindPurger was originally written by DHH in the tailwindcss-rails gem and is licensed under the following MIT License. This class has been edited slightly to make it work with Spina's use case.

Copyright © 2020 David Heinemeier Hansson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Constants

AT_RULE
BEGINNING_OF_BLOCK
CLASSLESS_BEGINNING_OF_BLOCK
CLASSLESS_SELECTOR_GROUP
CLASS_BREAK
CLASS_NAME_PATTERN
COMMENT
COMMENTS_AND_BLANK_LINES
END_OF_BLOCK
PROPERTIES
PROPERTY_NAME
PROPERTY_VALUE
SELECTOR_GROUP

Attributes

keep_these_class_names[R]

Public Class Methods

escape_class_selector(class_name) click to toggle source
# File lib/spina/tailwind_purger.rb, line 62
def escape_class_selector(class_name)
  class_name.gsub(/\A\d|[^-_a-z0-9]/, '\\\\\0')
end
extract_class_names(string) click to toggle source
# File lib/spina/tailwind_purger.rb, line 54
def extract_class_names(string)
  string.scan(CLASS_NAME_PATTERN).uniq.sort!
end
extract_class_names_from(files) click to toggle source
# File lib/spina/tailwind_purger.rb, line 58
def extract_class_names_from(files)
  Array(files).flat_map { |file| extract_class_names(file.read) }.uniq.sort!
end
new(keep_these_class_names) click to toggle source
# File lib/spina/tailwind_purger.rb, line 67
def initialize(keep_these_class_names)
  @keep_these_class_names = keep_these_class_names
end
purge(input, keeping_class_names_from_files:) click to toggle source
# File lib/spina/tailwind_purger.rb, line 50
def purge(input, keeping_class_names_from_files:)
  new(extract_class_names_from(keeping_class_names_from_files)).purge(input)
end

Public Instance Methods

purge(input) click to toggle source
# File lib/spina/tailwind_purger.rb, line 71
def purge(input)
  conveyor = Conveyor.new(input)

  until conveyor.done?
    conveyor.discard(COMMENTS_AND_BLANK_LINES) \
    or conveyor.conditionally_keep(PROPERTIES) { conveyor.staged_output.last != "" } \
    or conveyor.conditionally_keep(END_OF_BLOCK) { not conveyor.staged_output.pop } \
    or conveyor.stage_output(CLASSLESS_BEGINNING_OF_BLOCK) \
    or conveyor.stage_output(BEGINNING_OF_BLOCK) { |match| purge_beginning_of_block(match.to_s) } \
    or raise "infinite loop"
  end

  conveyor.output
end

Private Instance Methods

keep_these_selectors_pattern() click to toggle source
# File lib/spina/tailwind_purger.rb, line 87
def keep_these_selectors_pattern
  @keep_these_selectors_pattern ||= begin
    escaped_classes = @keep_these_class_names.map { |name| Regexp.escape self.class.escape_class_selector(name) }
    /(?:\A|,)[^.,{]*(?:[.](?:#{escaped_classes.join("|")})#{CLASS_BREAK}[^.,{]*)*(?=[,{])/
  end
end
purge_beginning_of_block(string) click to toggle source
# File lib/spina/tailwind_purger.rb, line 94
def purge_beginning_of_block(string)
  purged = string.scan(keep_these_selectors_pattern).join
  unless purged.empty?
    purged.sub!(/\A,\s*/, "")
    purged.rstrip!
    purged << " {\n"
  end
  purged
end