Sha256: 7e752cd1e18daed7f10e381687b03a49d999e82d075684408853c010f72dc2b2

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true
require 'set'

module Sprockets
  class SourceMapProcessor
    def self.call(input)
      case input[:content_type]
      when "application/js-sourcemap+json"
        accept = "application/javascript"
      when "application/css-sourcemap+json"
        accept = "text/css"
      else
        fail input[:content_type]
      end

      links = Set.new(input[:metadata][:links])

      env = input[:environment]

      uri, _  = env.resolve!(input[:filename], accept: accept)
      asset   = env.load(uri)
      map     = asset.metadata[:map] || []
      sources = asset.metadata[:sources]

      # TODO: Because of the default piplene hack we have to apply dependencies
      #       from compiled asset to the source map, otherwise the source map cache
      #       will never detect the changes from directives
      dependencies = Set.new(input[:metadata][:dependencies])
      dependencies.merge(asset.metadata[:dependencies])

      map.map { |m| m[:source] }.uniq.compact.each do |source|
        # TODO: Resolve should expect fingerprints
        fingerprint = source[/-([0-9a-f]{7,128})\.[^.]+\z/, 1]
        if fingerprint
          path = source.sub("-#{fingerprint}", "")
        else
          path = source
        end
        uri, _ = env.resolve!(path)
        links << uri
      end

      json = env.encode_json_source_map(map, sources: sources, filename: asset.logical_path)

      { data: json, links: links, dependencies: dependencies }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sprockets-4.0.0.beta4 lib/sprockets/source_map_processor.rb