class Jekyll::Assets::Plugins::SrcMap::Writer

Public Instance Methods

call() click to toggle source

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 21
def call
  clean_file!
  clean_sources!
  write_map!
  write_src!
end
files() click to toggle source

– Provides our custom manifest key, full of files. @note We push everything from the file we are writing to the maps. @return [Array<String>] –

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 33
def files
  @files ||= begin
    key = "sourceMapFiles"
    out = env.manifest.data[key] ||= []
    unless Manifest.keep_keys.include?(key)
      Manifest.keep_keys << key
    end

    out
  end
end
skip?(_) click to toggle source
# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 15
def skip?(_)
  !env.asset_config[:source_maps] ||
    !asset.metadata[:map]
end

Private Instance Methods

asset_path() click to toggle source

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 166
def asset_path
  base.join(env.strip_paths(@asset.filename)).to_s
end
base() click to toggle source

– @note we shim this on name. Privates the base directory in the source. @return [String] –

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 110
def base
  Pathutil.new(asset.filename.sub(env.jekyll
    .in_source_dir + "/", "")).dirname
end
clean_file!() click to toggle source

– @note something like _assets/* Makes sure that the name sits in the asset path. @return [String] –

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 73
def clean_file!
  map[:file] = base.join(map[:file]).to_s
end
clean_sources!() click to toggle source

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 79
def clean_sources!
  if map[:sources]
    then map[:sources] = map[:sources].map do |v|
      base.join(strip_src(v))
    end
  else
    map[:sections].each do |v|
      v[:map][:sources] = v[:map][:sources].map do |vv|
        base.join(strip_src(vv))
      end
    end
  end

  map
end
map() click to toggle source

– @return [HashWithIndifferentAccess] @note do not modify the original map. Provides a modifible SourceMap –

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 51
def map
  @map ||= asset.metadata[:map]
    .with_indifferent_access
end
map_files() click to toggle source

– rubocop:disable Layout/BlockEndNewline rubocop:disable Layout/MultilineBlockLayout rubocop:disable Style/BlockDelimiters –

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 121
def map_files
  return original_map[:sources] if original_map.key?(:sources)
  original_map[:sections].map { |v| v[:map][:sources] \
    if v.key?(:map) }.flatten.compact
end
map_path(file) click to toggle source

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 172
def map_path(file)
  asset = base.join(env.strip_paths(file))
  SrcMap.path({
    asset: asset, env: env
  })
end
original_map() click to toggle source

– @return [HashWithIndifferentAccess] @note this is frozen so you can't modify. Provides an unmodifiable SourceMap –

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 62
def original_map
  @original_map ||= asset.metadata[:map]
    .with_indifferent_access.freeze
end
strip_base(asset) click to toggle source

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 144
def strip_base(asset)
  return asset if asset.is_a?(Sprockets::Asset)
  asset.sub(base + "/", "")
end
strip_src(path) click to toggle source

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 97
def strip_src(path)
  path = Pathutil.new(path)
  base = path.basename.gsub(%r!\.source!, "")
  return path.dirname.join(base).to_s unless path.dirname == "."
  base.to_s if path.dirname == "."
end
write_map!() click to toggle source

– rubocop:enable Layout/BlockEndNewline rubocop:enable Layout/MultilineBlockLayout rubocop:enable Style/BlockDelimiters –

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 133
def write_map!
  path = SrcMap.map_path(asset: asset, env: env)
  write(env.in_dest_dir(path)) do |f|
    files.push(path)
    f.write(map.to_json)
    files.uniq!
  end
end
write_src!() click to toggle source

# File lib/jekyll/assets/plugins/srcmap/writer.rb, line 151
def write_src!
  [asset_path, map_files].flatten.compact.uniq.each do |v|
    next unless (v = env.find_asset(strip_base(v), pipeline: :source))
    path = map_path(v.filename)

    write(environment.in_dest_dir(path)) do |f|
      f.write(v.source)
      files.push(path.to_s)
        .uniq!
    end
  end
end