padrino-assets 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -4
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/.yardopts +10 -0
- data/CHANGELOG.md +11 -8
- data/Gemfile +1 -1
- data/LICENSE +18 -18
- data/README.md +126 -64
- data/Rakefile +30 -7
- data/lib/padrino-assets.rb +154 -91
- data/lib/padrino-assets/helpers.rb +445 -379
- data/lib/padrino-assets/version.rb +5 -4
- data/lib/tasks/cleanup.rake +8 -7
- data/lib/tasks/clobber.rake +7 -6
- data/lib/tasks/compress.rake +14 -13
- data/lib/tasks/precompile.rake +32 -29
- data/padrino-assets.gemspec +25 -24
- data/spec/assets_spec.rb +61 -0
- data/spec/fixtures/assets/application.css +11 -0
- data/spec/fixtures/assets/application.js +1 -0
- data/spec/fixtures/assets/pony.jpg +0 -0
- data/spec/fixtures/compiled_assets/application-b8588c6975a4539fbf2c11471870bdca.css +11 -0
- data/spec/fixtures/compiled_assets/manifest.json +1 -0
- data/spec/helpers_spec.rb +293 -0
- data/spec/spec.rb +37 -0
- data/spec/tasks_spec.rb +114 -0
- metadata +39 -21
- data/examples/nginx.md +0 -11
- data/test/test.rb +0 -52
- data/test/test_helpers.rb +0 -339
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
### 0.
|
4
|
-
*
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
# Version History
|
2
|
+
|
3
|
+
### 0.3.0 - IN DEVELOPMENT
|
4
|
+
* Now uses [Sprockets](https://github.com/sstephenson/sprockets) `context_class` in-order to make helpers available to all assets
|
5
|
+
|
6
|
+
### 0.2.0 - (January 17, 2012)
|
7
|
+
* The current `request` is now passed to **:assets_host** when a `Proc` is used
|
8
|
+
* Added the rake task `assets:compress` for [deflating](http://en.wikipedia.org/wiki/Gzip) assets
|
9
|
+
* Basic support for asset minification
|
10
|
+
|
11
|
+
### 0.1.0 - (January 12, 2012)
|
9
12
|
* Initial release
|
data/Gemfile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
1
|
+
source :rubygems
|
2
2
|
gemspec
|
data/LICENSE
CHANGED
@@ -1,19 +1,19 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
of this software and associated documentation files (the "Software"), to deal
|
5
|
-
in the Software without restriction, including without limitation the rights
|
6
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
copies of the Software, and to permit persons to whom the Software is
|
8
|
-
furnished to do so, subject to the following conditions:
|
9
|
-
|
10
|
-
The above copyright notice and this permission notice shall be included in
|
11
|
-
all copies or substantial portions of the Software.
|
12
|
-
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
1
|
+
Copyright (c) 2012 Benjamin Bloch
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
19
|
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,64 +1,126 @@
|
|
1
|
-
# Padrino Assets
|
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
|
-
|
1
|
+
# Padrino Assets
|
2
|
+
|
3
|
+
Overview
|
4
|
+
--------
|
5
|
+
|
6
|
+
Padrino assets is a plugin for the [Padrino](https://github.com/padrino/padrino-framework) web framework which uses [Sprockets](https://github.com/sstephenson/sprockets) to manage and compile web assets.
|
7
|
+
|
8
|
+
Setup & Installation
|
9
|
+
--------------------
|
10
|
+
|
11
|
+
Include it in your project's `Gemfile`
|
12
|
+
|
13
|
+
``` ruby
|
14
|
+
gem 'padrino-assets'
|
15
|
+
```
|
16
|
+
|
17
|
+
Modify your `app/app.rb` file to register the plugin:
|
18
|
+
|
19
|
+
``` ruby
|
20
|
+
class ExampleApplication < Padrino::Application
|
21
|
+
register Padrino::Assets
|
22
|
+
end
|
23
|
+
```
|
24
|
+
* __Note:__ Because `padrino-assets` overrides some existing helpers the above must be placed below `Padrino::Helpers`
|
25
|
+
|
26
|
+
Modify your `config.ru` file to mount the environment:
|
27
|
+
|
28
|
+
``` ruby
|
29
|
+
map '/assets' do
|
30
|
+
run Padrino::Assets.environment
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
By default, Sprockets is configured to load assets from your project's `app/assets` and `lib/assets` directories. Any files stored in these directories are readily available to the included helpers, and will be served by the Sprockets middleware.
|
35
|
+
|
36
|
+
Because of this the following directories are no longer used, and will be served statically:
|
37
|
+
|
38
|
+
* public/images
|
39
|
+
* public/stylesheets
|
40
|
+
* public/javascripts
|
41
|
+
|
42
|
+
You should now be storing your assets in the following directories:
|
43
|
+
|
44
|
+
* app/assets/images
|
45
|
+
* app/assets/stylesheets
|
46
|
+
* app/assets/javascripts
|
47
|
+
|
48
|
+
Should your project need to add additional paths you can easily do so by adding the following line:
|
49
|
+
|
50
|
+
``` ruby
|
51
|
+
Padrino::Assets.load_paths << 'path/to/my/assets'
|
52
|
+
```
|
53
|
+
|
54
|
+
Configuration
|
55
|
+
-------------
|
56
|
+
|
57
|
+
Although the defaults are enough to get you started. There are several configuration options available to further customize this plugin to your project's personal needs. The following options can be changed at anytime with `set`, `enable`, or `disable`.
|
58
|
+
|
59
|
+
#### :assets_host
|
60
|
+
Is the URL to the external server currently hosting your assets. Generally, you only set this option if you want to host your assets on a separate server or service such as Amazon S3.
|
61
|
+
|
62
|
+
When a `Proc` is used, the current asset, and request will be passed to it as arguments:
|
63
|
+
|
64
|
+
``` ruby
|
65
|
+
set :assets_hosts, ->(asset, request) do
|
66
|
+
if request.ssl?
|
67
|
+
'https://secure.assets.com'
|
68
|
+
else
|
69
|
+
'http://assets.com'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
Of course if you don't need anything flashy a string will suffice:
|
75
|
+
|
76
|
+
``` ruby
|
77
|
+
set :assets_host, 'http://assets.com'
|
78
|
+
```
|
79
|
+
|
80
|
+
#### :compress_assets
|
81
|
+
If enabled, will automatically compress, and gzip your assets to save server bandwidth.
|
82
|
+
Due to the Java dependencies, and the lack of a suitable pure Ruby alternative, a default compressor is not __currently__ provided.
|
83
|
+
|
84
|
+
#### :precompile_assets
|
85
|
+
Is an array of assets that will be compiled when the task `assets:precompile` is run.
|
86
|
+
Once compiled, an MD5 checksum is calculated and inserted into the file name.
|
87
|
+
This checksum serves as a way to version your assets so that they can be properly cached by web browsers.
|
88
|
+
|
89
|
+
To filter which assets are compiled you have a few options. As an example, using a regular expression:
|
90
|
+
|
91
|
+
``` ruby
|
92
|
+
set :precompile_assets, [/^\w\.(?!(?:css|js)$)/i]
|
93
|
+
```
|
94
|
+
|
95
|
+
The above would compile all assets that do not have the extension `.css` or `.js`.
|
96
|
+
|
97
|
+
Or if you prefer, a collection of files would work too:
|
98
|
+
|
99
|
+
``` ruby
|
100
|
+
set :precompile_assets, [
|
101
|
+
'jquery.js',
|
102
|
+
'jquery.unobtrusive.js',
|
103
|
+
'application.css',
|
104
|
+
'pony.jpg'
|
105
|
+
]
|
106
|
+
```
|
107
|
+
|
108
|
+
If you are deploying to a production environment it is recommended that you precompile your assets so that they can be statically served by your web server. In a development environment, your assets will be compiled with every request.
|
109
|
+
|
110
|
+
Dependencies
|
111
|
+
------------
|
112
|
+
|
113
|
+
* [Padrino-Core](https://github.com/padrino/padrino-framework)
|
114
|
+
* [Padrino-Helpers](https://github.com/padrino/padrino-framework)
|
115
|
+
* [Ruby](http://www.ruby-lang.org/en) >= 1.9.2
|
116
|
+
* [Sprockets](https://github.com/sstephenson/sprockets)
|
117
|
+
|
118
|
+
TODO
|
119
|
+
----
|
120
|
+
|
121
|
+
* Additional documentation
|
122
|
+
|
123
|
+
Copyright
|
124
|
+
---------
|
125
|
+
|
126
|
+
Copyright © 2012 Benjamin Bloch (Cirex). See LICENSE for details.
|
data/Rakefile
CHANGED
@@ -1,7 +1,30 @@
|
|
1
|
-
|
2
|
-
require '
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
require 'padrino-assets/version'
|
3
|
+
|
4
|
+
require 'rake'
|
5
|
+
require 'yard'
|
6
|
+
require 'rspec'
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
|
9
|
+
RSpec::Core::RakeTask.new do |task|
|
10
|
+
task.pattern = 'spec/**/*_spec.rb'
|
11
|
+
end
|
12
|
+
|
13
|
+
YARD::Rake::YardocTask.new
|
14
|
+
|
15
|
+
task :build do
|
16
|
+
`gem build padrino-assets.gemspec`
|
17
|
+
end
|
18
|
+
|
19
|
+
task :install => :build do
|
20
|
+
`gem install padrino-assets-#{Padrino::Assets::VERSION}.gem`
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'Releases the current version into the wild'
|
24
|
+
task :release => :build do
|
25
|
+
`git tag -a v#{Padrino::Assets::VERSION} -m "Version #{Padrino::Assets::VERSION}"`
|
26
|
+
`gem push padrino-assets-#{Padrino::Assets::VERSION}.gem`
|
27
|
+
`git push --tags`
|
28
|
+
end
|
29
|
+
|
30
|
+
task :default => :spec
|
data/lib/padrino-assets.rb
CHANGED
@@ -1,91 +1,154 @@
|
|
1
|
-
|
2
|
-
require 'padrino-
|
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
|
-
def
|
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
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'padrino-core'
|
3
|
+
require 'padrino-helpers'
|
4
|
+
|
5
|
+
FileSet.glob_require('padrino-assets/**/*.rb', __FILE__)
|
6
|
+
|
7
|
+
module Padrino
|
8
|
+
module Assets
|
9
|
+
class << self
|
10
|
+
###
|
11
|
+
# Returns a list of paths Sprockets will use in order to find assets used by the project
|
12
|
+
#
|
13
|
+
# @return [Array]
|
14
|
+
# List of assets paths
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# Padrino::Assets.load_paths << Dir[Padrino.root('vendor', '**', 'assets')]
|
18
|
+
#
|
19
|
+
# @since 0.1.0
|
20
|
+
# @api public
|
21
|
+
def load_paths
|
22
|
+
@_load_paths ||= ['app/assets/**', 'lib/assets/**'].map do |directory|
|
23
|
+
Dir[Padrino.root(directory)]
|
24
|
+
end.flatten
|
25
|
+
end
|
26
|
+
|
27
|
+
###
|
28
|
+
# Returns the configured Sprockets environment
|
29
|
+
#
|
30
|
+
# @return [Sprockets::Environment]
|
31
|
+
# Sprockets environment
|
32
|
+
#
|
33
|
+
# @see https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/environment.rb
|
34
|
+
#
|
35
|
+
# @since 0.1.0
|
36
|
+
# @api public
|
37
|
+
def environment
|
38
|
+
@_environment
|
39
|
+
end
|
40
|
+
|
41
|
+
###
|
42
|
+
# Returns a compiled manifest of our assets
|
43
|
+
#
|
44
|
+
# @return [Sprockets::Manifest]
|
45
|
+
# Sprockets manifest
|
46
|
+
#
|
47
|
+
# @see https://github.com/sstephenson/sprockets/blob/master/lib/sprockets/manifest.rb
|
48
|
+
#
|
49
|
+
# @since 0.1.0
|
50
|
+
# @api public
|
51
|
+
def manifest
|
52
|
+
@_manifest
|
53
|
+
end
|
54
|
+
|
55
|
+
###
|
56
|
+
# Returns a list of available asset compressors
|
57
|
+
#
|
58
|
+
# @return [Hash]
|
59
|
+
# List of available asset compressors
|
60
|
+
#
|
61
|
+
# @since 0.3.0
|
62
|
+
# @api public
|
63
|
+
def compressors
|
64
|
+
@_compressors ||= Hash.new { |k, v| k[v] = Hash.new }
|
65
|
+
end
|
66
|
+
|
67
|
+
###
|
68
|
+
# Registers an asset compressor for use with Sprockets
|
69
|
+
#
|
70
|
+
# @param [Symbol] type
|
71
|
+
# The type of compressor you are registering (:js, :css)
|
72
|
+
#
|
73
|
+
# @example
|
74
|
+
# Padrino::Assets.register_compressor :js, :simple => 'SimpleCompressor'
|
75
|
+
# Padrino::Assets.register_compressor :css, :simple => 'SimpleCompressor'
|
76
|
+
#
|
77
|
+
# @since 0.3.0
|
78
|
+
# @api public
|
79
|
+
def register_compressor(type, compressor)
|
80
|
+
compressors[type].merge!(compressor)
|
81
|
+
end
|
82
|
+
|
83
|
+
# @since 0.3.0
|
84
|
+
# @api private
|
85
|
+
def find_registered_compressor(type, compressor)
|
86
|
+
return compressor unless compressor.is_a?(Symbol)
|
87
|
+
|
88
|
+
if compressor = compressors[type][compressor]
|
89
|
+
compressor = compressor.safe_constantize
|
90
|
+
end
|
91
|
+
|
92
|
+
compressor.respond_to?(:new) ? compressor.new : compressor
|
93
|
+
end
|
94
|
+
|
95
|
+
# @private
|
96
|
+
def registered(app)
|
97
|
+
app.helpers Helpers
|
98
|
+
app.set :assets_prefix, '/assets'
|
99
|
+
app.set :assets_version, 1.0
|
100
|
+
app.set :assets_host, nil
|
101
|
+
app.set :compress_assets, true
|
102
|
+
app.set :js_compressor, nil
|
103
|
+
app.set :css_compressor, nil
|
104
|
+
app.set :index_assets, -> { app.environment == :production }
|
105
|
+
app.set :manifest_file, -> { File.join(app.public_folder, app.assets_prefix, 'manifest.json') }
|
106
|
+
app.set :precompile_assets, [ /^\w+\.(?!(?:js|css)$)/i, /^application\.(js|css)$/i ]
|
107
|
+
|
108
|
+
# FIXME: Temporary fix for `padrino start`
|
109
|
+
app.get '/assets/*' do
|
110
|
+
env['PATH_INFO'].gsub!('/assets', '')
|
111
|
+
Padrino::Assets.environment.call(env)
|
112
|
+
end
|
113
|
+
|
114
|
+
Padrino.after_load do
|
115
|
+
require 'sprockets'
|
116
|
+
|
117
|
+
environment = Sprockets::Environment.new(Padrino.root)
|
118
|
+
|
119
|
+
environment.logger = app.logger
|
120
|
+
environment.version = app.assets_version
|
121
|
+
|
122
|
+
if defined?(Padrino::Cache)
|
123
|
+
if app.respond_to?(:caching) && app.caching?
|
124
|
+
environment.cache = app.cache
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
if app.compress_assets?
|
129
|
+
environment.js_compressor = find_registered_compressor(:js, app.js_compressor)
|
130
|
+
environment.css_compressor = find_registered_compressor(:css, app.css_compressor)
|
131
|
+
end
|
132
|
+
|
133
|
+
load_paths.flatten.each do |path|
|
134
|
+
environment.append_path(path)
|
135
|
+
end
|
136
|
+
|
137
|
+
environment.context_class.class_eval do
|
138
|
+
include Helpers
|
139
|
+
end
|
140
|
+
|
141
|
+
@_environment = app.index_assets ? environment.index : environment
|
142
|
+
@_manifest = Sprockets::Manifest.new(@_environment, app.manifest_file)
|
143
|
+
end
|
144
|
+
|
145
|
+
Padrino::Tasks.files << Dir[File.dirname(__FILE__) + '/tasks/**/*.rake']
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
register_compressor :css, :yui => 'YUI::CssCompressor'
|
150
|
+
register_compressor :js, :yui => 'YUI::JavaScriptCompressor'
|
151
|
+
register_compressor :js, :closure => 'Closure::Compiler'
|
152
|
+
register_compressor :js, :uglifier => 'Uglifier'
|
153
|
+
end # Assets
|
154
|
+
end # Padrino
|