Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The State of Sprockets

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for Yuichi Goto Yuichi Goto
October 24, 2016

The State of Sprockets

Ruby on Rails Tech Meetup(2016/10/24)

Avatar for Yuichi Goto

Yuichi Goto

October 24, 2016
Tweet

More Decks by Yuichi Goto

Other Decks in Technology

Transcript

  1. Sprocketsͱ͸ "Sprockets is a Ruby library for compiling and serving

    web assets." - https://github.com/rails/sprockets/blob/3.x/README.md
  2. Serving web assets • RackΞϓϦέʔγϣϯͱ࣮ͯ͠૷ • ಛఆͷURLʹϚ΢ϯτͯ͠࢖͏
 Railsͷ৔߹: /assets ->

    /public/assets • ϑϨʔϜϫʔΫΛ໰Θͣར༻Ͱ͖Δ
 ྫ: Rails, Sinatra
  3. Sprockets 4 • 4.0.0.beta4ʢ2016/10/18ϦϦʔεʣ • ଴๬ͷ৽ػೳ 1. Source Maps 2.

    ES6 Support - https://github.com/rails/sprockets/blob/v4.0.0.beta4/UPGRADING.md
  4. ΋͏ͻͱͭͷԸܙ: Minifying • ຊ൪؀ڥͱಉ͡Α͏ʹJS/CSSΛѹॖͯ͠
 ։ൃͰ͖Δʢ΍ͬͱ͜͜·Ͱɾɾɾʂײʣ # config/environments/development.rb Rails.application.configure do #

    Compress JavaScripts and CSS. config.assets.js_compressor = :uglifier config.assets.css_compressor = :sass end production.rbͱ ಉ͡ઃఆ͕࢖͑Δ
  5. ྫ: SassTemplate # https://github.com/rails/sprockets/blob/2.x/lib/ sprockets/sass_template.rb class SassTemplate < Tilt::Template def

    evaluate(context, locals, &block) end end # https://github.com/rails/sprockets/blob/2.x/lib/ sprockets.rb register_engine '.sass', SassTemplate Tilt::TemplateΛܧঝͯ͠ #evaluateΛ࣮૷
  6. ྫ: SassProcessor # https://github.com/rails/sprockets/blob/v4.0.0.beta4/lib/sprockets/ sass_processor.rb class SassProcessor def self.instance @instance

    ||= new end def self.call(input) instance.call(input) end def call(input) end end # https://github.com/rails/sprockets/blob/v4.0.0.beta4/lib/sprockets.rb register_transformer 'text/sass', 'text/css', SassProcessor #call͸ॲཧ݁ՌΛؚΉ HashΛฦ͢Α͏ʹ࣮૷
  7. v4Ͱಥવಈ͔ͳ͘ͳͬͨ ※ ͜ΕΛػʹ৭ʑͱௐ΂ͨ͜ͱ͕ࠓճͷൃදʹܨ͕͍ͬͯ·͢ # https://github.com/yasaichi/gakubuchi/blob/v1.2.2/lib/gakubuchi/ engine_registrar.rb class EngineRegistrar def register(target,

    engine) klass = constantize(engine) return false if !klass.instance_of?(::Class) || registered? (target) @env.register_engine(target, klass) true end end # https://github.com/yasaichi/gakubuchi/blob/v1.2.2/lib/gakubuchi/ railtie.rb engine_registrar.register(:haml, '::Tilt::HamlTemplate')
  8. • Sprockets΁ͷొ࿥࣌ʹgreaseͰม׵͢Δ͚ͩ ref. https://github.com/yasaichi/grease/blob/v0.2.0/README.md ྫ: HamlΛv3Ҏ߱Ͱར༻͢Δ # For v3 register_engine

    ".haml", Grease.apply(Tilt::HamlTemplate), mime_type: "text/html", silence_deprecation: true # For v4 register_mime_type "text/haml", extensions: %w(.haml) register_transformer "text/haml", "text/html", Grease.apply(Tilt::HamlTemplate)
  9. Future: Sprockets 4 • ৽ػೳ: Source Maps, ES6 Support •

    ։ൃ؀ڥͰ΋assetͷ݁߹ɾѹॖ͕Ͱ͖Δ
 Α͏ʹͳΓɺ։ൃͷߴ଎Խ͕ظ଴͞ΕΔ • JSͷ৽͍͠࢓༷ʹ͍ͭ·Ͱ௥ैͰ͖Δ͔৺഑ ͢ΔΑ͏ͳਓ͸ͦ΋ͦ΋ର৅ʹͯ͠ͳ͍ײ