Guest User

Untitled

a guest
Jan 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. module Sprockets
  2. autoload :Helpers, "sprockets/helpers"
  3. autoload :LazyCompressor, "sprockets/compressors"
  4. autoload :NullCompressor, "sprockets/compressors"
  5.  
  6. # TODO: Get rid of config.assets.enabled
  7. class Railtie < ::Rails::Railtie
  8. config.default_asset_host_protocol = :relative
  9.  
  10. rake_tasks do
  11. load "sprockets/assets.rake"
  12. end
  13.  
  14. initializer "sprockets.environment" do |app|
  15. config = app.config
  16. next unless config.assets.enabled
  17.  
  18. require 'sprockets'
  19.  
  20. app.assets = Sprockets::Environment.new(app.root.to_s) do |env|
  21. env.logger = ::Rails.logger
  22. env.version = ::Rails.env + "-#{config.assets.version}"
  23.  
  24. if config.assets.cache_store != false
  25. env.cache = ActiveSupport::Cache.lookup_store(config.assets.cache_store) || ::Rails.cache
  26. end
  27. end
  28.  
  29. if config.assets.manifest
  30. path = File.join(config.assets.manifest, "manifest.yml")
  31. else
  32. path = File.join(Rails.public_path, config.assets.prefix, "manifest.yml")
  33. end
  34.  
  35. if File.exist?(path)
  36. config.assets.digests = YAML.load_file(path)
  37. end
  38.  
  39. ActiveSupport.on_load(:action_view) do
  40. include ::Sprockets::Helpers::RailsHelper
  41.  
  42. app.assets.context_class.instance_eval do
  43. include ::Sprockets::Helpers::RailsHelper
  44. end
  45. end
  46. end
  47.  
  48. # We need to configure this after initialization to ensure we collect
  49. # paths from all engines. This hook is invoked exactly before routes
  50. # are compiled, and so that other Railties have an opportunity to
  51. # register compressors.
  52. config.after_initialize do |app|
  53. next unless app.assets
  54. config = app.config
  55.  
  56. config.assets.paths.each { |path| app.assets.append_path(path) }
  57.  
  58. if config.assets.compress
  59. # temporarily hardcode default JS compressor to uglify. Soon, it will work
  60. # the same as SCSS, where a default plugin sets the default.
  61. unless config.assets.js_compressor == false
  62. app.assets.js_compressor = LazyCompressor.new { expand_js_compressor(config.assets.js_compressor || :uglifier) }
  63. end
  64.  
  65. unless config.assets.css_compressor == false
  66. app.assets.css_compressor = LazyCompressor.new { expand_css_compressor(config.assets.css_compressor) }
  67. end
  68. end
  69.  
  70. if config.assets.compile
  71. app.routes.prepend do
  72. mount app.assets => config.assets.prefix
  73. end
  74.  
  75. if config.action_controller.perform_caching
  76. app.assets = app.assets.index
  77. end
  78. end
  79. end
  80.  
  81. protected
  82. def expand_js_compressor(sym)
  83. case sym
  84. when :closure
  85. require 'closure-compiler'
  86. Closure::Compiler.new
  87. when :uglifier
  88. require 'uglifier'
  89. Uglifier.new
  90. when :yui
  91. require 'yui/compressor'
  92. YUI::JavaScriptCompressor.new
  93. else
  94. sym
  95. end
  96. end
  97.  
  98. def expand_css_compressor(sym)
  99. case sym
  100. when :yui
  101. require 'yui/compressor'
  102. YUI::CssCompressor.new
  103. else
  104. sym
  105. end
  106. end
  107. end
  108. end
Add Comment
Please, Sign In to add comment