Skip to content

Instantly share code, notes, and snippets.

@pftg
Last active December 17, 2015 04:09
Show Gist options
  • Save pftg/5548479 to your computer and use it in GitHub Desktop.
Save pftg/5548479 to your computer and use it in GitHub Desktop.
spocket-rails refactoring
# sprockets-rails/lib/sprockets/rails/task.rb
require 'rake'
require 'rake/sprocketstask'
require 'sprockets'
module Sprockets
module Rails
class Task < Rake::SprocketsTask
attr_accessor :app
def environment
app.assets
end
def assets
app.config.assets.precompile
end
def output
app.config.root.join('public', app.config.assets.prefix).to_s
end
def cache_path
app.config.root.join(%w(tmp cache assets)).to_s
end
def initialize(app = nil)
self.app = app
super
end
def define
namespace :assets do
# Override this task change the loaded dependencies
desc "Load asset compile environment"
task :environment do
# Load full Rails environment by default
Rake::Task['environment'].invoke
end
desc "Compile all the assets named in config.assets.precompile"
task :precompile => :environment do
with_logger do
manifest.compile(assets)
end
end
desc "Remove old compiled assets"
task :clean => :environment do
with_logger do
manifest.clean(keep)
end
end
desc "Remove compiled assets"
task :clobber => :environment do
with_logger do
manifest.clobber
rm_rf cache_path if cache_path
end
end
end
end
end
end
end
# sprockets-rails/lib/sprockets/railtie.rb
require 'rails'
require 'rails/railtie'
require 'action_controller/railtie'
require 'active_support/core_ext/module/remove_method'
require 'sprockets'
require 'sprockets/rails/helper'
module Rails
class Application
# Hack: We need to remove Rails' built in config.assets so we can
# do our own thing.
class Configuration
remove_possible_method :assets
end
# Undefine Rails' assets method before redefining it, to avoid warnings.
remove_possible_method :assets
remove_possible_method :assets=
# Returns Sprockets::Environment for app config.
def assets
@assets ||= Sprockets::Environment.new(root.to_s) do |env|
env.version = ::Rails.env
path = "#{config.root}/tmp/cache/assets/#{::Rails.env}"
env.cache = Sprockets::Cache::FileStore.new(path)
env.context_class.class_eval do
include ::Sprockets::Rails::Helper
end
end
end
attr_writer :assets
end
end
module Sprockets
class Railtie < ::Rails::Railtie
LOOSE_APP_ASSETS = lambda do |path, filename|
filename =~ /app\/assets/ && !%w(.js .css).include?(File.extname(path))
end
class OrderedOptions < ActiveSupport::OrderedOptions
def configure(&block)
self._blocks << block
end
end
config.assets = OrderedOptions.new
config.assets._blocks = []
config.assets.paths = []
config.assets.prefix = "/assets"
config.assets.precompile = [LOOSE_APP_ASSETS, /(?:\/|\\|\A)application\.(css|js)$/]
config.assets.version = ""
config.assets.debug = false
config.assets.compile = true
config.assets.digest = false
rake_tasks do |app|
require 'sprockets/rails/task'
Sprockets::Rails::Task.new(app)
# Another version
# Sprockets::Rails::Task.new do |t|
# t.app = app
# end
end
config.after_initialize do |app|
config = app.config
manifest_path = File.join(app.root, 'public', config.assets.prefix)
unless config.assets.version.blank?
app.assets.version += "-#{config.assets.version}"
end
# Copy config.assets.paths to Sprockets
config.assets.paths.each do |path|
app.assets.append_path path
end
ActionView::Base.instance_eval do
include Sprockets::Rails::Helper
# Copy relevant config to AV context
self.debug_assets = config.assets.debug
self.digest_assets = config.assets.digest
self.assets_prefix = config.assets.prefix
# Copy over to Sprockets as well
context = app.assets.context_class
context.assets_prefix = config.assets.prefix
context.digest_assets = config.assets.digest
context.config = config.action_controller
if config.assets.compile
self.assets_environment = app.assets
self.assets_manifest = Sprockets::Manifest.new(app.assets, manifest_path)
else
self.assets_manifest = Sprockets::Manifest.new(manifest_path)
end
end
app.assets.js_compressor = config.assets.js_compressor
app.assets.css_compressor = config.assets.css_compressor
# Run app.assets.configure blocks
config.assets._blocks.each do |block|
block.call app.assets
end
# No more configuration changes at this point.
# With cache classes on, Sprockets won't check the FS when files
# change. Preferable in production when the FS only changes on
# deploys when the app restarts.
if config.cache_classes
app.assets = app.assets.index
end
if config.assets.compile
if app.routes.respond_to?(:prepend)
app.routes.prepend do
mount app.assets => config.assets.prefix
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment