Sha256: 5ce3c4321b47d0a83bdbde0514bdfc31020e6b7a3a6f3e0a2fd68d0d90618b05

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

require 'addressable/uri'
require 'middleman-core/middleware/inline_url_rewriter'

class Middleman::Extensions::AssetHost < ::Middleman::Extension
  option :host, nil, 'The asset host to use or a Proc to determine asset host', required: true
  option :exts, %w(.css .png .jpg .jpeg .webp .svg .svgz .js .gif), 'List of extensions that get cache busters strings appended to them.'
  option :sources, %w(.htm .html .php .css .js), 'List of extensions that are searched for bustable assets.'
  option :ignore, [], 'Regexes of filenames to skip adding query strings to'

  def after_configuration
    app.use ::Middleman::Middleware::InlineURLRewriter,
            id: :asset_host,
            url_extensions: options.exts,
            source_extensions: options.sources,
            ignore: options.ignore,
            middleman_app: app,
            proc: method(:rewrite_url)
  end

  Contract String, Or[String, Pathname], Any => String
  def rewrite_url(asset_path, dirpath, _request_path)
    uri = ::Addressable::URI.parse(asset_path)
    relative_path = uri.path[0..0] != '/'

    full_asset_path = if relative_path
      dirpath.join(asset_path).to_s
    else
      asset_path
    end

    asset_prefix = if options[:host].is_a?(Proc)
      options[:host].call(full_asset_path)
    elsif options[:host].is_a?(String)
      options[:host]
    end

    File.join(asset_prefix, full_asset_path)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
middleman-core-4.0.0.beta.2 lib/middleman-core/extensions/asset_host.rb
middleman-core-4.0.0.beta.1 lib/middleman-core/extensions/asset_host.rb