asset 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4cd9e5084290d4157c2f2c45785bbc5813256273
4
- data.tar.gz: 46b793da9b9774e454134f193036381d03e459f5
3
+ metadata.gz: 932eaabc18bcabdc15594baf1ad4efd8232b0183
4
+ data.tar.gz: 096e286dca5b2889169391a0877f66a0fe34482d
5
5
  SHA512:
6
- metadata.gz: bd153573b90fbc422dadf116410462db9f06b8cbab3b0a3b6d5985d54d88b93e375819593c552d73456f41393180a9344fcab1eb215eadb5d6629bbbf62c2cb9
7
- data.tar.gz: b73c59885ceacc7fcc72aa4b628c76a816ff31e62112bf65227a41d9f03eec3da89c67f80dbfb55b9d474ec51bc29383ebb27390a51589f3e8bf504255ac3e76
6
+ metadata.gz: 741dff30d58e04a02d311ec51c65952e1bb49c23c246bb8ac6238f5fd18cd1dd8ec9321f6d1a437d38182cf32ba16791f380f56adf551d3636393972224b7418
7
+ data.tar.gz: ddfc8b5a2f2c5f730a368f486bd7cb3a5b5faa6114d5b1933de085542375295abd7a8021ede99fb7c883906dedd3c7836b83cffee326b121dda06adbe8cf727a
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  Gemfile.lock
3
3
  log
4
4
  tmp
5
+ .sass-cache
@@ -1,3 +1,9 @@
1
- **Version 0.1.0** - *2017-01-05*
1
+ **Version 0.1.0** - *2017-01-07*
2
+
3
+ - Options added for robots.txt and favicon.ico
4
+ - Compress and bundle options are working
5
+ - Fixed caching issue
6
+
7
+ **Version 0.0.3** - *2017-01-05*
2
8
 
3
9
  - Fixed gemspec issues
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::Helpers
14
+ include Asset::Router
15
15
 
16
16
  # Sinatra
17
- helpers Asset::Helpers
17
+ helpers Asset::Router
18
18
  ```
19
19
 
20
20
  ### Settings
21
21
  ```ruby
22
- Asset.mode = ENV['RACK_ENV'] || MODE rescue 'development'
23
- Asset.path = APP_ASSETS rescue File.join(Dir.pwd, 'app', 'assets')
24
- Asset.cache = File.join(Dir.pwd, 'tmp')
25
- Asset.debug = false
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
- - bundle: false
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 'application.js'
64
- <%= script_tag 'application.js' %>
79
+ # Bundle all Javscript files with 'bundle.js'
80
+ <%= script_tag 'bundle.js' %>
65
81
 
66
- # Bundle all files with 'application.css'
67
- <%= script_tag 'application.css' %>
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
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'asset'
3
- s.version = '0.0.3'
4
- s.date = '2017-01-05'
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"]
@@ -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'
@@ -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
- # File? Or bundle?
13
- def file?
14
- @path !~ /^application\.(js|css)$/
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. Pass keyed = false to get the path instead of the file
18
- def files(keyed = true)
19
- if file? or (keyed and ::Asset.mode == 'production')
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(cache = (::Asset.mode = 'production'))
33
- return joined unless cache
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
- # Compress the files
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(File.join(::Asset.cache, %{#{@key}.#{@type}}), 'w'){|f| f.write(r)}
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(false).map{|f| File.read(File.join(::Asset.path, @type, f))}.join
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].each{|r| puts "#{r.upcase}: #{send(r).inspect}"}
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
@@ -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
- path =~ /(-[a-f0-9]{1,32})/
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 ($1 and $1 != item.key) or !item.content(!!$1)
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
@@ -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, File.split(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}").utc
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 application bundle
46
+ # Insert the bundle
47
47
  manifest.insert(0, ::Asset::Item.new(
48
- "application.#{type}", type,
49
- digest("application.#{type}", max), max))
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.3
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-05 00:00:00.000000000 Z
11
+ date: 2017-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack