Sha256: 3e812e647aacc77c417e104fd1969d8bd1e6224844be56acc06eddbfcceec3b5

Contents?: true

Size: 1.41 KB

Versions: 12

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.gsub("</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

12 entries across 12 versions & 1 rubygems

Version Path
shopify-cli-2.34.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.33.1 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.33.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.32.1 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.32.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.31.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.30.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.29.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.28.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.27.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.26.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb
shopify-cli-2.25.0 lib/shopify_cli/theme/dev_server/hot_reload/script_injector.rb