Sha256: 14b681d6f531e53470b220db445c60a535e87d32271aba291ba5054e81a42bb9

Contents?: true

Size: 2 KB

Versions: 7

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

module Pakyow
  class Application
    module Actions
      module Assets
        # Pipeline Action that processes assets at request time.
        #
        # This is intended for development use, please don't use it in production.
        # Instead, precompile assets into the public directory.
        #
        class Process
          def call(connection)
            if connection.app.config.assets.process
              # TODO: can we short circuit if the request path doesn't match the connection?
              if asset = find_asset(connection) || find_pack(connection) || find_asset_map(connection) || find_pack_map(connection)
                connection.set_header("content-type", asset.mime_type)
                connection.body = asset
                connection.halt
              end
            end
          end

          private

          def find_asset(connection, path = connection.path)
            connection.app.state(:asset).find { |asset|
              asset.public_path == path
            }
          end

          def find_pack(connection, path = connection.path)
            connection.app.state(:pack).lazy.map { |pack|
              pack.packed(path)
            }.find { |packed| !packed.nil? && packed.any? }
          end

          def find_asset_map(connection)
            if wants_map?(connection)
              if (asset = find_asset(connection, unmapped(connection))) && asset.source_map?
                asset.source_map
              end
            end
          end

          def find_pack_map(connection)
            if (pack = find_pack(connection, unmapped(connection))) && pack.source_map?
              pack.source_map
            else
              nil
            end
          end

          def wants_map?(connection)
            connection.path.end_with?(".map")
          end

          def unmapped(connection)
            File.join(File.dirname(connection.path), File.basename(connection.path, ".map"))
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pakyow-assets-1.0.6 lib/pakyow/application/actions/assets/process.rb
pakyow-assets-1.0.5 lib/pakyow/application/actions/assets/process.rb
pakyow-assets-1.0.4 lib/pakyow/application/actions/assets/process.rb
pakyow-assets-1.0.3 lib/pakyow/application/actions/assets/process.rb
pakyow-assets-1.0.2 lib/pakyow/application/actions/assets/process.rb
pakyow-assets-1.0.1 lib/pakyow/application/actions/assets/process.rb
pakyow-assets-1.0.0 lib/pakyow/application/actions/assets/process.rb