asset 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +7 -1
- data/README.md +28 -19
- data/asset.gemspec +2 -2
- data/lib/asset.rb +22 -12
- data/lib/assets/item.rb +34 -25
- data/lib/assets/router.rb +8 -8
- data/lib/assets/util.rb +7 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 932eaabc18bcabdc15594baf1ad4efd8232b0183
|
4
|
+
data.tar.gz: 096e286dca5b2889169391a0877f66a0fe34482d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 741dff30d58e04a02d311ec51c65952e1bb49c23c246bb8ac6238f5fd18cd1dd8ec9321f6d1a437d38182cf32ba16791f380f56adf551d3636393972224b7418
|
7
|
+
data.tar.gz: ddfc8b5a2f2c5f730a368f486bd7cb3a5b5faa6114d5b1933de085542375295abd7a8021ede99fb7c883906dedd3c7836b83cffee326b121dda06adbe8cf727a
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -11,18 +11,31 @@ gem install asset
|
|
11
11
|
or add to Gemfile. In your Rack app include the line
|
12
12
|
```ruby
|
13
13
|
# Rack apps
|
14
|
-
include Asset::
|
14
|
+
include Asset::Router
|
15
15
|
|
16
16
|
# Sinatra
|
17
|
-
helpers Asset::
|
17
|
+
helpers Asset::Router
|
18
18
|
```
|
19
19
|
|
20
20
|
### Settings
|
21
21
|
```ruby
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
# Default is development
|
23
|
+
@mode = ENV['RACK_ENV'] || 'development'
|
24
|
+
|
25
|
+
# Where your assets live
|
26
|
+
@path = File.join(Dir.pwd, 'app', 'assets')
|
27
|
+
|
28
|
+
# Where to write the cache, default to APP_ROOT/tmp
|
29
|
+
@cache = File.join(Dir.pwd, 'tmp')
|
30
|
+
|
31
|
+
# Automatically bounce (404) for browser /favicon.ico requests
|
32
|
+
@favicon = true
|
33
|
+
|
34
|
+
# Send /robots.txt to a standard robots txt with reference to /sitemap.xml
|
35
|
+
@robots = true
|
36
|
+
|
37
|
+
# Debug option
|
38
|
+
@debug = false
|
26
39
|
```
|
27
40
|
|
28
41
|
### Usage
|
@@ -42,11 +55,14 @@ js:
|
|
42
55
|
# Example with options:
|
43
56
|
css:
|
44
57
|
- app.css:
|
58
|
+
# Compress = false will prevent the file from being compressed (default true)
|
45
59
|
- compress: true
|
46
|
-
|
60
|
+
# This will not be part of the bundle.js if bundle = false (default true)
|
61
|
+
- bundle: true
|
47
62
|
- themes/themes.css
|
48
63
|
|
49
64
|
js:
|
65
|
+
# The same options apply to JS files
|
50
66
|
- app.js:
|
51
67
|
- bundle: false
|
52
68
|
- lib/cookie.js
|
@@ -60,11 +76,11 @@ js:
|
|
60
76
|
# Multiple
|
61
77
|
<%= script_tag 'app.js', 'lib/cookies.js' %>
|
62
78
|
|
63
|
-
# Bundle all files with '
|
64
|
-
<%= script_tag '
|
79
|
+
# Bundle all Javscript files with 'bundle.js'
|
80
|
+
<%= script_tag 'bundle.js' %>
|
65
81
|
|
66
|
-
# Bundle all files with '
|
67
|
-
<%= script_tag '
|
82
|
+
# Bundle all CSS files with 'bundle.css'
|
83
|
+
<%= script_tag 'bundle.css' %>
|
68
84
|
```
|
69
85
|
|
70
86
|
In development mode, all files will be printed. In production mode, you'll get only one file.
|
@@ -111,15 +127,8 @@ The helpers will generate these URLs, so you don't have to worry about it.
|
|
111
127
|
|
112
128
|
### Images and fonts
|
113
129
|
|
114
|
-
To include support for other static file types likes images and fonts, use [Rack::Static](https://github.com/rack/rack/blob/master/lib/rack/static.rb)
|
130
|
+
To include support for other static file types likes images and fonts, use [Rack::Static,](https://github.com/rack/rack/blob/master/lib/rack/static.rb) see the example above.
|
115
131
|
|
116
|
-
```ruby
|
117
|
-
use Rack::Static, :urls => ['/images', '/fonts'], :root => APP_ASSETS,
|
118
|
-
:header_rules => [
|
119
|
-
[:all, {'Cache-Control' => 'public, max-age=31536000'}],
|
120
|
-
[:fonts, {'Access-Control-Allow-Origin' => '*'}]
|
121
|
-
]
|
122
|
-
```
|
123
132
|
|
124
133
|
We've included an image tag helper too for this use:
|
125
134
|
```erb
|
data/asset.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'asset'
|
3
|
-
s.version = '0.0
|
4
|
-
s.date = '2017-01-
|
3
|
+
s.version = '0.1.0'
|
4
|
+
s.date = '2017-01-07'
|
5
5
|
s.summary = "Compress and serve your CSS and JS automatically"
|
6
6
|
s.description = "The only thing you need for your assets."
|
7
7
|
s.authors = ["Fugroup Limited"]
|
data/lib/asset.rb
CHANGED
@@ -1,22 +1,32 @@
|
|
1
1
|
require 'rack'
|
2
|
-
|
2
|
+
require 'yaml'
|
3
|
+
autoload :Uglifier, 'uglifier'
|
4
|
+
autoload :Tilt, 'tilt'
|
5
|
+
|
6
|
+
# Asset packer, middleware and helpers
|
7
|
+
# @homepage: https://github.com/fugroup/asset
|
8
|
+
# @author: Vidar <vidar@fugroup.net>, Fugroup Ltd.
|
9
|
+
# @license: MIT, contributions are welcome.
|
3
10
|
module Asset
|
11
|
+
class << self; attr_accessor :mode, :path, :cache, :favicon, :robots, :manifest, :debug; end
|
4
12
|
|
5
|
-
#
|
6
|
-
# Asset packer, middleware and helpers
|
7
|
-
# @homepage: https://github.com/fugroup/asset
|
8
|
-
# @author: Vidar <vidar@fugroup.net>, Fugroup Ltd.
|
9
|
-
# @license: MIT, contributions are welcome.
|
10
|
-
# # # # # #
|
11
|
-
|
12
|
-
class << self; attr_accessor :mode, :path, :cache, :manifest, :debug; end
|
13
|
-
|
14
|
-
# Default is production
|
13
|
+
# Default is development
|
15
14
|
@mode = ENV['RACK_ENV'] || 'development'
|
15
|
+
|
16
|
+
# Where your assets live
|
16
17
|
@path = File.join(Dir.pwd, 'app', 'assets')
|
18
|
+
|
19
|
+
# Where to write the cache, default to APP_ROOT/tmp
|
17
20
|
@cache = File.join(Dir.pwd, 'tmp')
|
18
|
-
@debug = false
|
19
21
|
|
22
|
+
# Automatically bounce (404) for browser /favicon.ico requests
|
23
|
+
@favicon = true
|
24
|
+
|
25
|
+
# Send /robots.txt to a standard robots txt with reference to /sitemap.xml
|
26
|
+
@robots = true
|
27
|
+
|
28
|
+
# Debug option
|
29
|
+
@debug = false
|
20
30
|
end
|
21
31
|
|
22
32
|
require_relative 'assets/util'
|
data/lib/assets/item.rb
CHANGED
@@ -1,51 +1,55 @@
|
|
1
1
|
module Asset
|
2
2
|
class Item
|
3
3
|
|
4
|
-
attr_accessor :path, :type, :key, :modified, :compress, :bundle, :name
|
4
|
+
attr_accessor :path, :type, :key, :modified, :compress, :bundle, :app, :name, :kpath
|
5
5
|
|
6
6
|
# Init
|
7
7
|
def initialize(*args)
|
8
8
|
@path, @type, @key, @modified, @compress, @bundle = args
|
9
|
+
@app = !!(@path =~ /^bundle\.(js|css)$/)
|
9
10
|
@name = @path.rpartition('.')[0]
|
11
|
+
@kpath = "#{@name}-#{kext}"
|
10
12
|
end
|
11
13
|
|
12
|
-
#
|
13
|
-
def
|
14
|
-
@
|
14
|
+
# Get the files for this item
|
15
|
+
def files
|
16
|
+
(@app and !p?) ? bundle_files : [p? ? @kpath : @path]
|
15
17
|
end
|
16
18
|
|
17
|
-
# Get the files
|
18
|
-
def
|
19
|
-
|
20
|
-
[keyed ? file : path]
|
21
|
-
else
|
22
|
-
::Asset.manifest.select{|i| i.type == @type and i.file?}.map{|i| keyed ? i.file : i.path}
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
# Get the file, meaning the full path with key
|
27
|
-
def file
|
28
|
-
::Asset.mode == 'development' ? @path : %{#{file? ? @name : 'application'}-#{@key}.#{@type}}
|
19
|
+
# Get the files for the bundle
|
20
|
+
def bundle_files
|
21
|
+
@bundle_files ||= ::Asset.manifest.select{|i| i.type == @type and i.bundle and !i.app}.map{|i| p? ? i.kpath : i.path}
|
29
22
|
end
|
30
23
|
|
31
24
|
# Get the content. Pass cache = false to fetch from disk instead of the cache.
|
32
|
-
def content(
|
33
|
-
|
34
|
-
|
35
|
-
File.read(File.join(::Asset.cache, %{#{@key}.#{@type}})).tap{|f| return f if f} rescue nil
|
25
|
+
def content(key = nil)
|
26
|
+
(!key or !@compress) ? (@joined ||= joined) : (@cached ||= cached)
|
27
|
+
end
|
36
28
|
|
37
|
-
|
29
|
+
# The cached content
|
30
|
+
def cached
|
31
|
+
File.read(cache_path).tap{|f| return f if f} rescue nil
|
38
32
|
compressed.tap{|r| write_cache(r)}
|
39
33
|
end
|
40
34
|
|
41
35
|
# Store in cache
|
42
36
|
def write_cache(r)
|
43
|
-
File.open(
|
37
|
+
File.open(cache_path, 'w'){|f| f.write(r)}
|
38
|
+
end
|
39
|
+
|
40
|
+
# Cache path
|
41
|
+
def cache_path
|
42
|
+
@cache_path ||= File.join(::Asset.cache, kext)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Key and extension
|
46
|
+
def kext
|
47
|
+
@kext ||= %{#{@key}.#{@type}}
|
44
48
|
end
|
45
49
|
|
46
50
|
# Compressed joined files
|
47
51
|
def compressed
|
48
|
-
case @type
|
52
|
+
@compressed ||= case @type
|
49
53
|
when 'css'
|
50
54
|
Tilt.new('scss', :style => :compressed){ joined }.render rescue joined
|
51
55
|
when 'js'
|
@@ -55,12 +59,17 @@ module Asset
|
|
55
59
|
|
56
60
|
# All files joined
|
57
61
|
def joined
|
58
|
-
@joined ||= files
|
62
|
+
@joined ||= files.map{|f| File.read(File.join(::Asset.path, @type, f))}.join
|
63
|
+
end
|
64
|
+
|
65
|
+
# Production mode?
|
66
|
+
def p?
|
67
|
+
::Asset.mode == 'production'
|
59
68
|
end
|
60
69
|
|
61
70
|
# Print data
|
62
71
|
def print
|
63
|
-
[:path, :type, :key, :name, :modified, :files, :content].
|
72
|
+
[:path, :type, :key, :name, :modified, :files, :content].map{|r| "#{r.upcase}: #{send(r).inspect}"}.join("\n")
|
64
73
|
end
|
65
74
|
|
66
75
|
end
|
data/lib/assets/router.rb
CHANGED
@@ -24,23 +24,23 @@ module Asset
|
|
24
24
|
# Match /assets?/:type/path
|
25
25
|
when /^(\/assets)?\/(js|css)\/(.+)/
|
26
26
|
type, path = $2, $3
|
27
|
+
path =~ /-([a-f0-9]{1,32})/
|
28
|
+
path.gsub!("-#{@key}", '') if (@key = $1)
|
27
29
|
|
28
|
-
|
29
|
-
path = path.gsub($1, '') if $1
|
30
|
-
|
30
|
+
# Find the item
|
31
31
|
item = ::Asset.manifest.find{|i| i.path == path and i.type == type}
|
32
32
|
|
33
33
|
# Not found if no item, wrong key or no content
|
34
|
-
return not_found if !item or (
|
34
|
+
return not_found if !item or (@key and @key != item.key) or !item.content(@key)
|
35
35
|
|
36
36
|
found(item)
|
37
37
|
|
38
38
|
# Bounce favicon requests
|
39
|
-
when /^\/favicon\.ico$/
|
39
|
+
when (::Asset.favicon and /^\/favicon\.ico$/)
|
40
40
|
not_found
|
41
41
|
|
42
42
|
# Return a standard robots.txt
|
43
|
-
when /^\/robots\.txt$/
|
43
|
+
when (::Asset.robots and /^\/robots\.txt$/)
|
44
44
|
robots
|
45
45
|
|
46
46
|
else
|
@@ -54,11 +54,11 @@ module Asset
|
|
54
54
|
# Found
|
55
55
|
def found(item)
|
56
56
|
[ 200, {'Content-Type' => MIME[item.type],
|
57
|
-
'Content-Length' => item.content.size,
|
57
|
+
'Content-Length' => item.content(@key).size,
|
58
58
|
'Cache-Control' => 'max-age=86400, public',
|
59
59
|
'Expires' => (Time.now + 86400*30).utc.rfc2822,
|
60
60
|
'Last-Modified' => item.modified.utc.rfc2822
|
61
|
-
}, [item.content]]
|
61
|
+
}, [item.content(@key)]]
|
62
62
|
end
|
63
63
|
|
64
64
|
# Not found
|
data/lib/assets/util.rb
CHANGED
@@ -3,16 +3,16 @@ module Asset
|
|
3
3
|
|
4
4
|
# Get timestamp
|
5
5
|
def self.mtime(path)
|
6
|
-
File.mtime(asset_path(path))
|
6
|
+
File.mtime(asset_path(path)).utc
|
7
7
|
end
|
8
8
|
|
9
9
|
# Asset path
|
10
10
|
def self.asset_path(path)
|
11
|
-
File.join(::Asset.path,
|
11
|
+
File.join(::Asset.path, path)
|
12
12
|
end
|
13
13
|
|
14
14
|
# Digest
|
15
|
-
def self.digest(path, time)
|
15
|
+
def self.digest(path, time = mtime(path))
|
16
16
|
Digest::MD5.hexdigest(%{#{path}#{time.to_i}})
|
17
17
|
end
|
18
18
|
|
@@ -32,7 +32,7 @@ module Asset
|
|
32
32
|
path = h ? name.keys[0] : name
|
33
33
|
|
34
34
|
# Get the modified time of the asset
|
35
|
-
modified = mtime("#{type}/#{path}")
|
35
|
+
modified = mtime("#{type}/#{path}")
|
36
36
|
|
37
37
|
# Record max to know the latest change
|
38
38
|
max = modified if modified > max
|
@@ -43,10 +43,10 @@ module Asset
|
|
43
43
|
(h ? name['compress'] : true), (h ? name['bundle'] : true))
|
44
44
|
end
|
45
45
|
|
46
|
-
# Insert the
|
46
|
+
# Insert the bundle
|
47
47
|
manifest.insert(0, ::Asset::Item.new(
|
48
|
-
"
|
49
|
-
digest("
|
48
|
+
"bundle.#{type}", type,
|
49
|
+
digest("bundle.#{type}", max), max))
|
50
50
|
end
|
51
51
|
manifest
|
52
52
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: asset
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fugroup Limited
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|