Module: Spree::Admin::TailwindHelper

Defined in:
lib/spree/admin/tailwind_helper.rb

Constant Summary collapse

SOURCE_PATHS =

[relative dir, glob] pairs Tailwind scans for utility classes.

[
  ["app/views/spree/admin", "**/*.erb"],
  ["app/helpers/spree/admin", "**/*.rb"],
  ["app/javascript/spree/admin", "**/*.js"]
].freeze

Class Method Summary collapse

Class Method Details

.engine_css_pathObject



17
18
19
# File 'lib/spree/admin/tailwind_helper.rb', line 17

def engine_css_path
  Spree::Admin::Engine.root.join("app/assets/tailwind")
end

.engine_sources(engine) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/spree/admin/tailwind_helper.rb', line 57

def engine_sources(engine)
  root = engine.root.to_s

  SOURCE_PATHS.filter_map do |dir, pattern|
    full_dir = File.join(root, dir)
    %(@source "#{full_dir}/#{pattern}";) if File.directory?(full_dir)
  end
end

.input_pathObject



5
6
7
# File 'lib/spree/admin/tailwind_helper.rb', line 5

def input_path
  Rails.root.join("app/assets/tailwind/spree_admin.css")
end

.output_pathObject



9
10
11
# File 'lib/spree/admin/tailwind_helper.rb', line 9

def output_path
  Rails.root.join("app/assets/builds/spree/admin/application.css")
end

.resolved_input_cssObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spree/admin/tailwind_helper.rb', line 21

def resolved_input_css
  css = File.read(input_path)
  css = css.gsub("$SPREE_ADMIN_PATH", Spree::Admin::Engine.root.to_s)
  css = css.gsub("/* $SPREE_ENGINE_SOURCES */", spree_engine_sources)

  # Resolve relative @source paths to absolute paths since the resolved
  # CSS is written to tmp/ which changes the relative path base
  source_base = input_path.dirname
  css = css.gsub(%r{@source\s+"(\.\./[^"]+)"}) do |_match|
    relative = Regexp.last_match(1)
    absolute = File.expand_path(relative, source_base)
    %(@source "#{absolute}")
  end

  css
end

.resolved_input_pathObject



13
14
15
# File 'lib/spree/admin/tailwind_helper.rb', line 13

def resolved_input_path
  Rails.root.join("tmp/tailwind/spree_admin_resolved.css")
end

.source_globsArray<String>

Every template/CSS glob that affects the compiled admin CSS, across all Spree engines plus the host app. Used by watch mode to poll for changes (OS file-system events don't cross a Docker bind mount from a macOS host).

Returns:

  • (Array<String>)

    absolute Dir.glob patterns



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/spree/admin/tailwind_helper.rb', line 71

def source_globs
  roots = spree_engines.map { |engine| engine.root.to_s }
  roots << Rails.root.to_s

  template_globs = roots.product(SOURCE_PATHS).map do |root, (dir, pattern)|
    File.join(root, dir, pattern)
  end

  css_globs = [engine_css_path.to_s, input_path.dirname.to_s].map do |dir|
    File.join(dir, "**", "*.css")
  end

  (template_globs + css_globs).uniq
end

.spree_engine_sourcesObject



38
39
40
41
42
# File 'lib/spree/admin/tailwind_helper.rb', line 38

def spree_engine_sources
  spree_engines.flat_map do |engine|
    engine_sources(engine)
  end.join("\n")
end

.spree_enginesObject



44
45
46
47
48
# File 'lib/spree/admin/tailwind_helper.rb', line 44

def spree_engines
  Rails::Engine.subclasses.select do |engine|
    engine.name&.start_with?("Spree")
  end
end

.write_resolved_cssObject



86
87
88
89
90
# File 'lib/spree/admin/tailwind_helper.rb', line 86

def write_resolved_css
  FileUtils.mkdir_p(resolved_input_path.dirname)
  File.write(resolved_input_path, resolved_input_css)
  resolved_input_path
end