Sha256: 0f620fc42a7e793df1be87a79cebd956b7428689701397c2279ccfe3676b1a0f

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

require "yaml"
require "down"

module Snowpack
  module Web
    class Assets
      attr_reader :root
      attr_reader :precompiled
      attr_reader :server_url

      def initialize(root:, precompiled:, server_url: nil)
        @root = root
        @precompiled = precompiled
        @server_url = server_url
      end

      def [](asset)
        if precompiled
          asset_path_from_manifest(asset)
        else
          asset_path_on_server(asset)
        end
      end

      def read(asset)
        path = self[asset]

        if File.exist?("#{root}/public#{path}")
          File.read("#{root}/public#{path}")
        else
          Down.open(path).read
        end
      end

      private

      def asset_path_from_manifest(asset)
        if (hashed_asset = manifest[asset])
          "/assets/#{hashed_asset}"
        end
      end

      def asset_path_on_server(asset)
        "#{server_url}/assets/#{asset}"
      end

      def manifest
        @manifest ||= YAML.load_file(manifest_path)
      end

      def manifest_path
        "#{root}/public/assets/asset-manifest.json"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
snowpack-1.0.0.alpha7 lib/snowpack/web/assets.rb
snowpack-1.0.0.alpha6 lib/snowpack/web/assets.rb
snowpack-1.0.0.alpha5 lib/snowpack/web/assets.rb
snowpack-1.0.0.alpha4 lib/snowpack/web/assets.rb
snowpack-1.0.0.alpha3 lib/snowpack/web/assets.rb
snowpack-1.0.0.alpha2 lib/snowpack/web/assets.rb