Class: ShopifyCLI::Theme::DevServer::HotReload::ScriptInjector

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb

Instance Method Summary collapse

Constructor Details

#initialize(ctx, theme: nil) ⇒ ScriptInjector

Returns a new instance of ScriptInjector.



9
10
11
12
13
# File 'lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb', line 9

def initialize(ctx, theme: nil)
  @ctx = ctx
  @theme = theme
  @sections_index = HotReload::SectionsIndex.new(theme) unless theme.nil?
end

Instance Method Details

#inject(body:, dir:, mode:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb', line 15

def inject(body:, dir:, mode:)
  @mode = mode
  @dir = dir
  hot_reload_script = [
    read("hot-reload-no-script.html"),
    "<script>",
    "(() => {",
    javascript_inline,
    *javascript_files.map { |file| read(file) },
    "})();",
    "</script>",
  ].join("\n")

  body = body.join.sub("</body>", "#{hot_reload_script}\n</body>")

  [body]
end