アセットパイプラインの仕組み (Sprockets)

参考:

sprockets/how_sprockets_works.md at master · rails/sprockets – GitHub

sprockets/extending_sprockets.md at master · rails/sprockets – GitHub

How the F does Sprockets Load an Asset? | Schneems

Demystifying Sprockets | byroot.github.io

Sprockets.config

参考:

sprockets/sprockets.rb at v3.7.2 · rails/sprockets – GitHub

Sprockets::Railtie

参考:

Sprockets::Railtie – sprockets-rails/railtie.rb at v3.2.1 · rails/sprockets-rails – GitHub

Sprockets::Environment

参考:

Class: Sprockets::Environment — Documentation for sprockets | RubyDoc.info

sprockets/environment.rb at v3.7.2 · rails/sprockets – GitHub

assets_environment – sprockets-rails/railtie.rb at v3.2.1 · rails/sprockets-rails – GitHub

Sprockets::Base#[]

参考:

sprockets/base.rb at v3.7.2 · rails/sprockets – GitHub

Sprockets::Asset

参考:

Class: Sprockets::Asset — Documentation for sprockets | RubyDoc.info

sprockets/asset.rb at v3.7.2 · rails/sprockets – GitHub

digest_path

参考:

digest_path – sprockets/asset.rb at v3.7.2 · rails/sprockets – GitHub

digest

参考:

hexdigest – sprockets/asset.rb at v3.7.2 · rails/sprockets – GitHub

etag – sprockets/asset.rb at v3.7.2 · rails/sprockets – GitHub

pack_hexdigest – sprockets/digest_utils.rb at v3.7.2 · rails/sprockets

file_digest / stat_digest

参考:

file_digest – sprockets/path_digest_utils.rb at v3.7.2 · rails/sprockets – GitHub

stat_digest – sprockets/path_digest_utils.rb at v3.7.2 · rails/sprockets – GitHub

find_asset

参考:

find_asset – sprockets/base.rb at v3.7.2 · rails/sprockets – GitHub

resolve

参考:

resolve – sprockets/resolve.rb at v3.7.2 · rails/sprockets – GitHub

resolve_with_compat – sprockets/legacy.rb at v3.7.2 · rails/sprockets – GitHub

load

参考:

load – sprockets/loader.rb at v3.7.2 · rails/sprockets – GitHub

load_from_unloaded – sprockets/loader.rb at v3.7.2 · rails/sprockets – GitHub

metadata[:digest]

参考:

metadata[:digest] – sprockets/loader.rb at v3.7.2 · rails/sprockets – GitHub

metadata[:digest] – sprockets/loader.rb at v3.7.2 · rails/sprockets – GitHub

digest_class

参考:

Reducing rails asset pipeline’s 64 bytes of digest size | Linux Tips

digest_class – sprockets/sprockets.rb at v3.7.2 · rails/sprockets – GitHub

digest_class – sprockets/configuration.rb at v3.7.2 · rails/sprockets – GitHub

detect_digest_class – sprockets/digest_utils.rb at v3.7.2 · rails/sprockets – GitHub

Rails asset pipeline and digest values – Stack Overflow

assets:precompile

参考:

assets:precompile – sprockets-rails/task.rb at v3.2.1 · rails/sprockets-rails – GitHub

enhance_assets_precompile

参考:

enhance_assets_precompile – webpacker/compile.rake at v4.0.0.pre.3 · rails/webpacker – GitHub

enhance_assets_precompile – webpacker/compile.rake at v4.0.0.pre.3 · rails/webpacker – GitHub

yarn:install

参考:

yarn:install – rails/yarn.rake at v5.2.2 · rails/rails – GitHub

enhance – rails/yarn.rake at v5.2.2 · rails/rails – GitHub

Sprockets::Rails::Task

参考:

sprockets-rails/task.rb at v3.2.1 · rails/sprockets-rails – GitHub

Sprockets::Rails::Task – sprockets-rails/railtie.rb at v3.2.1 · rails/sprockets-rails – GitHub

Rake::SprocketsTask

参考:

SprocketsTask – sprockets/sprocketstask.rb at v3.7.2 · rails/sprockets – GitHub

cached – sprockets/sprocketstask.rb at v3.7.2 · rails/sprockets – GitHub

with_logger – sprockets/sprocketstask.rb at v3.7.2 · rails/sprockets – GitHub

Sprockets::Manifest

参考:

sprockets/manifest.rb at v3.7.2 · rails/sprockets – GitHub

compile – sprockets/manifest.rb at v3.7.2 · rails/sprockets – GitHub

ManifestUtils

参考:

find_directory_manifest – sprockets/manifest_utils.rb at v3.7.2 · rails/sprockets – GitHub

generate_manifest_path – sprockets/manifest_utils.rb at v3.7.2 · rails/sprockets – GitHub

Sprockets::ERBProcessor

参考:

sprockets/erb_processor.rb at v3.7.2 · rails/sprockets – GitHub

asset_path

参考:

rails/asset_url_helper.rb at v5.2.2 · rails/rails – GitHub

image_path

参考:

rails/asset_url_helper.rb at v5.2.2 · rails/rails – GitHub

compute_asset_path

参考:

sprockets-rails/helper.rb at v3.2.1 · rails/sprockets-rails – GitHub

アセットをコンパイルするタスクを自前で用意する

desc "Compile assets"
task compile_assets: :environment do
  app    = Rails.application
  config = app.config
  index  = app.assets.cached

  output = File.join(
    config.paths["public"].first,
    config.assets.prefix
  )

  manifest_path = File.expand_path(
    "tmp/tmp.sprockets-manifest-#{SecureRandom.hex(16)}.json",
    Rails.root
  )

  puts "cached_env: #{index}"
  puts "output_dir: #{output}"
  puts "manifest_path: #{manifest_path}"

  manifest = Sprockets::Manifest.new(
    index,
    output,
    manifest_path
  )

  logger       = Logger.new($stderr)
  logger.level = Logger::INFO
  env          = manifest.environment
  env.logger   = logger

  assets = app.config.assets.precompile

  manifest.compile(assets)
end

アセットをコンパイルする

$ rails compile_assets

指定したアセットをコンパイルする

Sprockets::Manifest.new( \
  Rails.application.assets.cached, \
  File.join(Rails.public_path, Rails.application.config.assets.prefix), \
  Rails.application.config.assets.manifest \
).compile("path/to/asset.css")

Sprockets::Processing

  • register_pipeline
  • register_bundle_processor
  • register_preprocessor
  • register_postprocessor

参考:

sprockets/processing.rb at v3.7.2 · rails/sprockets – GitHub

Module: Sprockets::Processing — Documentation for sprockets | RubyDoc.info

Polymer on Rails, tooling the asset pipeline for performance | Cooking Healthy Code

Sprockets::Compressing

  • register_compressor
  • js_compressor=
  • css_compressor=

参考:

register_compressor – sprockets/compressing.rb at v3.7.2 · rails/sprockets – GitHub

Module: Sprockets::Compressing — Documentation for sprockets | RubyDoc.info

Sprockets::Transformers

  • register_transformer

参考:

register_transformer – sprockets/transformers.rb at v3.7.2 · rails/sprockets – GitHub

Module: Sprockets::Transformers — Documentation for sprockets | RubyDoc.info

hash_reassoc

複製したハッシュに要素を追加して、フリーズして返す。フリーズされたconfig に設定を追加するために用いる。

参考:

sprockets/utils.rb at v3.7.2 · rails/sprockets – GitHub

Tagged:

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です