Class: Sprockets::StaticCompiler
- Inherits:
-
Object
- Object
- Sprockets::StaticCompiler
- Defined in:
- lib/sprockets/static_compiler.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
Returns the value of attribute env.
-
#paths ⇒ Object
Returns the value of attribute paths.
-
#target ⇒ Object
Returns the value of attribute target.
Instance Method Summary collapse
- #aliased_path_for(logical_path) ⇒ Object
- #compile ⇒ Object
-
#initialize(env, target, paths, options = {}) ⇒ StaticCompiler
constructor
A new instance of StaticCompiler.
- #path_for(asset) ⇒ Object
- #write_asset(asset) ⇒ Object
- #write_manifest(manifest) ⇒ Object
Constructor Details
#initialize(env, target, paths, options = {}) ⇒ StaticCompiler
Returns a new instance of StaticCompiler.
7 8 9 10 11 12 13 14 |
# File 'lib/sprockets/static_compiler.rb', line 7 def initialize(env, target, paths, = {}) @env = env @target = target @paths = paths @digest = .key?(:digest) ? .delete(:digest) : true @manifest = .key?(:manifest) ? .delete(:manifest) : true @manifest_path = .delete(:manifest_path) || target end |
Instance Attribute Details
#env ⇒ Object
Returns the value of attribute env.
5 6 7 |
# File 'lib/sprockets/static_compiler.rb', line 5 def env @env end |
#paths ⇒ Object
Returns the value of attribute paths.
5 6 7 |
# File 'lib/sprockets/static_compiler.rb', line 5 def paths @paths end |
#target ⇒ Object
Returns the value of attribute target.
5 6 7 |
# File 'lib/sprockets/static_compiler.rb', line 5 def target @target end |
Instance Method Details
#aliased_path_for(logical_path) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/sprockets/static_compiler.rb', line 48 def aliased_path_for(logical_path) if File.basename(logical_path).start_with?('index') logical_path.sub(/\/index([^\/]+)$/, '\1') else logical_path.sub(/\.([^\/]+)$/, '/index.\1') end end |
#compile ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/sprockets/static_compiler.rb', line 16 def compile manifest = {} env.each_logical_path(paths) do |logical_path| if asset = env.find_asset(logical_path) digest_path = write_asset(asset) manifest[asset.logical_path] = digest_path manifest[aliased_path_for(asset.logical_path)] = digest_path end end write_manifest(manifest) if @manifest end |
#path_for(asset) ⇒ Object
44 45 46 |
# File 'lib/sprockets/static_compiler.rb', line 44 def path_for(asset) @digest ? asset.digest_path : asset.logical_path end |
#write_asset(asset) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/sprockets/static_compiler.rb', line 35 def write_asset(asset) path_for(asset).tap do |path| filename = File.join(target, path) FileUtils.mkdir_p File.dirname(filename) asset.write_to(filename) asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/ end end |
#write_manifest(manifest) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/sprockets/static_compiler.rb', line 28 def write_manifest(manifest) FileUtils.mkdir_p(@manifest_path) File.open("#{@manifest_path}/manifest.yml", 'wb') do |f| YAML.dump(manifest, f) end end |