sprockets 2.6.0 → 4.2.2
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 +7 -0
- data/CHANGELOG.md +118 -0
- data/{LICENSE → MIT-LICENSE} +2 -2
- data/README.md +541 -289
- data/bin/sprockets +20 -7
- data/lib/rake/sprocketstask.rb +34 -17
- data/lib/sprockets/add_source_map_comment_to_asset_processor.rb +60 -0
- data/lib/sprockets/asset.rb +158 -210
- data/lib/sprockets/autoload/babel.rb +8 -0
- data/lib/sprockets/autoload/closure.rb +8 -0
- data/lib/sprockets/autoload/coffee_script.rb +8 -0
- data/lib/sprockets/autoload/eco.rb +8 -0
- data/lib/sprockets/autoload/ejs.rb +8 -0
- data/lib/sprockets/autoload/jsminc.rb +8 -0
- data/lib/sprockets/autoload/sass.rb +8 -0
- data/lib/sprockets/autoload/sassc.rb +8 -0
- data/lib/sprockets/autoload/uglifier.rb +8 -0
- data/lib/sprockets/autoload/yui.rb +8 -0
- data/lib/sprockets/autoload/zopfli.rb +7 -0
- data/lib/sprockets/autoload.rb +16 -0
- data/lib/sprockets/babel_processor.rb +66 -0
- data/lib/sprockets/base.rb +89 -378
- data/lib/sprockets/bower.rb +61 -0
- data/lib/sprockets/bundle.rb +105 -0
- data/lib/sprockets/cache/file_store.rb +190 -14
- data/lib/sprockets/cache/memory_store.rb +84 -0
- data/lib/sprockets/cache/null_store.rb +54 -0
- data/lib/sprockets/cache.rb +271 -0
- data/lib/sprockets/cached_environment.rb +64 -0
- data/lib/sprockets/closure_compressor.rb +48 -0
- data/lib/sprockets/coffee_script_processor.rb +39 -0
- data/lib/sprockets/compressing.rb +134 -0
- data/lib/sprockets/configuration.rb +79 -0
- data/lib/sprockets/context.rb +166 -150
- data/lib/sprockets/dependencies.rb +74 -0
- data/lib/sprockets/digest_utils.rb +197 -0
- data/lib/sprockets/directive_processor.rb +241 -215
- data/lib/sprockets/eco_processor.rb +33 -0
- data/lib/sprockets/ejs_processor.rb +32 -0
- data/lib/sprockets/encoding_utils.rb +261 -0
- data/lib/sprockets/environment.rb +23 -64
- data/lib/sprockets/erb_processor.rb +43 -0
- data/lib/sprockets/errors.rb +5 -13
- data/lib/sprockets/exporters/base.rb +71 -0
- data/lib/sprockets/exporters/file_exporter.rb +24 -0
- data/lib/sprockets/exporters/zlib_exporter.rb +33 -0
- data/lib/sprockets/exporters/zopfli_exporter.rb +14 -0
- data/lib/sprockets/exporting.rb +73 -0
- data/lib/sprockets/file_reader.rb +16 -0
- data/lib/sprockets/http_utils.rb +135 -0
- data/lib/sprockets/jsminc_compressor.rb +32 -0
- data/lib/sprockets/jst_processor.rb +36 -19
- data/lib/sprockets/loader.rb +347 -0
- data/lib/sprockets/manifest.rb +228 -112
- data/lib/sprockets/manifest_utils.rb +48 -0
- data/lib/sprockets/mime.rb +78 -31
- data/lib/sprockets/npm.rb +52 -0
- data/lib/sprockets/path_dependency_utils.rb +77 -0
- data/lib/sprockets/path_digest_utils.rb +48 -0
- data/lib/sprockets/path_utils.rb +367 -0
- data/lib/sprockets/paths.rb +43 -19
- data/lib/sprockets/preprocessors/default_source_map.rb +49 -0
- data/lib/sprockets/processing.rb +146 -164
- data/lib/sprockets/processor_utils.rb +170 -0
- data/lib/sprockets/resolve.rb +295 -0
- data/lib/sprockets/sass_cache_store.rb +20 -15
- data/lib/sprockets/sass_compressor.rb +55 -10
- data/lib/sprockets/sass_functions.rb +3 -70
- data/lib/sprockets/sass_importer.rb +3 -29
- data/lib/sprockets/sass_processor.rb +313 -0
- data/lib/sprockets/sassc_compressor.rb +56 -0
- data/lib/sprockets/sassc_processor.rb +297 -0
- data/lib/sprockets/server.rb +159 -91
- data/lib/sprockets/source_map_processor.rb +66 -0
- data/lib/sprockets/source_map_utils.rb +483 -0
- data/lib/sprockets/transformers.rb +173 -0
- data/lib/sprockets/uglifier_compressor.rb +66 -0
- data/lib/sprockets/unloaded_asset.rb +139 -0
- data/lib/sprockets/uri_tar.rb +99 -0
- data/lib/sprockets/uri_utils.rb +194 -0
- data/lib/sprockets/utils/gzip.rb +99 -0
- data/lib/sprockets/utils.rb +193 -52
- data/lib/sprockets/version.rb +2 -1
- data/lib/sprockets/yui_compressor.rb +56 -0
- data/lib/sprockets.rb +217 -75
- metadata +272 -117
- data/lib/sprockets/asset_attributes.rb +0 -131
- data/lib/sprockets/bundled_asset.rb +0 -80
- data/lib/sprockets/caching.rb +0 -96
- data/lib/sprockets/charset_normalizer.rb +0 -41
- data/lib/sprockets/eco_template.rb +0 -38
- data/lib/sprockets/ejs_template.rb +0 -37
- data/lib/sprockets/engines.rb +0 -74
- data/lib/sprockets/index.rb +0 -99
- 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/sass_template.rb +0 -60
- data/lib/sprockets/scss_template.rb +0 -13
- data/lib/sprockets/static_asset.rb +0 -58
data/README.md
CHANGED
|
@@ -3,68 +3,50 @@
|
|
|
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
|
+
## Installation
|
|
9
9
|
|
|
10
10
|
Install Sprockets from RubyGems:
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
``` sh
|
|
13
|
+
$ gem install sprockets
|
|
14
|
+
```
|
|
13
15
|
|
|
14
16
|
Or include it in your project's `Gemfile` with Bundler:
|
|
15
17
|
|
|
16
|
-
|
|
18
|
+
``` ruby
|
|
19
|
+
gem 'sprockets', '~> 4.0'
|
|
20
|
+
```
|
|
17
21
|
|
|
18
|
-
|
|
22
|
+
## Upgrading to Sprockets 4.x
|
|
19
23
|
|
|
20
|
-
|
|
21
|
-
access and serve assets from your application. Under Rails 3.1 and
|
|
22
|
-
later, `YourApp::Application.assets` is a preconfigured
|
|
23
|
-
`Sprockets::Environment` instance. For Rack-based applications, create
|
|
24
|
-
an instance in `config.ru`.
|
|
24
|
+
These are the major features in Sprockets 4.x
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
- Source Maps
|
|
27
|
+
- Manifest.js
|
|
28
|
+
- ES6 support
|
|
29
|
+
- Deprecated processor interface in 3.x is removed in 4.x
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
Read more about them by referencing [Upgrading document](UPGRADING.md)
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
to search for assets.
|
|
33
|
+
## Guides
|
|
35
34
|
|
|
36
|
-
|
|
37
|
-
of a single directory containing your application's asset source
|
|
38
|
-
files. When mounted, the environment will serve assets from this
|
|
39
|
-
directory as if they were static files in your public root.
|
|
35
|
+
For most people interested in using Sprockets, you will want to see the README below.
|
|
40
36
|
|
|
41
|
-
|
|
42
|
-
files into multiple directories -- even directories that live outside
|
|
43
|
-
your application -- and combine those directories into a single
|
|
44
|
-
virtual filesystem. That means you can easily bundle JavaScript, CSS
|
|
45
|
-
and images into a Ruby library and import them into your application.
|
|
37
|
+
If you are a framework developer that is using Sprockets, see [Building an Asset Processing Framework](guides/building_an_asset_processing_framework.md).
|
|
46
38
|
|
|
47
|
-
|
|
39
|
+
If you are a library developer who is extending the functionality of Sprockets, see [Extending Sprockets](guides/extending_sprockets.md).
|
|
48
40
|
|
|
49
|
-
|
|
50
|
-
`append_path` and `prepend_path` methods. Directories at the beginning
|
|
51
|
-
of the load path have precedence over subsequent directories.
|
|
41
|
+
If you want to work on Sprockets or better understand how it works read [How Sprockets Works](guides/how_sprockets_works.md)
|
|
52
42
|
|
|
53
|
-
|
|
54
|
-
environment.append_path 'app/assets/javascripts'
|
|
55
|
-
environment.append_path 'lib/assets/javascripts'
|
|
56
|
-
environment.append_path 'vendor/assets/jquery'
|
|
43
|
+
## Behavior Overview
|
|
57
44
|
|
|
58
|
-
|
|
59
|
-
prepending for cases where you need to override existing assets.
|
|
45
|
+
You can interact with Sprockets primarily through directives and file extensions. This section covers how to use each of these things, and the defaults that ship with Sprockets.
|
|
60
46
|
|
|
61
|
-
|
|
47
|
+
Since you are likely using Sprockets through another framework (such as the [the Rails asset pipeline](http://guides.rubyonrails.org/asset_pipeline.html)), there will be configuration options you can toggle that will change behavior such as what directories or files get compiled. For that documentation you should see your framework's documentation.
|
|
62
48
|
|
|
63
|
-
|
|
64
|
-
environment as a Rack server and request assets via HTTP. You can also
|
|
65
|
-
access assets programmatically from within your application.
|
|
66
|
-
|
|
67
|
-
### Logical Paths ###
|
|
49
|
+
#### Accessing Assets
|
|
68
50
|
|
|
69
51
|
Assets in Sprockets are always referenced by their *logical path*.
|
|
70
52
|
|
|
@@ -74,391 +56,661 @@ contains the directory `app/assets/javascripts`:
|
|
|
74
56
|
|
|
75
57
|
<table>
|
|
76
58
|
<tr>
|
|
77
|
-
<th>Asset source file</th>
|
|
78
59
|
<th>Logical path</th>
|
|
60
|
+
<th>Source file on disk</th>
|
|
79
61
|
</tr>
|
|
80
62
|
<tr>
|
|
81
|
-
<td>app/assets/javascripts/application.js</td>
|
|
82
63
|
<td>application.js</td>
|
|
64
|
+
<td>app/assets/javascripts/application.js</td>
|
|
83
65
|
</tr>
|
|
84
66
|
<tr>
|
|
85
|
-
<td>app/assets/javascripts/models/project.js</td>
|
|
86
67
|
<td>models/project.js</td>
|
|
68
|
+
<td>app/assets/javascripts/models/project.js</td>
|
|
69
|
+
</tr>
|
|
70
|
+
<tr>
|
|
71
|
+
<td>hello.js</td>
|
|
72
|
+
<td>app/assets/javascripts/hello.coffee</td>
|
|
87
73
|
</tr>
|
|
88
74
|
</table>
|
|
89
75
|
|
|
90
|
-
|
|
91
|
-
virtual filesystem whose entries are logical paths.
|
|
76
|
+
> Note: For assets that are compiled or transpiled, you want to specify the extension that you want, not the extension on disk. For example we specified `hello.js` even if the file on disk is a coffeescript file, since the asset it will generate is javascript.
|
|
92
77
|
|
|
93
|
-
###
|
|
78
|
+
### Directives
|
|
94
79
|
|
|
95
|
-
|
|
96
|
-
logical paths underneath the *mount point*. For example, if you mount
|
|
97
|
-
your environment at `/assets` and request the URL
|
|
98
|
-
`/assets/application.js`, Sprockets will search your load path for the
|
|
99
|
-
file named `application.js` and serve it.
|
|
80
|
+
Directives are special comments in your asset file and the main way of interacting with processors. What kind of interactions? You can use these directives to tell Sprockets to load other files, or specify dependencies on other assets.
|
|
100
81
|
|
|
101
|
-
|
|
102
|
-
mounted at `/assets`. If you are using Sprockets with a Rack
|
|
103
|
-
application, you will need to mount the environment yourself. A good
|
|
104
|
-
way to do this is with the `map` method in `config.ru`:
|
|
82
|
+
For example, let's say you have custom JavaScript that you've written. You put this javascript in a file called `beta.js`. The javascript makes heavy use of jQuery, so you need to load that before your code executes. You could add a `require` directive to the top of `beta.js`:
|
|
105
83
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
environment = Sprockets::Environment.new
|
|
109
|
-
environment.append_path 'app/assets/javascripts'
|
|
110
|
-
environment.append_path 'app/assets/stylesheets'
|
|
111
|
-
run environment
|
|
112
|
-
end
|
|
84
|
+
```js
|
|
85
|
+
//= require jquery
|
|
113
86
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
87
|
+
$().ready({
|
|
88
|
+
// my custom code here
|
|
89
|
+
})
|
|
90
|
+
```
|
|
117
91
|
|
|
118
|
-
|
|
92
|
+
The directive processor understands comment blocks in three formats:
|
|
119
93
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
94
|
+
``` css
|
|
95
|
+
/* Multi-line comment blocks (CSS, SCSS, JavaScript)
|
|
96
|
+
*= require foo
|
|
97
|
+
*/
|
|
98
|
+
```
|
|
123
99
|
|
|
124
|
-
|
|
125
|
-
|
|
100
|
+
``` js
|
|
101
|
+
// Single-line comment blocks (SCSS, JavaScript)
|
|
102
|
+
//= require foo
|
|
103
|
+
```
|
|
126
104
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
105
|
+
``` coffee
|
|
106
|
+
# Single-line comment blocks (CoffeeScript)
|
|
107
|
+
#= require foo
|
|
108
|
+
```
|
|
130
109
|
|
|
131
|
-
|
|
110
|
+
> Note: Directives are only processed if they come before any application code. Once you have a line that does not include a comment or whitespace then Sprockets will stop looking for directives. If you use a directive outside of the "header" of the document it will not do anything, and won't raise any errors.
|
|
132
111
|
|
|
133
|
-
|
|
134
|
-
CoffeeScript, and automatically compiled to CSS or JavaScript by
|
|
135
|
-
Sprockets. Compilers for these languages are called *engines*.
|
|
112
|
+
Here is a list of the available directives:
|
|
136
113
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
`
|
|
140
|
-
|
|
114
|
+
- [`require`](#require) - Add the contents of a file to current
|
|
115
|
+
- [`require_self`](#require_self) - Change order of where current contents are concatenated to current
|
|
116
|
+
- [`require_directory`](#require_directory) - Add contents of each file in a folder to current
|
|
117
|
+
- [`require_tree`](#require_tree) - Add contents of all files in all directories in a path to current
|
|
118
|
+
- [`link`](#link) - Make target file compile and be publicly available without adding contents to current
|
|
119
|
+
- [`link_directory`](#link_directory) - Make target directory compile and be publicly available without adding contents to current
|
|
120
|
+
- [`link_tree`](#link_tree) - Make target tree compile and be publicly available without adding contents to current
|
|
121
|
+
- [`depend_on`](#depend_on) - Recompile current file if target has changed
|
|
122
|
+
- [`depend_on_directory`](#depend_on_directory) - Recompile current file if any files in target directory has changed
|
|
123
|
+
- [`stub`](#stub) - Ignore target file
|
|
141
124
|
|
|
142
|
-
|
|
125
|
+
You can see what each of these does below.
|
|
143
126
|
|
|
144
|
-
|
|
145
|
-
adds features like nested rules, variables, mixins and selector
|
|
146
|
-
inheritance.
|
|
127
|
+
### Specifying Processors through File Extensions
|
|
147
128
|
|
|
148
|
-
|
|
149
|
-
to write CSS assets in Sprockets.
|
|
129
|
+
Sprockets uses the filename extensions to determine what processors to run on your file and in what order. For example if you have a file:
|
|
150
130
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
131
|
+
```
|
|
132
|
+
application.scss
|
|
133
|
+
```
|
|
154
134
|
|
|
155
|
-
|
|
135
|
+
Then Sprockets will by default run the sass processor (which implements scss). The output file will be converted to css.
|
|
156
136
|
|
|
157
|
-
[
|
|
158
|
-
variables, mixins, operations and functions.
|
|
137
|
+
You can specify multiple processors by specifying multiple file extensions. For example you can use Ruby's [ERB template language](#invoking-ruby-with-erb) to embed content in your doc before running the sass processor. To accomplish this you would need to name your file
|
|
159
138
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
depends on `therubyracer` which embeds the V8 JavaScript runtime in
|
|
164
|
-
Ruby.
|
|
139
|
+
```
|
|
140
|
+
application.scss.erb
|
|
141
|
+
```
|
|
165
142
|
|
|
166
|
-
|
|
143
|
+
Processors are run from right to left (tail to head), so in the above example the processor associated with `erb` will be run before the processor associated with `scss` extension.
|
|
167
144
|
|
|
168
|
-
|
|
145
|
+
For a description of the processors that Sprockets has by default see the "default processors" section below. Other libraries may register additional processors.
|
|
169
146
|
|
|
170
|
-
|
|
171
|
-
language that compiles to the "good parts" of JavaScript, featuring a
|
|
172
|
-
cleaner syntax with array comprehensions, classes, and function
|
|
173
|
-
binding.
|
|
147
|
+
When "asking" for a compiled file, you always ask for the extension you want. For example if you're using Rails, to get the contents of `application.scss.erb` you would use
|
|
174
148
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
an [ExecJS](https://github.com/sstephenson/execjs)-supported runtime
|
|
179
|
-
on your system to invoke it.
|
|
149
|
+
```
|
|
150
|
+
asset_path("application.css")
|
|
151
|
+
```
|
|
180
152
|
|
|
181
|
-
|
|
182
|
-
`.js.coffee`.
|
|
153
|
+
Sprockets understands that `application.scss.erb` will compile down to a `application.css`. Ask for what you need, not what you have.
|
|
183
154
|
|
|
184
|
-
|
|
155
|
+
If this isn't working like you expect, make sure you didn't typo an extension, and make sure the file is on a "load path" (see framework docs for adding new load paths).
|
|
185
156
|
|
|
186
|
-
|
|
187
|
-
strings or markup. JavaScript templates have the special format
|
|
188
|
-
extension `.jst` and are compiled to JavaScript functions.
|
|
157
|
+
## File Order Processing
|
|
189
158
|
|
|
190
|
-
|
|
191
|
-
logical path as a property on the global `JST` object. Invoke a
|
|
192
|
-
template function to render the template as a string. The resulting
|
|
193
|
-
string can then be inserted into the DOM.
|
|
159
|
+
By default files are processed in alphabetical order. This behavior can impact your asset compilation when one asset needs to be loaded before another.
|
|
194
160
|
|
|
195
|
-
|
|
196
|
-
<div>Hello, <span><%= name %></span>!</div>
|
|
161
|
+
For example if you have an `application.js` and it loads another directory
|
|
197
162
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
163
|
+
```js
|
|
164
|
+
//= require_directory my_javascript
|
|
165
|
+
```
|
|
201
166
|
|
|
202
|
-
|
|
203
|
-
[EJS](https://github.com/sstephenson/ruby-ejs), for embedded
|
|
204
|
-
JavaScript, and [Eco](https://github.com/sstephenson/ruby-eco), for
|
|
205
|
-
embedded CoffeeScript. Both languages use the familiar `<% … %>`
|
|
206
|
-
syntax for embedding logic in templates.
|
|
167
|
+
The files in that directory will be loaded in alphabetical order. If the directory looks like this:
|
|
207
168
|
|
|
208
|
-
|
|
209
|
-
|
|
169
|
+
```sh
|
|
170
|
+
$ ls -1 my_javascript/
|
|
210
171
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
outlined above for the CoffeeScript engine.
|
|
172
|
+
alpha.js
|
|
173
|
+
beta.js
|
|
174
|
+
jquery.js
|
|
175
|
+
```
|
|
216
176
|
|
|
217
|
-
|
|
177
|
+
Then `alpha.js` will be loaded before either of the other two. This can be a problem if `alpha.js` uses jquery. For this reason it is not recommend to use `require_directory` with files that are ordering dependent. You can either require individual files manually:
|
|
218
178
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
179
|
+
```js
|
|
180
|
+
//= require jquery
|
|
181
|
+
//= require alpha
|
|
182
|
+
//= require beta
|
|
183
|
+
```
|
|
222
184
|
|
|
223
|
-
|
|
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`.
|
|
185
|
+
Or you can use index files to proxy your folders.
|
|
227
186
|
|
|
228
|
-
|
|
229
|
-
`Sprockets::Context` instance for the given asset. Common uses for ERB
|
|
230
|
-
include:
|
|
187
|
+
### Index files are proxies for folders
|
|
231
188
|
|
|
232
|
-
|
|
233
|
-
`asset_data_uri` helper
|
|
234
|
-
- inserting the URL to another asset, such as with the `asset_path`
|
|
235
|
-
helper provided by the Sprockets Rails plugin
|
|
236
|
-
- embedding other application resources, such as a localized string
|
|
237
|
-
database, in a JavaScript asset via JSON
|
|
238
|
-
- embedding version constants loaded from another file
|
|
189
|
+
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:
|
|
239
190
|
|
|
240
|
-
|
|
241
|
-
|
|
191
|
+
```erb
|
|
192
|
+
<%= asset_path("foo/index.js") %>
|
|
193
|
+
```
|
|
242
194
|
|
|
243
|
-
|
|
195
|
+
Instead you would need to use:
|
|
244
196
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
197
|
+
```erb
|
|
198
|
+
<%= asset_path("foo.js") %>
|
|
199
|
+
```
|
|
248
200
|
|
|
249
|
-
|
|
201
|
+
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 .`
|
|
250
202
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
203
|
+
```js
|
|
204
|
+
//= require_tree .
|
|
205
|
+
```
|
|
254
206
|
|
|
255
|
-
Sprockets
|
|
256
|
-
them to recursively build a dependency graph. When you request an
|
|
257
|
-
asset with dependencies, the dependencies will be included in order at
|
|
258
|
-
the top of the file.
|
|
207
|
+
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.
|
|
259
208
|
|
|
260
|
-
|
|
209
|
+
For example, if you have an `application.js` and want all the files in the `foo/` folder you could do this:
|
|
261
210
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
211
|
+
```js
|
|
212
|
+
//= require foo.js
|
|
213
|
+
```
|
|
265
214
|
|
|
266
|
-
|
|
267
|
-
//= require jquery-ui
|
|
268
|
-
//= require backbone
|
|
269
|
-
//= require_tree .
|
|
215
|
+
Then create a file `foo/index.js` that requires all the files in that folder in any order you want using relative references:
|
|
270
216
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
217
|
+
```js
|
|
218
|
+
//= require ./foo.min.js
|
|
219
|
+
//= require ./foo-ui.js
|
|
220
|
+
```
|
|
275
221
|
|
|
276
|
-
|
|
277
|
-
asset, but directive comments are stripped after
|
|
278
|
-
processing. Sprockets will not look for directives in comment blocks
|
|
279
|
-
that occur after the first line of code.
|
|
222
|
+
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.
|
|
280
223
|
|
|
281
|
-
|
|
224
|
+
## Cache
|
|
282
225
|
|
|
283
|
-
|
|
226
|
+
Compiling assets is slow. It requires a lot of disk use to pull assets off of hard drives, a lot of RAM to manipulate those files in memory, and a lot of CPU for compilation operations. Because of this Sprockets has a cache to speed up asset compilation times. That's the good news. The bad news, is that sprockets has a cache and if you've found a bug it's likely going to involve the cache.
|
|
284
227
|
|
|
285
|
-
|
|
286
|
-
*= require foo
|
|
287
|
-
*/
|
|
228
|
+
By default Sprockets uses the file system to cache assets. It makes sense that Sprockets does not want to generate assets that already exist on disk in `public/assets`, what might not be as intuitive is that Sprockets needs to cache "partial" assets.
|
|
288
229
|
|
|
289
|
-
|
|
290
|
-
//= require foo
|
|
230
|
+
For example if you have an `application.js` and it is made up of `a.js`, `b.js`, all the way to `z.js`
|
|
291
231
|
|
|
292
|
-
|
|
293
|
-
|
|
232
|
+
```js
|
|
233
|
+
//= require a.js
|
|
234
|
+
//= require b.js
|
|
235
|
+
# ...
|
|
236
|
+
//= require z.js
|
|
237
|
+
```
|
|
294
238
|
|
|
295
|
-
|
|
239
|
+
The first time this file is compiled the `application.js` output will be written to disk, but also intermediary compiled files for `a.js` etc. will be written to the cache directory (usually `tmp/cache/assets`).
|
|
296
240
|
|
|
297
|
-
|
|
298
|
-
source files.
|
|
241
|
+
So, if `b.js` changes it will get recompiled. However instead of having to recompile the other files from `a.js` to `z.js` since they did not change, we can use the prior intermediary files stored in the cached values . If these files were expensive to generate, then this "partial" asset cache strategy can save a lot of time.
|
|
299
242
|
|
|
300
|
-
|
|
301
|
-
logical path or a relative path. Relative paths begin with `./` and
|
|
302
|
-
reference files relative to the location of the current file.
|
|
243
|
+
Directives such as `require`, `link`, `depend_on`, and `depend_on_directory` tell Sprockets what assets need to be re-compiled when a file changes. Files are considered "fresh" based on their mtime on disk and a combination of cache keys.
|
|
303
244
|
|
|
304
|
-
|
|
245
|
+
On Rails you can force a "clean" install by clearing the `public/assets` and `tmp/cache/assets` directories.
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
## Default Directives
|
|
249
|
+
|
|
250
|
+
Directives take a path or a path to a file. Paths for directive can be relative to the current file, for example:
|
|
251
|
+
|
|
252
|
+
```js
|
|
253
|
+
//= require ../foo.js
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
This would load the file up one directory and named `foo.js`. However this isn't required if `foo.js` is on one of Sprocket's load paths. You can simply use
|
|
257
|
+
|
|
258
|
+
```js
|
|
259
|
+
//= require foo.js
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
Without any prepended dots and sprockets will search for the asset. If the asset is on a sub-path of the load path, you can specify it without using a relative path as well:
|
|
263
|
+
|
|
264
|
+
```js
|
|
265
|
+
//= require sub/path/foo.js
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
You can also use an absolute path, but this is discouraged unless you know the directory structure of every machine you plan on running code on.
|
|
269
|
+
|
|
270
|
+
Below is a section for each of the built in directive types supported by Sprockets.
|
|
271
|
+
|
|
272
|
+
### require
|
|
305
273
|
|
|
306
274
|
`require` *path* inserts the contents of the asset source file
|
|
307
275
|
specified by *path*. If the file is required multiple times, it will
|
|
308
276
|
appear in the bundle only once.
|
|
309
277
|
|
|
310
|
-
|
|
278
|
+
**Example:**
|
|
279
|
+
|
|
280
|
+
If you've got an `a.js`:
|
|
281
|
+
|
|
282
|
+
```js
|
|
283
|
+
var a = "A";
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
and a `b.js`;
|
|
287
|
+
|
|
288
|
+
```js
|
|
289
|
+
var b = "B";
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
Then you could require both of these in an `application.js`
|
|
293
|
+
|
|
294
|
+
```js
|
|
295
|
+
//= require a.js
|
|
296
|
+
//= require b.js
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
Which would generate one concatenated file:
|
|
300
|
+
|
|
301
|
+
```js
|
|
302
|
+
var a = "A";
|
|
303
|
+
var b = "B";
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### require_self
|
|
307
|
+
|
|
308
|
+
`require_self` tells Sprockets to insert the body of the current
|
|
309
|
+
source file before any subsequent `require` directives.
|
|
310
|
+
|
|
311
|
+
**Example:**
|
|
311
312
|
|
|
312
|
-
|
|
313
|
-
specified source file even if it has already been included or
|
|
314
|
-
required.
|
|
313
|
+
If you've got an `a.js`:
|
|
315
314
|
|
|
316
|
-
|
|
315
|
+
```js
|
|
316
|
+
var a = "A";
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
And an `application.js`
|
|
320
|
+
|
|
321
|
+
```js
|
|
322
|
+
//= require_self
|
|
323
|
+
//= require 'a.js'
|
|
324
|
+
|
|
325
|
+
var app_name = "Sprockets";
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
Then this will take the contents of `application.js` (that come after the last require) and put them at the beginning of the file:
|
|
329
|
+
|
|
330
|
+
```js
|
|
331
|
+
var app_name = "Sprockets";
|
|
332
|
+
var a = "A";
|
|
333
|
+
```
|
|
334
|
+
|
|
335
|
+
### require_directory
|
|
317
336
|
|
|
318
337
|
`require_directory` *path* requires all source files of the same
|
|
319
338
|
format in the directory specified by *path*. Files are required in
|
|
320
339
|
alphabetical order.
|
|
321
340
|
|
|
322
|
-
|
|
341
|
+
**Example:**
|
|
342
|
+
|
|
343
|
+
If we've got a directory called `alphabet` with an `a.js` and `b.js` files like before, then our `application.js`
|
|
344
|
+
|
|
345
|
+
```js
|
|
346
|
+
//= require_directory alphabet
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
Would produce:
|
|
350
|
+
|
|
351
|
+
```js
|
|
352
|
+
var a = "A";
|
|
353
|
+
var b = "B";
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
You can also see [Index files are proxies for folders](#index-files-are-proxies-for-folders) for another method of organizing folders that will give you more control.
|
|
357
|
+
|
|
358
|
+
### require_tree
|
|
323
359
|
|
|
324
360
|
`require_tree` *path* works like `require_directory`, but operates
|
|
325
361
|
recursively to require all files in all subdirectories of the
|
|
326
362
|
directory specified by *path*.
|
|
327
363
|
|
|
328
|
-
###
|
|
364
|
+
### link
|
|
329
365
|
|
|
330
|
-
`
|
|
331
|
-
|
|
366
|
+
`link` *path* declares a dependency on the target *path* and adds it to a list
|
|
367
|
+
of subdependencies to be compiled when the asset is written out to
|
|
368
|
+
disk.
|
|
369
|
+
|
|
370
|
+
Example:
|
|
371
|
+
|
|
372
|
+
If you've got a `manifest.js` file and you want to specify that a `admin.js` source file should be
|
|
373
|
+
generated and made available to the public you can link it by including this in the `manifest.js` file:
|
|
374
|
+
|
|
375
|
+
```
|
|
376
|
+
//= link admin.js
|
|
377
|
+
```
|
|
378
|
+
|
|
379
|
+
The argument to `link` is a _logical path_, that is it will be resolved according to the
|
|
380
|
+
configured asset load paths. See [Accessing Assets](#accessing-assets) above. A path relative to
|
|
381
|
+
the current file won't work, it must be a logical path.
|
|
382
|
+
|
|
383
|
+
**Caution**: the "link" directive should always have an explicit extension on the end.
|
|
384
|
+
|
|
385
|
+
`link` can also be used to include manifest files from mounted Rails engines:
|
|
386
|
+
|
|
387
|
+
```
|
|
388
|
+
//= link my_engine_manifest
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
This would find a manifest file at `my_engine/app/assets/config/my_engine_manifest.js` and include its directives.
|
|
392
|
+
|
|
393
|
+
### link_directory
|
|
332
394
|
|
|
333
|
-
|
|
395
|
+
`link_directory` *path* links all the files inside the directory specified by the *path*. By "link", we mean they are specified as compilation targets to be written out to disk, and made available to be served to user-agents.
|
|
396
|
+
|
|
397
|
+
Files in subdirectories will not be linked (Compare to [link_tree](#link_tree)).
|
|
398
|
+
|
|
399
|
+
The *path* argument to `link_directory` is _not_ a logical path (it does not use the asset load paths), but is a path relative to the file the `link_directory` directive is found in, and can use `..` to . For instance, you might want:
|
|
400
|
+
|
|
401
|
+
```js
|
|
402
|
+
//= link_directory ../stylesheets
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
`link_directory` can take an optional second argument with an extension or content-type, with the
|
|
406
|
+
two arguments separated by a space:
|
|
407
|
+
|
|
408
|
+
```js
|
|
409
|
+
//= link_directory ../stylesheets text/css
|
|
410
|
+
//= link_directory ../more_stylesheets .css
|
|
411
|
+
```
|
|
412
|
+
|
|
413
|
+
This will limit the matching files to link to only files recognized as that type. An extension is
|
|
414
|
+
just a shortcut for the type referenced, it does not need to match the source file exactly, but
|
|
415
|
+
instead identifies the content-type the source file must be recognized as.
|
|
416
|
+
|
|
417
|
+
### link_tree
|
|
418
|
+
|
|
419
|
+
`link_tree` *path* works like [link_directory](#link_directory), but operates
|
|
420
|
+
recursively to link all files in all subdirectories of the
|
|
421
|
+
directory specified by *path*.
|
|
422
|
+
|
|
423
|
+
Example:
|
|
424
|
+
|
|
425
|
+
```js
|
|
426
|
+
//= link_tree ./path/to/folder
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
Like `link_directory`, the argument is path relative to the current file, it is *not* a 'logical path' tresolved against load paths.
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
As with `link_directory`, you can also specify a second argument -- separated by a space -- so any extra files not matching the content-type specified will be ignored:
|
|
433
|
+
|
|
434
|
+
```js
|
|
435
|
+
//= link_tree ./path/to/folder text/javascript
|
|
436
|
+
//= link_tree ./path/to/other_folder .js
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
### depend_on
|
|
334
441
|
|
|
335
442
|
`depend_on` *path* declares a dependency on the given *path* without
|
|
336
443
|
including it in the bundle. This is useful when you need to expire an
|
|
337
444
|
asset's cache in response to a change in another file.
|
|
338
445
|
|
|
339
|
-
|
|
446
|
+
**Example:**
|
|
447
|
+
|
|
448
|
+
If you have a file such as `bar.data` and you're using data from that file in another file, then
|
|
449
|
+
you need to tell sprockets that it needs to re-compile the file if `bar.data` changes:
|
|
450
|
+
|
|
451
|
+
```js
|
|
452
|
+
//= depend_on "bar.data"
|
|
453
|
+
|
|
454
|
+
var bar = '<%= File.read("bar.data") %>'
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
To depend on an entire directory containing multiple files, use `depend_on_directory`
|
|
458
|
+
|
|
459
|
+
### depend_on_asset
|
|
460
|
+
|
|
461
|
+
`depend_on_asset` *path* works like `depend_on`, but operates
|
|
462
|
+
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`.
|
|
340
463
|
|
|
341
|
-
|
|
464
|
+
### depend_on_directory
|
|
465
|
+
|
|
466
|
+
`depend_on_directory` *path* declares all files in the given *path* without
|
|
467
|
+
including them in the bundle. This is useful when you need to expire an
|
|
468
|
+
asset's cache in response to a change in multiple files in a single directory.
|
|
469
|
+
|
|
470
|
+
All paths are relative to your declaration and must begin with `./`
|
|
471
|
+
|
|
472
|
+
Also, your must include these directories in your [load path](guides/building_an_asset_processing_framework.md#the-load-path).
|
|
473
|
+
|
|
474
|
+
**Example:**
|
|
475
|
+
|
|
476
|
+
If we've got a directory called `data` with files `a.data` and `b.data`
|
|
477
|
+
|
|
478
|
+
```
|
|
479
|
+
// ./data/a.data
|
|
480
|
+
A
|
|
481
|
+
```
|
|
482
|
+
|
|
483
|
+
```
|
|
484
|
+
// ./data/b.data
|
|
485
|
+
B
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
```
|
|
489
|
+
// ./file.js.erb
|
|
490
|
+
//= depend_on_directory ./data
|
|
491
|
+
var a = '<% File.read('data/a.data') %>'
|
|
492
|
+
var b = '<% File.read('data/b.data') %>'
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
Would produce:
|
|
496
|
+
|
|
497
|
+
```js
|
|
498
|
+
var a = "A";
|
|
499
|
+
var b = "B";
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
You can also see [Index files are proxies for folders](#index-files-are-proxies-for-folders) for another method of organizing folders that will give you more control.
|
|
503
|
+
|
|
504
|
+
### stub
|
|
505
|
+
|
|
506
|
+
`stub` *path* excludes that asset and its dependencies from the asset bundle.
|
|
342
507
|
The *path* must be a valid asset and may or may not already be part
|
|
343
|
-
of the bundle.
|
|
344
|
-
|
|
508
|
+
of the bundle. `stub` should only be used at the top level bundle, not
|
|
509
|
+
within any subdependencies.
|
|
345
510
|
|
|
346
|
-
|
|
511
|
+
### Invoking Ruby with ERB
|
|
347
512
|
|
|
348
|
-
|
|
513
|
+
Sprockets provides an ERB engine for preprocessing assets using
|
|
514
|
+
embedded Ruby code. Append `.erb` to a CSS or JavaScript asset's
|
|
515
|
+
filename to enable the ERB engine.
|
|
349
516
|
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
copy of the latest code using Git:
|
|
517
|
+
For example if you have an `app/application/javascripts/app_name.js.erb`
|
|
518
|
+
you could have this in the template
|
|
353
519
|
|
|
354
|
-
|
|
520
|
+
```js
|
|
521
|
+
var app_name = "<%= ENV['APP_NAME'] %>";
|
|
522
|
+
```
|
|
355
523
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
524
|
+
Generated files are cached. If you're using an `ENV` var then
|
|
525
|
+
when you change then ENV var the asset will be forced to
|
|
526
|
+
recompile. This behavior is only true for environment variables,
|
|
527
|
+
if you are pulling a value from somewhere else, such as a database,
|
|
528
|
+
you must manually invalidate the cache to see the change.
|
|
361
529
|
|
|
362
|
-
|
|
530
|
+
If you're using Rails, there are helpers you can use such as `asset_url`
|
|
531
|
+
that will cause a recompile if the value changes.
|
|
363
532
|
|
|
364
|
-
|
|
533
|
+
For example if you have this in your `application.css`
|
|
365
534
|
|
|
366
|
-
|
|
535
|
+
``` css
|
|
536
|
+
.logo {
|
|
537
|
+
background: url(<%= asset_url("logo.png") %>)
|
|
538
|
+
}
|
|
539
|
+
```
|
|
367
540
|
|
|
368
|
-
|
|
541
|
+
When you modify the `logo.png` on disk, it will force `application.css` to be
|
|
542
|
+
recompiled so that the fingerprint will be correct in the generated asset.
|
|
369
543
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
544
|
+
You can manually make sprockets depend on any other file that is generated
|
|
545
|
+
by sprockets by using the `depend_on` or `depend_on_directory` directive. Rails
|
|
546
|
+
implements the above feature by auto calling `depend_on` on the original asset
|
|
547
|
+
when the `asset_url` is used inside of an asset.
|
|
373
548
|
|
|
374
|
-
|
|
549
|
+
### Styling with Sass and SCSS
|
|
375
550
|
|
|
376
|
-
|
|
551
|
+
[Sass](http://sass-lang.com/) is a language that compiles to CSS and
|
|
552
|
+
adds features like nested rules, variables, mixins and selector
|
|
553
|
+
inheritance.
|
|
377
554
|
|
|
378
|
-
|
|
555
|
+
If the `sass` gem is available to your application, you can use Sass
|
|
556
|
+
to write CSS assets in Sprockets.
|
|
379
557
|
|
|
380
|
-
|
|
381
|
-
|
|
558
|
+
Sprockets supports both Sass syntaxes. For the original
|
|
559
|
+
whitespace-sensitive syntax, use the extension `.sass`. For the
|
|
560
|
+
new SCSS syntax, use the extension `.scss`.
|
|
382
561
|
|
|
383
|
-
|
|
562
|
+
In Rails if you have `app/application/stylesheets/foo.scss` it can
|
|
563
|
+
be referenced with `<%= asset_path("foo.css") %>`. When referencing
|
|
564
|
+
an asset in Rails, always specify the extension you want. Sprockets will
|
|
565
|
+
convert `foo.scss` to `foo.css`.
|
|
384
566
|
|
|
385
|
-
|
|
386
|
-
* Include dependency paths in asset mtime
|
|
567
|
+
### Scripting with CoffeeScript
|
|
387
568
|
|
|
388
|
-
|
|
569
|
+
[CoffeeScript](http://jashkenas.github.io/coffeescript/) is a
|
|
570
|
+
language that compiles to the "good parts" of JavaScript, featuring a
|
|
571
|
+
cleaner syntax with array comprehensions, classes, and function
|
|
572
|
+
binding.
|
|
573
|
+
|
|
574
|
+
If the `coffee-script` gem is available to your application, you can
|
|
575
|
+
use CoffeeScript to write JavaScript assets in Sprockets. Note that
|
|
576
|
+
the CoffeeScript compiler is written in JavaScript, and you will need
|
|
577
|
+
an [ExecJS](https://github.com/rails/execjs)-supported runtime
|
|
578
|
+
on your system to invoke it.
|
|
579
|
+
|
|
580
|
+
To write JavaScript assets with CoffeeScript, use the extension
|
|
581
|
+
`.coffee`.
|
|
582
|
+
|
|
583
|
+
In Rails if you have `app/application/javascripts/foo.coffee` it can
|
|
584
|
+
be referenced with `<%= asset_path("foo.js") %>`. When referencing
|
|
585
|
+
an asset in Rails, always specify the extension you want. Sprockets will
|
|
586
|
+
convert `foo.coffee` to `foo.js`.
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
## ES6 Support
|
|
590
|
+
|
|
591
|
+
Sprockets 4 ships with a Babel processor. This allows you to transpile ECMAScript6 to JavaScript just like you would transpile CoffeeScript to JavaScript. To use this, modify your Gemfile:
|
|
592
|
+
|
|
593
|
+
```ruby
|
|
594
|
+
gem 'babel-transpiler'
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
Any asset with the extension `es6` will be treated as an ES6 file:
|
|
598
|
+
|
|
599
|
+
```es6
|
|
600
|
+
// app/assets/javascript/application.es6
|
|
601
|
+
|
|
602
|
+
var square = (n) => n * n
|
|
603
|
+
|
|
604
|
+
console.log(square);
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
Start a Rails server in development mode and visit `localhost:3000/assets/application.js`, and this asset will be transpiled to JavaScript:
|
|
608
|
+
|
|
609
|
+
```js
|
|
610
|
+
var square = function square(n) {
|
|
611
|
+
return n * n;
|
|
612
|
+
};
|
|
613
|
+
|
|
614
|
+
console.log(square);
|
|
615
|
+
```
|
|
389
616
|
|
|
390
|
-
* Fixed MultiJson feature detect
|
|
391
617
|
|
|
392
|
-
|
|
618
|
+
### JavaScript Templating with EJS and Eco
|
|
393
619
|
|
|
394
|
-
*
|
|
395
|
-
|
|
620
|
+
Sprockets supports *JavaScript templates* for client-side rendering of
|
|
621
|
+
strings or markup. JavaScript templates have the special format
|
|
622
|
+
extension `.jst` and are compiled to JavaScript functions.
|
|
396
623
|
|
|
397
|
-
|
|
624
|
+
When loaded, a JavaScript template function can be accessed by its
|
|
625
|
+
logical path as a property on the global `JST` object. Invoke a
|
|
626
|
+
template function to render the template as a string. The resulting
|
|
627
|
+
string can then be inserted into the DOM.
|
|
628
|
+
|
|
629
|
+
```
|
|
630
|
+
<!-- templates/hello.jst.ejs -->
|
|
631
|
+
<div>Hello, <span><%= name %></span>!</div>
|
|
632
|
+
|
|
633
|
+
// application.js
|
|
634
|
+
//= require templates/hello
|
|
635
|
+
$("#hello").html(JST["templates/hello"]({ name: "Sam" }));
|
|
636
|
+
```
|
|
637
|
+
|
|
638
|
+
Sprockets supports two JavaScript template languages:
|
|
639
|
+
[EJS](https://github.com/sstephenson/ruby-ejs), for embedded
|
|
640
|
+
JavaScript, and [Eco](https://github.com/sstephenson/ruby-eco), for
|
|
641
|
+
embedded CoffeeScript. Both languages use the familiar `<% … %>`
|
|
642
|
+
syntax for embedding logic in templates.
|
|
398
643
|
|
|
399
|
-
|
|
400
|
-
|
|
644
|
+
If the `ejs` gem is available to your application, you can use EJS
|
|
645
|
+
templates in Sprockets. EJS templates have the extension `.jst.ejs`.
|
|
401
646
|
|
|
402
|
-
|
|
647
|
+
If the `eco` gem is available to your application, you can use [Eco
|
|
648
|
+
templates](https://github.com/sstephenson/eco) in Sprockets. Eco
|
|
649
|
+
templates have the extension `.jst.eco`. Note that the `eco` gem
|
|
650
|
+
depends on the CoffeeScript compiler, so the same caveats apply as
|
|
651
|
+
outlined above for the CoffeeScript engine.
|
|
403
652
|
|
|
404
|
-
|
|
653
|
+
### Minifying Assets
|
|
405
654
|
|
|
406
|
-
|
|
655
|
+
Several JavaScript and CSS minifiers are available through shorthand.
|
|
407
656
|
|
|
408
|
-
|
|
409
|
-
* Added Asset#bytesize alias
|
|
410
|
-
* Security: Check path for forbidden access after unescaping
|
|
657
|
+
In Rails you will specify them with:
|
|
411
658
|
|
|
412
|
-
|
|
659
|
+
```ruby
|
|
660
|
+
config.assets.js_compressor = :terser
|
|
661
|
+
config.assets.css_compressor = :scss
|
|
662
|
+
```
|
|
413
663
|
|
|
414
|
-
|
|
664
|
+
If you're not using Rails, configure this directly on the "environment".
|
|
415
665
|
|
|
416
|
-
|
|
666
|
+
``` ruby
|
|
667
|
+
environment.js_compressor = :terser
|
|
668
|
+
environment.css_compressor = :scss
|
|
669
|
+
```
|
|
417
670
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
* Added json manifest log of compiled assets.
|
|
421
|
-
* Added `stub` directive that allows you to exclude files from the bundle.
|
|
422
|
-
* 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.
|
|
671
|
+
If you are using Sprockets directly with a Rack app, don't forget to add
|
|
672
|
+
the `terser` and `sass` gems to your Gemfile when using above options.
|
|
423
673
|
|
|
424
|
-
|
|
674
|
+
### Gzip
|
|
425
675
|
|
|
426
|
-
|
|
676
|
+
By default when Sprockets generates a compiled asset file it will also produce a gzipped copy of that file. Sprockets only gzips non-binary files such as CSS, javascript, and SVG files.
|
|
427
677
|
|
|
428
|
-
|
|
678
|
+
For example if Sprockets is generating
|
|
429
679
|
|
|
430
|
-
|
|
680
|
+
```
|
|
681
|
+
application-12345.css
|
|
682
|
+
```
|
|
431
683
|
|
|
432
|
-
|
|
684
|
+
Then it will also generate a compressed copy in
|
|
433
685
|
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
686
|
+
```
|
|
687
|
+
application-12345.css.gz
|
|
688
|
+
```
|
|
437
689
|
|
|
438
|
-
|
|
690
|
+
This behavior can be disabled, refer to your framework specific documentation.
|
|
439
691
|
|
|
440
|
-
|
|
441
|
-
* Make JST namespace configurable.
|
|
692
|
+
### Serving Assets
|
|
442
693
|
|
|
443
|
-
|
|
694
|
+
In production you should generate your assets to a directory on disk and serve them either via Nginx or a feature like Rail's `config.public_file_server.enabled = true`.
|
|
444
695
|
|
|
445
|
-
|
|
696
|
+
On Rails you can generate assets by running:
|
|
446
697
|
|
|
447
|
-
|
|
698
|
+
```term
|
|
699
|
+
$ RAILS_ENV=production rake assets:precompile
|
|
700
|
+
```
|
|
448
701
|
|
|
449
|
-
|
|
450
|
-
* Decode URIs as default internal.
|
|
451
|
-
* Fix symlinked asset directories.
|
|
702
|
+
In development Rails will serve assets from `Sprockets::Server`.
|
|
452
703
|
|
|
453
|
-
|
|
704
|
+
## Contributing to Sprockets
|
|
454
705
|
|
|
455
|
-
|
|
706
|
+
Sprockets is the work of hundreds of contributors. You're encouraged to submit pull requests, propose
|
|
707
|
+
features and discuss issues.
|
|
456
708
|
|
|
457
|
-
|
|
709
|
+
See [CONTRIBUTING](CONTRIBUTING.md).
|
|
458
710
|
|
|
459
|
-
|
|
711
|
+
### Version History
|
|
460
712
|
|
|
461
|
-
|
|
713
|
+
Please see the [CHANGELOG](https://github.com/rails/sprockets/tree/master/CHANGELOG.md)
|
|
462
714
|
|
|
463
|
-
|
|
464
|
-
|
|
715
|
+
## License
|
|
716
|
+
Sprockets is released under the [MIT License](MIT-LICENSE).
|