Class: Sprockets::StaticCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/sprockets/static_compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, target, options = {}) ⇒ StaticCompiler

Returns a new instance of StaticCompiler.



8
9
10
11
12
# File 'lib/sprockets/static_compiler.rb', line 8

def initialize(env, target, options = {})
  @env = env
  @target = target
  @digest = options.key?(:digest) ? options.delete(:digest) : true
end

Instance Attribute Details

#digestObject

Returns the value of attribute digest.



6
7
8
# File 'lib/sprockets/static_compiler.rb', line 6

def digest
  @digest
end

#envObject

Returns the value of attribute env.



6
7
8
# File 'lib/sprockets/static_compiler.rb', line 6

def env
  @env
end

#targetObject

Returns the value of attribute target.



6
7
8
# File 'lib/sprockets/static_compiler.rb', line 6

def target
  @target
end

Instance Method Details

#compile(asset) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/sprockets/static_compiler.rb', line 25

def compile(asset)
  asset_path = digest_asset(asset)
  filename = File.join(target, asset_path)
  FileUtils.mkdir_p File.dirname(filename)
  asset.write_to(filename)
  asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
  asset_path
end

#digest_asset(asset) ⇒ Object



47
48
49
# File 'lib/sprockets/static_compiler.rb', line 47

def digest_asset(asset)
  digest ? asset.digest_path : asset.logical_path
end

#precompile(paths) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/sprockets/static_compiler.rb', line 14

def precompile(paths)
  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

#precompile_path?(logical_path, paths) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sprockets/static_compiler.rb', line 34

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