mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Move precompile task to Sprockets::StaticCompiler
This commit is contained in:
parent
7dbf6960c0
commit
857d20efda
3 changed files with 56 additions and 22 deletions
|
@ -20,30 +20,10 @@ namespace :assets do
|
||||||
config = Rails.application.config
|
config = Rails.application.config
|
||||||
env = Rails.application.assets
|
env = Rails.application.assets
|
||||||
target = Pathname.new(File.join(Rails.public_path, config.assets.prefix))
|
target = Pathname.new(File.join(Rails.public_path, config.assets.prefix))
|
||||||
manifest = {}
|
|
||||||
manifest_path = config.assets.manifest || target
|
manifest_path = config.assets.manifest || target
|
||||||
|
|
||||||
config.assets.precompile.each do |path|
|
static_compiler = Sprockets::StaticCompiler.new(env, target, :digest => config.assets.digest)
|
||||||
env.each_logical_path do |logical_path|
|
manifest = static_compiler.precompile(config.assets.precompile)
|
||||||
if path.is_a?(Regexp)
|
|
||||||
next unless path.match(logical_path)
|
|
||||||
elsif path.is_a?(Proc)
|
|
||||||
next unless path.call(logical_path)
|
|
||||||
else
|
|
||||||
next unless File.fnmatch(path.to_s, logical_path)
|
|
||||||
end
|
|
||||||
|
|
||||||
if asset = env.find_asset(logical_path)
|
|
||||||
asset_path = config.assets.digest ? asset.digest_path : logical_path
|
|
||||||
manifest[logical_path] = asset_path
|
|
||||||
filename = target.join(asset_path)
|
|
||||||
|
|
||||||
mkdir_p filename.dirname
|
|
||||||
asset.write_to(filename)
|
|
||||||
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
File.open("#{manifest_path}/manifest.yml", 'wb') do |f|
|
File.open("#{manifest_path}/manifest.yml", 'wb') do |f|
|
||||||
YAML.dump(manifest, f)
|
YAML.dump(manifest, f)
|
||||||
|
|
|
@ -2,6 +2,7 @@ module Sprockets
|
||||||
autoload :Helpers, "sprockets/helpers"
|
autoload :Helpers, "sprockets/helpers"
|
||||||
autoload :LazyCompressor, "sprockets/compressors"
|
autoload :LazyCompressor, "sprockets/compressors"
|
||||||
autoload :NullCompressor, "sprockets/compressors"
|
autoload :NullCompressor, "sprockets/compressors"
|
||||||
|
autoload :StaticCompiler, "sprockets/static_compiler"
|
||||||
|
|
||||||
# TODO: Get rid of config.assets.enabled
|
# TODO: Get rid of config.assets.enabled
|
||||||
class Railtie < ::Rails::Railtie
|
class Railtie < ::Rails::Railtie
|
||||||
|
|
53
actionpack/lib/sprockets/static_compiler.rb
Normal file
53
actionpack/lib/sprockets/static_compiler.rb
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
require 'fileutils'
|
||||||
|
require 'pathname'
|
||||||
|
|
||||||
|
module Sprockets
|
||||||
|
class StaticCompiler
|
||||||
|
attr_accessor :env, :target, :digest
|
||||||
|
|
||||||
|
def initialize(env, target, options = {})
|
||||||
|
@env = env
|
||||||
|
@target = target
|
||||||
|
@digest = options.key?(:digest) ? options.delete(:digest) : true
|
||||||
|
end
|
||||||
|
|
||||||
|
def precompile(paths)
|
||||||
|
Rails.application.config.assets.digest = digest
|
||||||
|
manifest = {}
|
||||||
|
|
||||||
|
env.each_logical_path do |logical_path|
|
||||||
|
next unless precompile_path?(logical_path, paths)
|
||||||
|
if asset = env.find_asset(logical_path)
|
||||||
|
manifest[logical_path] = compile(asset)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
manifest
|
||||||
|
end
|
||||||
|
|
||||||
|
def compile(asset)
|
||||||
|
asset_path = digest_asset(asset)
|
||||||
|
filename = target.join(asset_path)
|
||||||
|
FileUtils.mkdir_p filename.dirname
|
||||||
|
asset.write_to(filename)
|
||||||
|
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
|
||||||
|
asset_path
|
||||||
|
end
|
||||||
|
|
||||||
|
def precompile_path?(logical_path, paths)
|
||||||
|
paths.each do |path|
|
||||||
|
if path.is_a?(Regexp)
|
||||||
|
return true if path.match(logical_path)
|
||||||
|
elsif path.is_a?(Proc)
|
||||||
|
return true if path.call(logical_path)
|
||||||
|
else
|
||||||
|
return true if File.fnmatch(path.to_s, logical_path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
|
def digest_asset(asset)
|
||||||
|
digest ? asset.digest_path : asset.logical_path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue