Sha256: 8ba268592048c3fa344268aad96341a8e22ae0bcd2ff4aee8881df8076c15709

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

# frozen_string_literal: true
require_relative "../hot_reload/sections_index"

module ShopifyCLI
  module Theme
    class DevServer
      class HotReload
        class ScriptInjector
          def initialize(ctx, theme: nil)
            @ctx = ctx
            @theme = theme
            @sections_index = HotReload::SectionsIndex.new(theme) unless theme.nil?
          end

          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

          private

          def javascript_files
            %w(hot_reload.js sse_client.js theme.js)
          end

          def javascript_inline
            env = { mode: @mode }
            env[:section_names_by_type] = @sections_index.section_names_by_type

            <<~JS
              (() => {
                window.__SHOPIFY_CLI_ENV__ = #{env.to_json};
              })();
            JS
          end

          def read(filename)
            ::File.read("#{@dir}/hot_reload/resources/#{filename}")
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shopify-cli-2.36.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.35.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb