This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
forked from rails/sprockets
/
static_compilation.rb
78 lines (65 loc) · 2.22 KB
/
static_compilation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'sprockets/static_asset'
require 'fileutils'
require 'pathname'
module Sprockets
# `Caching` is an internal mixin whose public methods are exposed on
# the `Environment` and `Index` classes.
module StaticCompilation
# `static_root` is a special path where compiled assets are served
# from. This is usually set to a `/public` or `/static` directory.
#
# In a production environment, Apache or nginx should be
# configured to serve assets from the directory.
def static_root
@static_root ||= nil
end
# Assign a static root directory.
def static_root=(root)
expire_index!
warn "Sprockets::Environment#static_root is deprecated"
@static_root = root ? Pathname.new(root) : nil
end
# `precompile` takes a like of paths, globs, or `Regexp`s to
# compile into `static_root`.
#
# precompile "application.js", "*.css", /.+\.(png|jpg)/
#
# This usually ran via a rake task.
def precompile(*paths)
options = paths.last.is_a?(Hash) ? paths.pop : {}
warn "Sprockets::Environment#precompile is deprecated"
if options[:to]
target = options[:to]
elsif static_root
warn "Sprockets::Environment#static_root is deprecated"
target = static_root
else
raise ArgumentError, "missing target"
end
target = Pathname.new(target)
manifest = {}
paths.each do |path|
each_logical_path do |logical_path|
if path.is_a?(Regexp)
# Match path against `Regexp`
next unless path.match(logical_path)
else
# Otherwise use fnmatch glob syntax
next unless File.fnmatch(path.to_s, logical_path)
end
if asset = find_asset(logical_path)
manifest[logical_path] = asset.digest_path
filename = target.join(asset.digest_path)
# Ensure directory exists
FileUtils.mkdir_p filename.dirname
# Write file
asset.write_to(filename)
# Write compressed file if its a bundled asset like .js or .css
asset.write_to("#{filename}.gz") if asset.is_a?(BundledAsset)
end
end
end
manifest
end
end
end