sprockets 2.12.5 → 3.7.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +296 -0
- data/LICENSE +2 -2
- data/README.md +235 -262
- data/bin/sprockets +1 -0
- data/lib/rake/sprocketstask.rb +5 -4
- data/lib/sprockets/asset.rb +143 -212
- data/lib/sprockets/autoload/closure.rb +7 -0
- data/lib/sprockets/autoload/coffee_script.rb +7 -0
- data/lib/sprockets/autoload/eco.rb +7 -0
- data/lib/sprockets/autoload/ejs.rb +7 -0
- data/lib/sprockets/autoload/sass.rb +7 -0
- data/lib/sprockets/autoload/uglifier.rb +7 -0
- data/lib/sprockets/autoload/yui.rb +7 -0
- data/lib/sprockets/autoload.rb +11 -0
- data/lib/sprockets/base.rb +56 -393
- data/lib/sprockets/bower.rb +58 -0
- data/lib/sprockets/bundle.rb +69 -0
- data/lib/sprockets/cache/file_store.rb +168 -14
- data/lib/sprockets/cache/memory_store.rb +66 -0
- data/lib/sprockets/cache/null_store.rb +46 -0
- data/lib/sprockets/cache.rb +236 -0
- data/lib/sprockets/cached_environment.rb +69 -0
- data/lib/sprockets/closure_compressor.rb +35 -10
- data/lib/sprockets/coffee_script_processor.rb +25 -0
- data/lib/sprockets/coffee_script_template.rb +17 -0
- data/lib/sprockets/compressing.rb +44 -23
- data/lib/sprockets/configuration.rb +83 -0
- data/lib/sprockets/context.rb +86 -144
- data/lib/sprockets/dependencies.rb +73 -0
- data/lib/sprockets/deprecation.rb +90 -0
- data/lib/sprockets/digest_utils.rb +180 -0
- data/lib/sprockets/directive_processor.rb +207 -211
- data/lib/sprockets/eco_processor.rb +32 -0
- data/lib/sprockets/eco_template.rb +9 -30
- data/lib/sprockets/ejs_processor.rb +31 -0
- data/lib/sprockets/ejs_template.rb +9 -29
- data/lib/sprockets/encoding_utils.rb +261 -0
- data/lib/sprockets/engines.rb +53 -35
- data/lib/sprockets/environment.rb +17 -64
- data/lib/sprockets/erb_processor.rb +30 -0
- data/lib/sprockets/erb_template.rb +11 -0
- data/lib/sprockets/errors.rb +4 -13
- data/lib/sprockets/file_reader.rb +15 -0
- data/lib/sprockets/http_utils.rb +117 -0
- data/lib/sprockets/jst_processor.rb +35 -15
- data/lib/sprockets/legacy.rb +330 -0
- data/lib/sprockets/legacy_proc_processor.rb +35 -0
- data/lib/sprockets/legacy_tilt_processor.rb +29 -0
- data/lib/sprockets/loader.rb +325 -0
- data/lib/sprockets/manifest.rb +202 -127
- data/lib/sprockets/manifest_utils.rb +45 -0
- data/lib/sprockets/mime.rb +112 -31
- data/lib/sprockets/path_dependency_utils.rb +85 -0
- data/lib/sprockets/path_digest_utils.rb +47 -0
- data/lib/sprockets/path_utils.rb +287 -0
- data/lib/sprockets/paths.rb +42 -19
- data/lib/sprockets/processing.rb +178 -126
- data/lib/sprockets/processor_utils.rb +180 -0
- data/lib/sprockets/resolve.rb +211 -0
- data/lib/sprockets/sass_cache_store.rb +22 -17
- data/lib/sprockets/sass_compressor.rb +39 -15
- data/lib/sprockets/sass_functions.rb +2 -70
- data/lib/sprockets/sass_importer.rb +2 -30
- data/lib/sprockets/sass_processor.rb +292 -0
- data/lib/sprockets/sass_template.rb +12 -59
- data/lib/sprockets/server.rb +129 -84
- data/lib/sprockets/transformers.rb +145 -0
- data/lib/sprockets/uglifier_compressor.rb +39 -12
- data/lib/sprockets/unloaded_asset.rb +137 -0
- data/lib/sprockets/uri_tar.rb +98 -0
- data/lib/sprockets/uri_utils.rb +188 -0
- data/lib/sprockets/utils/gzip.rb +67 -0
- data/lib/sprockets/utils.rb +210 -44
- data/lib/sprockets/version.rb +1 -1
- data/lib/sprockets/yui_compressor.rb +39 -11
- data/lib/sprockets.rb +142 -81
- metadata +96 -90
- data/lib/sprockets/asset_attributes.rb +0 -137
- data/lib/sprockets/bundled_asset.rb +0 -78
- data/lib/sprockets/caching.rb +0 -96
- data/lib/sprockets/charset_normalizer.rb +0 -41
- data/lib/sprockets/index.rb +0 -100
- data/lib/sprockets/processed_asset.rb +0 -152
- data/lib/sprockets/processor.rb +0 -32
- data/lib/sprockets/safety_colons.rb +0 -28
- data/lib/sprockets/scss_template.rb +0 -13
- data/lib/sprockets/static_asset.rb +0 -60
data/README.md
CHANGED
@@ -3,22 +3,76 @@
|
|
3
3
|
Sprockets is a Ruby library for compiling and serving web assets.
|
4
4
|
It features declarative dependency management for JavaScript and CSS
|
5
5
|
assets, as well as a powerful preprocessor pipeline that allows you to
|
6
|
-
write assets in languages like CoffeeScript, Sass
|
6
|
+
write assets in languages like CoffeeScript, Sass and SCSS.
|
7
7
|
|
8
|
-
|
8
|
+
|
9
|
+
## Installation
|
9
10
|
|
10
11
|
Install Sprockets from RubyGems:
|
11
12
|
|
12
|
-
|
13
|
+
``` sh
|
14
|
+
$ gem install sprockets
|
15
|
+
```
|
13
16
|
|
14
17
|
Or include it in your project's `Gemfile` with Bundler:
|
15
18
|
|
16
|
-
|
19
|
+
``` ruby
|
20
|
+
gem 'sprockets', '~> 3.0'
|
21
|
+
```
|
22
|
+
|
23
|
+
## Using sprockets
|
24
|
+
|
25
|
+
For most people interested in using sprockets you will want to see [End User Asset Generation](guides/end_user_asset_generation.md) guide. This contains information about sprocket's directive syntax, and default processing behavior.
|
26
|
+
|
27
|
+
If you are a framework developer that is using sprockets, see [Building an Asset Processing Framework](guides/building_an_asset_processing_framework.md).
|
28
|
+
|
29
|
+
If you are a library developer who is extending the functionality of sprockets, see [Extending Sprockets](guides/extending_sprockets.md).
|
30
|
+
|
31
|
+
Below is a disjointed mix of documentation for all three of these roles. Eventually they will be moved to an appropriate guide, for now the recommended way to consume documentation is to view the appropriate guide first and then supplement with docs from the README.
|
32
|
+
|
33
|
+
## Behavior
|
34
|
+
|
35
|
+
### Index files are proxies for folders
|
36
|
+
|
37
|
+
In sprockets index files such as `index.js` or `index.css` files inside of a folder will generate a file with the folder's name. So if you have a `foo/index.js` file it will compile down to `foo.js`. This is similar to NPM's behavior of using [folders as modules](https://nodejs.org/api/modules.html#modules_folders_as_modules). It is also somewhat similar to the way that a file in `public/my_folder/index.html` can be reached by a request to `/my_folder`. This means that you cannot directly use an index file. For example this would not work:
|
38
|
+
|
39
|
+
```
|
40
|
+
<%= asset_path("foo/index.js") %>
|
41
|
+
```
|
42
|
+
|
43
|
+
Instead you would need to use:
|
44
|
+
|
45
|
+
```
|
46
|
+
<%= asset_path("foo.js") %>
|
47
|
+
```
|
17
48
|
|
18
|
-
|
49
|
+
Why would you want to use this behavior? It is common behavior where you might want to include an entire directory of files in a top level javascript. You can do this in sprockets using `require_tree .`
|
50
|
+
|
51
|
+
```
|
52
|
+
//= require_tree .
|
53
|
+
```
|
54
|
+
|
55
|
+
This has the problem that files are required alphabetically. If your directory has `jquery-ui.js` and `jquery.min.js` then sprockets will require `jquery-ui.js` before `jquery` is required which won't work (because jquery-ui depends on jquery). Previously the only way to get the correct ordering would be to rename your files, something like `0-jquery-ui.js`. Instead of doing that you can use an index file.
|
56
|
+
|
57
|
+
For example, if you have an `application.js` and want all the files in the `foo/` folder you could do this:
|
58
|
+
|
59
|
+
```
|
60
|
+
//= require foo.js
|
61
|
+
```
|
62
|
+
|
63
|
+
Then create a file `foo/index.js` that requires all the files in that folder in any order you want:
|
64
|
+
|
65
|
+
```
|
66
|
+
//= require foo.min.js
|
67
|
+
//= require foo-ui.js
|
68
|
+
```
|
69
|
+
|
70
|
+
Now in your `application.js` will correctly load the `foo.min.js` before `foo-ui.js`. If you used `require_tree` it would not work correctly.
|
71
|
+
|
72
|
+
## Understanding the Sprockets Environment
|
19
73
|
|
20
74
|
You'll need an instance of the `Sprockets::Environment` class to
|
21
|
-
access and serve assets from your application. Under Rails
|
75
|
+
access and serve assets from your application. Under Rails 4.0 and
|
22
76
|
later, `YourApp::Application.assets` is a preconfigured
|
23
77
|
`Sprockets::Environment` instance. For Rack-based applications, create
|
24
78
|
an instance in `config.ru`.
|
@@ -28,7 +82,7 @@ assets, manipulating the load path, and registering processors. It is
|
|
28
82
|
also a Rack application that can be mounted at a URL to serve assets
|
29
83
|
over HTTP.
|
30
84
|
|
31
|
-
|
85
|
+
### The Load Path
|
32
86
|
|
33
87
|
The *load path* is an ordered list of directories that Sprockets uses
|
34
88
|
to search for assets.
|
@@ -42,29 +96,31 @@ The power of the load path is that it lets you organize your source
|
|
42
96
|
files into multiple directories -- even directories that live outside
|
43
97
|
your application -- and combine those directories into a single
|
44
98
|
virtual filesystem. That means you can easily bundle JavaScript, CSS
|
45
|
-
and images into a Ruby library and import them into your application.
|
99
|
+
and images into a Ruby library or [Bower](http://bower.io) package and import them into your application.
|
46
100
|
|
47
|
-
|
101
|
+
#### Manipulating the Load Path
|
48
102
|
|
49
103
|
To add a directory to your environment's load path, use the
|
50
104
|
`append_path` and `prepend_path` methods. Directories at the beginning
|
51
105
|
of the load path have precedence over subsequent directories.
|
52
106
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
107
|
+
``` ruby
|
108
|
+
environment = Sprockets::Environment.new
|
109
|
+
environment.append_path 'app/assets/javascripts'
|
110
|
+
environment.append_path 'lib/assets/javascripts'
|
111
|
+
environment.append_path 'vendor/assets/bower_components'
|
112
|
+
```
|
57
113
|
|
58
114
|
In general, you should append to the path by default and reserve
|
59
115
|
prepending for cases where you need to override existing assets.
|
60
116
|
|
61
|
-
|
117
|
+
### Accessing Assets
|
62
118
|
|
63
119
|
Once you've set up your environment's load path, you can mount the
|
64
120
|
environment as a Rack server and request assets via HTTP. You can also
|
65
121
|
access assets programmatically from within your application.
|
66
122
|
|
67
|
-
|
123
|
+
#### Logical Paths
|
68
124
|
|
69
125
|
Assets in Sprockets are always referenced by their *logical path*.
|
70
126
|
|
@@ -90,7 +146,7 @@ contains the directory `app/assets/javascripts`:
|
|
90
146
|
In this way, all directories in the load path are merged to create a
|
91
147
|
virtual filesystem whose entries are logical paths.
|
92
148
|
|
93
|
-
|
149
|
+
#### Serving Assets Over HTTP
|
94
150
|
|
95
151
|
When you mount an environment, all of its assets are accessible as
|
96
152
|
logical paths underneath the *mount point*. For example, if you mount
|
@@ -98,48 +154,57 @@ your environment at `/assets` and request the URL
|
|
98
154
|
`/assets/application.js`, Sprockets will search your load path for the
|
99
155
|
file named `application.js` and serve it.
|
100
156
|
|
101
|
-
Under Rails
|
157
|
+
Under Rails 4.0 and later, your Sprockets environment is automatically
|
102
158
|
mounted at `/assets`. If you are using Sprockets with a Rack
|
103
159
|
application, you will need to mount the environment yourself. A good
|
104
160
|
way to do this is with the `map` method in `config.ru`:
|
105
161
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
162
|
+
``` ruby
|
163
|
+
require 'sprockets'
|
164
|
+
map '/assets' do
|
165
|
+
environment = Sprockets::Environment.new
|
166
|
+
environment.append_path 'app/assets/javascripts'
|
167
|
+
environment.append_path 'app/assets/stylesheets'
|
168
|
+
run environment
|
169
|
+
end
|
113
170
|
|
114
|
-
|
115
|
-
|
116
|
-
|
171
|
+
map '/' do
|
172
|
+
run YourRackApp
|
173
|
+
end
|
174
|
+
```
|
117
175
|
|
118
|
-
|
176
|
+
#### Accessing Assets Programmatically
|
119
177
|
|
120
178
|
You can use the `find_asset` method (aliased as `[]`) to retrieve an
|
121
179
|
asset from a Sprockets environment. Pass it a logical path and you'll
|
122
|
-
get a `Sprockets::
|
180
|
+
get a `Sprockets::Asset` instance back:
|
123
181
|
|
124
|
-
|
125
|
-
|
182
|
+
``` ruby
|
183
|
+
environment['application.js']
|
184
|
+
# => #<Sprockets::Asset ...>
|
185
|
+
```
|
126
186
|
|
127
187
|
Call `to_s` on the resulting asset to access its contents, `length` to
|
128
188
|
get its length in bytes, `mtime` to query its last-modified time, and
|
129
|
-
`
|
189
|
+
`filename` to get its full path on the filesystem.
|
190
|
+
|
130
191
|
|
131
|
-
|
192
|
+
## Using Processors
|
132
193
|
|
133
|
-
Asset source files can be written in another
|
194
|
+
Asset source files can be written in another format, like SCSS or
|
134
195
|
CoffeeScript, and automatically compiled to CSS or JavaScript by
|
135
|
-
Sprockets.
|
196
|
+
Sprockets. Processors that convert a file from one format to another are called *transformers*.
|
136
197
|
|
137
|
-
|
138
|
-
filename. For example, a CSS file written in SCSS might have the name
|
139
|
-
`layout.css.scss`, while a JavaScript file written in CoffeeScript
|
140
|
-
might have the name `dialog.js.coffee`.
|
198
|
+
### Minifying Assets
|
141
199
|
|
142
|
-
|
200
|
+
Several JavaScript and CSS minifiers are available through shorthand.
|
201
|
+
|
202
|
+
``` ruby
|
203
|
+
environment.js_compressor = :uglify
|
204
|
+
environment.css_compressor = :scss
|
205
|
+
```
|
206
|
+
|
207
|
+
### Styling with Sass and SCSS
|
143
208
|
|
144
209
|
[Sass](http://sass-lang.com/) is a language that compiles to CSS and
|
145
210
|
adds features like nested rules, variables, mixins and selector
|
@@ -149,23 +214,10 @@ If the `sass` gem is available to your application, you can use Sass
|
|
149
214
|
to write CSS assets in Sprockets.
|
150
215
|
|
151
216
|
Sprockets supports both Sass syntaxes. For the original
|
152
|
-
whitespace-sensitive syntax, use the extension `.
|
153
|
-
new SCSS syntax, use the extension `.
|
154
|
-
|
155
|
-
## Styling with LESS ##
|
217
|
+
whitespace-sensitive syntax, use the extension `.sass`. For the
|
218
|
+
new SCSS syntax, use the extension `.scss`.
|
156
219
|
|
157
|
-
|
158
|
-
variables, mixins, operations and functions.
|
159
|
-
|
160
|
-
If the `less` gem is available to your application, you can use LESS
|
161
|
-
to write CSS assets in Sprockets. Note that the LESS compiler is
|
162
|
-
written in JavaScript and the `less` gem (on MRI) uses `therubyracer`
|
163
|
-
which embeds the V8 JavaScript runtime in Ruby, while on JRuby you're
|
164
|
-
going to need `therubyrhino` gem installed.
|
165
|
-
|
166
|
-
To write CSS assets with LESS, use the extension `.css.less`.
|
167
|
-
|
168
|
-
## Scripting with CoffeeScript ##
|
220
|
+
### Scripting with CoffeeScript
|
169
221
|
|
170
222
|
[CoffeeScript](http://jashkenas.github.com/coffee-script/) is a
|
171
223
|
language that compiles to the "good parts" of JavaScript, featuring a
|
@@ -175,13 +227,13 @@ binding.
|
|
175
227
|
If the `coffee-script` gem is available to your application, you can
|
176
228
|
use CoffeeScript to write JavaScript assets in Sprockets. Note that
|
177
229
|
the CoffeeScript compiler is written in JavaScript, and you will need
|
178
|
-
an [ExecJS](https://github.com/
|
230
|
+
an [ExecJS](https://github.com/rails/execjs)-supported runtime
|
179
231
|
on your system to invoke it.
|
180
232
|
|
181
233
|
To write JavaScript assets with CoffeeScript, use the extension
|
182
|
-
`.
|
234
|
+
`.coffee`.
|
183
235
|
|
184
|
-
|
236
|
+
### JavaScript Templating with EJS and Eco
|
185
237
|
|
186
238
|
Sprockets supports *JavaScript templates* for client-side rendering of
|
187
239
|
strings or markup. JavaScript templates have the special format
|
@@ -192,12 +244,14 @@ logical path as a property on the global `JST` object. Invoke a
|
|
192
244
|
template function to render the template as a string. The resulting
|
193
245
|
string can then be inserted into the DOM.
|
194
246
|
|
195
|
-
|
196
|
-
|
247
|
+
```
|
248
|
+
<!-- templates/hello.jst.ejs -->
|
249
|
+
<div>Hello, <span><%= name %></span>!</div>
|
197
250
|
|
198
|
-
|
199
|
-
|
200
|
-
|
251
|
+
// application.js
|
252
|
+
//= require templates/hello
|
253
|
+
$("#hello").html(JST["templates/hello"]({ name: "Sam" }));
|
254
|
+
```
|
201
255
|
|
202
256
|
Sprockets supports two JavaScript template languages:
|
203
257
|
[EJS](https://github.com/sstephenson/ruby-ejs), for embedded
|
@@ -214,17 +268,12 @@ templates have the extension `.jst.eco`. Note that the `eco` gem
|
|
214
268
|
depends on the CoffeeScript compiler, so the same caveats apply as
|
215
269
|
outlined above for the CoffeeScript engine.
|
216
270
|
|
217
|
-
|
271
|
+
### Invoking Ruby with ERB
|
218
272
|
|
219
273
|
Sprockets provides an ERB engine for preprocessing assets using
|
220
274
|
embedded Ruby code. Append `.erb` to a CSS or JavaScript asset's
|
221
275
|
filename to enable the ERB engine.
|
222
276
|
|
223
|
-
**Note**: Sprockets processes multiple engine extensions in order from
|
224
|
-
right to left, so you can use multiple engines with a single
|
225
|
-
asset. For example, to have a CoffeeScript asset that is first
|
226
|
-
preprocessed with ERB, use the extension `.js.coffee.erb`.
|
227
|
-
|
228
277
|
Ruby code embedded in an asset is evaluated in the context of a
|
229
278
|
`Sprockets::Context` instance for the given asset. Common uses for ERB
|
230
279
|
include:
|
@@ -237,16 +286,11 @@ include:
|
|
237
286
|
database, in a JavaScript asset via JSON
|
238
287
|
- embedding version constants loaded from another file
|
239
288
|
|
240
|
-
See the [Helper Methods](
|
289
|
+
See the [Helper Methods](lib/sprockets/context.rb) section for more information about
|
241
290
|
interacting with `Sprockets::Context` instances via ERB.
|
242
291
|
|
243
|
-
### String Interpolation Syntax ###
|
244
|
-
|
245
|
-
If you need access to Ruby from an asset but cannot use ERB's `<% …
|
246
|
-
%>` syntax, Sprockets also supports Ruby string interpolation syntax
|
247
|
-
(`#{ … }`) with the `.str` engine extension.
|
248
292
|
|
249
|
-
|
293
|
+
## Managing and Bundling Dependencies
|
250
294
|
|
251
295
|
You can create *asset bundles* -- ordered concatenations of asset
|
252
296
|
source files -- by specifying dependencies in a special comment syntax
|
@@ -257,16 +301,18 @@ them to recursively build a dependency graph. When you request an
|
|
257
301
|
asset with dependencies, the dependencies will be included in order at
|
258
302
|
the top of the file.
|
259
303
|
|
260
|
-
|
304
|
+
### The Directive Processor
|
261
305
|
|
262
306
|
Sprockets runs the *directive processor* on each CSS and JavaScript
|
263
307
|
source file. The directive processor scans for comment lines beginning
|
264
308
|
with `=` in comment blocks at the top of the file.
|
265
309
|
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
310
|
+
``` js
|
311
|
+
//= require jquery
|
312
|
+
//= require jquery-ui
|
313
|
+
//= require backbone
|
314
|
+
//= require_tree .
|
315
|
+
```
|
270
316
|
|
271
317
|
The first word immediately following `=` specifies the directive
|
272
318
|
name. Any words following the directive name are treated as
|
@@ -278,21 +324,27 @@ contain spaces, similar to commands in the Unix shell.
|
|
278
324
|
processing. Sprockets will not look for directives in comment blocks
|
279
325
|
that occur after the first line of code.
|
280
326
|
|
281
|
-
|
327
|
+
#### Supported Comment Types
|
282
328
|
|
283
329
|
The directive processor understands comment blocks in three formats:
|
284
330
|
|
285
|
-
|
286
|
-
|
287
|
-
|
331
|
+
``` css
|
332
|
+
/* Multi-line comment blocks (CSS, SCSS, JavaScript)
|
333
|
+
*= require foo
|
334
|
+
*/
|
335
|
+
```
|
288
336
|
|
289
|
-
|
290
|
-
|
337
|
+
``` js
|
338
|
+
// Single-line comment blocks (SCSS, JavaScript)
|
339
|
+
//= require foo
|
340
|
+
```
|
291
341
|
|
292
|
-
|
293
|
-
|
342
|
+
``` coffee
|
343
|
+
# Single-line comment blocks (CoffeeScript)
|
344
|
+
#= require foo
|
345
|
+
```
|
294
346
|
|
295
|
-
|
347
|
+
### Sprockets Directives
|
296
348
|
|
297
349
|
You can use the following directives to declare dependencies in asset
|
298
350
|
source files.
|
@@ -301,236 +353,157 @@ For directives that take a *path* argument, you may specify either a
|
|
301
353
|
logical path or a relative path. Relative paths begin with `./` and
|
302
354
|
reference files relative to the location of the current file.
|
303
355
|
|
304
|
-
|
356
|
+
#### The `require` Directive
|
305
357
|
|
306
358
|
`require` *path* inserts the contents of the asset source file
|
307
359
|
specified by *path*. If the file is required multiple times, it will
|
308
360
|
appear in the bundle only once.
|
309
361
|
|
310
|
-
### The `include` Directive ###
|
311
|
-
|
312
|
-
`include` *path* works like `require`, but inserts the contents of the
|
313
|
-
specified source file even if it has already been included or
|
314
|
-
required.
|
315
|
-
|
316
362
|
### The `require_directory` Directive ###
|
317
363
|
|
318
364
|
`require_directory` *path* requires all source files of the same
|
319
365
|
format in the directory specified by *path*. Files are required in
|
320
366
|
alphabetical order.
|
321
367
|
|
322
|
-
|
368
|
+
#### The `require_tree` Directive
|
323
369
|
|
324
370
|
`require_tree` *path* works like `require_directory`, but operates
|
325
371
|
recursively to require all files in all subdirectories of the
|
326
372
|
directory specified by *path*.
|
327
373
|
|
328
|
-
|
374
|
+
#### The `require_self` Directive
|
329
375
|
|
330
376
|
`require_self` tells Sprockets to insert the body of the current
|
331
|
-
source file before any subsequent `require`
|
332
|
-
|
333
|
-
### The `depend_on` Directive ###
|
334
|
-
|
335
|
-
`depend_on` *path* declares a dependency on the given *path* without
|
336
|
-
including it in the bundle. This is useful when you need to expire an
|
337
|
-
asset's cache in response to a change in another file.
|
377
|
+
source file before any subsequent `require` directives.
|
338
378
|
|
339
|
-
|
379
|
+
#### The `link` Directive
|
340
380
|
|
341
|
-
`
|
342
|
-
|
343
|
-
|
344
|
-
### The `stub` Directive ###
|
345
|
-
|
346
|
-
`stub` *path* allows dependency to be excluded from the asset bundle.
|
347
|
-
The *path* must be a valid asset and may or may not already be part
|
348
|
-
of the bundle. Once stubbed, it is blacklisted and can't be brought
|
349
|
-
back by any other `require`.
|
350
|
-
|
351
|
-
# Development #
|
381
|
+
`link` *path* declares a dependency on the target *path* and adds it to a list
|
382
|
+
of subdependencies to automatically be compiled when the asset is written out to
|
383
|
+
disk.
|
352
384
|
|
353
|
-
|
385
|
+
For an example, in a CSS file you might reference an external image that always
|
386
|
+
needs to be compiled along with the css file.
|
354
387
|
|
355
|
-
|
356
|
-
|
357
|
-
|
388
|
+
``` css
|
389
|
+
/*= link "logo.png" */
|
390
|
+
.logo {
|
391
|
+
background-image: url(logo.png)
|
392
|
+
}
|
393
|
+
```
|
358
394
|
|
359
|
-
|
395
|
+
However, if you use a `asset-path` or `asset-url` SCSS helper, these links will
|
396
|
+
automatically be defined for you.
|
360
397
|
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
398
|
+
``` css
|
399
|
+
.logo {
|
400
|
+
background-image: asset-url("logo.png")
|
401
|
+
}
|
402
|
+
```
|
366
403
|
|
367
|
-
|
404
|
+
#### The `depend_on` Directive
|
368
405
|
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
**2.12.2** (September 5, 2014)
|
374
|
-
|
375
|
-
* Ensure internal asset lookups calls are still restricted to load paths within
|
376
|
-
asset compiles. Though, you should not depend on internal asset resolves to be
|
377
|
-
completely restricted for security reasons. Assets themselves should be
|
378
|
-
considered full scripting environments with filesystem access.
|
379
|
-
|
380
|
-
**2.12.1** (April 17, 2014)
|
381
|
-
|
382
|
-
* Fix making manifest target directory when its different than the output directory.
|
383
|
-
|
384
|
-
**2.12.0** (March 13, 2014)
|
385
|
-
|
386
|
-
* Avoid context reference in SassImporter hack so its Marshallable. Fixes
|
387
|
-
issues with Sass 3.3.x.
|
388
|
-
|
389
|
-
**2.11.0** (February 19, 2014)
|
390
|
-
|
391
|
-
* Support for `.bower.json`
|
392
|
-
|
393
|
-
**2.10.0** (May 24, 2013)
|
394
|
-
|
395
|
-
* Support for `bower.json`
|
396
|
-
|
397
|
-
**2.9.3** (April 20, 2013)
|
398
|
-
|
399
|
-
* Fixed sass caching bug
|
400
|
-
|
401
|
-
**2.9.2** (April 8, 2013)
|
402
|
-
|
403
|
-
* Improve file freshness check performance
|
404
|
-
* Directive processor encoding fixes
|
405
|
-
|
406
|
-
**2.9.1** (April 6, 2013)
|
407
|
-
|
408
|
-
* Support for Uglifier 2.x
|
409
|
-
|
410
|
-
**2.9.0** (February 25, 2013)
|
411
|
-
|
412
|
-
* Write out gzipped variants of bundled assets.
|
413
|
-
|
414
|
-
**2.8.2** (December 10, 2012)
|
415
|
-
|
416
|
-
* Fixed top level Sass constant references
|
417
|
-
* Fixed manifest logger when environment is disabled
|
418
|
-
|
419
|
-
**2.8.1** (October 31, 2012)
|
420
|
-
|
421
|
-
* Fixed Sass importer bug
|
422
|
-
|
423
|
-
**2.8.0** (October 16, 2012)
|
424
|
-
|
425
|
-
* Allow manifest location to be seperated from output directory
|
426
|
-
* Pass logical path and absolute path to each_logical_path iterator
|
427
|
-
|
428
|
-
**2.7.0** (October 10, 2012)
|
429
|
-
|
430
|
-
* Added --css-compressor and --js-compressor command line flags
|
431
|
-
* Added css/js compressor shorthand
|
432
|
-
* Change default manifest.json filename to be a randomized manifest-16HEXBYTES.json
|
433
|
-
* Allow nil environment to be passed to manifest
|
434
|
-
* Allow manifest instance to be set on rake task
|
435
|
-
|
436
|
-
**2.6.0** (September 19, 2012)
|
437
|
-
|
438
|
-
* Added bower component.json require support
|
439
|
-
|
440
|
-
**2.5.0** (September 4, 2012)
|
441
|
-
|
442
|
-
* Fixed Ruby 2.0 RegExp warning
|
443
|
-
* Provide stubbed implementation of context *_path helpers
|
444
|
-
* Add SassCompressor
|
445
|
-
|
446
|
-
**2.4.5** (July 10, 2012)
|
447
|
-
|
448
|
-
* Tweaked some logger levels
|
449
|
-
|
450
|
-
**2.4.4** (July 2, 2012)
|
451
|
-
|
452
|
-
* Canonicalize logical path extensions
|
453
|
-
* Check absolute paths passed to depend_on
|
406
|
+
`depend_on` *path* declares a dependency on the given *path* without
|
407
|
+
including it in the bundle. This is useful when you need to expire an
|
408
|
+
asset's cache in response to a change in another file.
|
454
409
|
|
455
|
-
|
410
|
+
#### The `depend_on_asset` Directive
|
456
411
|
|
457
|
-
*
|
458
|
-
|
412
|
+
`depend_on_asset` *path* works like `depend_on`, but operates
|
413
|
+
recursively reading the file and following the directives found. This is automatically implied if you use `link`, so consider if it just makes sense using `link` instead of `depend_on_asset`.
|
459
414
|
|
460
|
-
|
415
|
+
#### The `stub` Directive
|
461
416
|
|
462
|
-
*
|
417
|
+
`stub` *path* allows dependency to be excluded from the asset bundle.
|
418
|
+
The *path* must be a valid asset and may or may not already be part
|
419
|
+
of the bundle. `stub` should only be used at the top level bundle, not
|
420
|
+
within any subdependencies.
|
463
421
|
|
464
|
-
**2.4.1** (April 26, 2012)
|
465
422
|
|
466
|
-
|
467
|
-
* Fixed gzip mtime
|
423
|
+
## Processor Interface
|
468
424
|
|
469
|
-
|
425
|
+
Sprockets 2.x was originally design around [Tilt](https://github.com/rtomayko/tilt)'s engine interface. However, starting with 3.x, a new interface has been introduced deprecating Tilt.
|
470
426
|
|
471
|
-
|
472
|
-
* Added global processor registry
|
427
|
+
Similar to Rack, a processor is a any "callable" (an object that responds to `call`). This maybe a simple Proc or a full class that defines a `def self.call(input)` method. The `call` method accepts an `input` Hash and returns a Hash of metadata.
|
473
428
|
|
474
|
-
|
429
|
+
Also see [`Sprockets::ProcessorUtils`](https://github.com/rails/sprockets/blob/master/lib/sprockets/processor_utils.rb) for public helper methods.
|
475
430
|
|
476
|
-
|
431
|
+
### input Hash
|
477
432
|
|
478
|
-
|
433
|
+
The `input` Hash defines the following public fields.
|
479
434
|
|
480
|
-
*
|
481
|
-
*
|
482
|
-
*
|
435
|
+
* `:data` - String asset contents
|
436
|
+
* `:environment` - Current `Sprockets::Environment` instance.
|
437
|
+
* `:cache` - A `Sprockets::Cache` instance. See [`Sprockets::Cache#fetch`](https://github.com/rails/sprockets/blob/master/lib/sprockets/cache.rb).
|
438
|
+
* `:uri` - String Asset URI.
|
439
|
+
* `:filename` - String full path to original file.
|
440
|
+
* `:load_path` - String current load path for filename.
|
441
|
+
* `:name` - String logical path for filename.
|
442
|
+
* `:content_type` - String content type of the output asset.
|
443
|
+
* `:metadata` - Hash of processor metadata.
|
483
444
|
|
484
|
-
|
445
|
+
``` ruby
|
446
|
+
def self.call(input)
|
447
|
+
input[:cache].fetch("my:cache:key:v1") do
|
448
|
+
# Remove all semicolons from source
|
449
|
+
input[:data].gsub(";", "")
|
450
|
+
end
|
451
|
+
end
|
452
|
+
```
|
485
453
|
|
486
|
-
|
454
|
+
### return Hash
|
487
455
|
|
488
|
-
|
456
|
+
The processor should return metadata `Hash`. With the exception of the `:data` key, the processor can store arbitrary JSON valid values in this Hash. The data will be stored and exposed on `Asset#metadata`.
|
489
457
|
|
490
|
-
|
491
|
-
* Added rake/sprocketstask.
|
492
|
-
* Added json manifest log of compiled assets.
|
493
|
-
* Added `stub` directive that allows you to exclude files from the bundle.
|
494
|
-
* Added per environment external encoding (Environment#default_external_encoding). Defaults to UTF-8. Fixes issues where LANG is not set correctly and Rubys default external is set to ASCII.
|
458
|
+
The returned `:data` replaces the assets `input[:data]` to the next processor in the chain. Returning a `String` is shorthand for returning `{ data: str }`. And returning `nil` is shorthand for a no-op where the input data is not transformed, `{ data: input[:data] }`.
|
495
459
|
|
496
|
-
|
460
|
+
### metadata
|
497
461
|
|
498
|
-
|
462
|
+
The metadata Hash provides an open format for processors to extend the pipeline processor. Internally, built-in processors use it for passing data to each other.
|
499
463
|
|
500
|
-
|
464
|
+
* `:required` - A `Set` of String Asset URIs that the Bundle processor should concatenate together.
|
465
|
+
* `:stubbed` - A `Set` of String Asset URIs that will be omitted from the `:required` set.
|
466
|
+
* `:links` - A `Set` of String Asset URIs that should be compiled along with this asset.
|
467
|
+
* `:dependencies` - A `Set` of String Cache URIs that should be monitored for caching.
|
501
468
|
|
502
|
-
|
469
|
+
``` ruby
|
470
|
+
def self.call(input)
|
471
|
+
# Any metadata may start off as nil, so initialize it the value
|
472
|
+
required = Set.new(input[:metadata][:required])
|
503
473
|
|
504
|
-
|
474
|
+
# Manually add "foo.js" asset uri to our bundle
|
475
|
+
required << input[:environment].resolve("foo.js")
|
505
476
|
|
506
|
-
|
507
|
-
|
508
|
-
|
477
|
+
{ required: required }
|
478
|
+
end
|
479
|
+
```
|
509
480
|
|
510
|
-
**2.0.3** (October 17, 2011)
|
511
481
|
|
512
|
-
|
513
|
-
* Make JST namespace configurable.
|
482
|
+
## Development
|
514
483
|
|
515
|
-
|
484
|
+
### Contributing
|
516
485
|
|
517
|
-
|
486
|
+
The Sprockets source code is [hosted on
|
487
|
+
GitHub](https://github.com/rails/sprockets). You can check out a
|
488
|
+
copy of the latest code using Git:
|
518
489
|
|
519
|
-
|
490
|
+
$ git clone https://github.com/rails/sprockets
|
520
491
|
|
521
|
-
|
522
|
-
|
523
|
-
|
492
|
+
If you've found a bug or have a question, please open an issue on the
|
493
|
+
[Sprockets issue
|
494
|
+
tracker](https://github.com/rails/sprockets/issues). Or, clone
|
495
|
+
the Sprockets repository, write a failing test case, fix the bug and
|
496
|
+
submit a pull request.
|
524
497
|
|
525
|
-
|
498
|
+
### Version History
|
526
499
|
|
527
|
-
|
500
|
+
Please see the [CHANGELOG](https://github.com/rails/sprockets/tree/master/CHANGELOG.md)
|
528
501
|
|
529
|
-
|
502
|
+
## License
|
530
503
|
|
531
|
-
Copyright ©
|
504
|
+
Copyright © 2014 Sam Stephenson <<sstephenson@gmail.com>>
|
532
505
|
|
533
|
-
Copyright ©
|
506
|
+
Copyright © 2014 Joshua Peek <<josh@joshpeek.com>>
|
534
507
|
|
535
508
|
Sprockets is distributed under an MIT-style license. See LICENSE for
|
536
509
|
details.
|