sprockets 4.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +72 -0
- data/README.md +665 -0
- data/bin/sprockets +93 -0
- data/lib/rake/sprocketstask.rb +153 -0
- data/lib/sprockets.rb +229 -0
- data/lib/sprockets/add_source_map_comment_to_asset_processor.rb +60 -0
- data/lib/sprockets/asset.rb +202 -0
- data/lib/sprockets/autoload.rb +16 -0
- 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/babel_processor.rb +66 -0
- data/lib/sprockets/base.rb +147 -0
- data/lib/sprockets/bower.rb +61 -0
- data/lib/sprockets/bundle.rb +105 -0
- data/lib/sprockets/cache.rb +271 -0
- data/lib/sprockets/cache/file_store.rb +208 -0
- data/lib/sprockets/cache/memory_store.rb +75 -0
- data/lib/sprockets/cache/null_store.rb +54 -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 +304 -0
- data/lib/sprockets/dependencies.rb +74 -0
- data/lib/sprockets/digest_utils.rb +200 -0
- data/lib/sprockets/directive_processor.rb +414 -0
- data/lib/sprockets/eco_processor.rb +33 -0
- data/lib/sprockets/ejs_processor.rb +32 -0
- data/lib/sprockets/encoding_utils.rb +262 -0
- data/lib/sprockets/environment.rb +46 -0
- data/lib/sprockets/erb_processor.rb +37 -0
- data/lib/sprockets/errors.rb +12 -0
- 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 +50 -0
- data/lib/sprockets/loader.rb +345 -0
- data/lib/sprockets/manifest.rb +338 -0
- data/lib/sprockets/manifest_utils.rb +48 -0
- data/lib/sprockets/mime.rb +96 -0
- 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 +82 -0
- data/lib/sprockets/preprocessors/default_source_map.rb +49 -0
- data/lib/sprockets/processing.rb +228 -0
- data/lib/sprockets/processor_utils.rb +169 -0
- data/lib/sprockets/resolve.rb +295 -0
- data/lib/sprockets/sass_cache_store.rb +30 -0
- data/lib/sprockets/sass_compressor.rb +63 -0
- data/lib/sprockets/sass_functions.rb +3 -0
- data/lib/sprockets/sass_importer.rb +3 -0
- 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 +295 -0
- 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 +191 -0
- data/lib/sprockets/utils.rb +202 -0
- data/lib/sprockets/utils/gzip.rb +99 -0
- data/lib/sprockets/version.rb +4 -0
- data/lib/sprockets/yui_compressor.rb +56 -0
- metadata +444 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7bbf26f3dfff9a86ca4b4407072f1786fafed4040a2f3f25c875842dc834bdcf
|
4
|
+
data.tar.gz: 5a6dd07a6568dca18b5d1eca850a1e3a78030c20a6530781d8a16ac31c25649c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fa3fb386fe8ded820388ff86e1b505736dadcf5e072fedc124c8635955af70df9db7386ce66f4e2d4f412c652e88df9c8dc93a451ac31b2982f0457393911202
|
7
|
+
data.tar.gz: 3dde983e601b16c7b4bc71343543c69b7b8787b70daaaad711bc45ec47b4dd53507d67f6b6a9c1f8de1a7069bfbc12f72c96a0eaa088f976187fe9d4ca8f78f8
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
**Master**
|
2
|
+
|
3
|
+
Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprockets/blob/master/UPGRADING.md
|
4
|
+
|
5
|
+
## 4.0.1
|
6
|
+
|
7
|
+
- Fix for Ruby 2.7 keyword arguments warning in `base.rb`. [#660](https://github.com/rails/sprockets/pull/660)
|
8
|
+
- Fix for when `x_sprockets_linecount` is missing from a source map.
|
9
|
+
- Fix subresource integrity to match the digest of the asset.
|
10
|
+
|
11
|
+
## 4.0.0
|
12
|
+
|
13
|
+
- Fixes for Ruby 2.7 keyword arguments warnings [#625](https://github.com/rails/sprockets/pull/625)
|
14
|
+
- Manifest files are sorted alphabetically [#626](https://github.com/rails/sprockets/pull/626)
|
15
|
+
|
16
|
+
## 4.0.0.beta10
|
17
|
+
|
18
|
+
- Fix YACB (Yet Another Caching Bug) [Fix broken expansion of asset link paths](https://github.com/rails/sprockets/pull/614)
|
19
|
+
|
20
|
+
## 4.0.0.beta9
|
21
|
+
|
22
|
+
- Minimum Ruby version for Sprockets 4 is now 2.5+ which matches minimum ruby verision of Rails [#604]
|
23
|
+
- Fix threading bug introduced in Sprockets 4 [#603]
|
24
|
+
- Warn when two potential manifest files exist. [#560]
|
25
|
+
|
26
|
+
## 4.0.0.beta8
|
27
|
+
|
28
|
+
- Security release for [CVE-2018-3760](https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-3760)
|
29
|
+
|
30
|
+
## 4.0.0.beta7
|
31
|
+
|
32
|
+
- Fix a year long bug that caused `Sprockets::FileNotFound` errors when the asset was present [#547]
|
33
|
+
- Raise an error when two assets such as foo.js and foo.js.erb would produce the same output artifact (foo.js) [#549 #530]
|
34
|
+
- Process `*.jst.eco.erb` files with ERBProcessor
|
35
|
+
|
36
|
+
## 4.0.0.beta6
|
37
|
+
|
38
|
+
- Fix source map line offsets [#515]
|
39
|
+
- Return a `400 Bad Request` when the path encoding is invalid. [#514]
|
40
|
+
|
41
|
+
## 4.0.0.beta5
|
42
|
+
|
43
|
+
- Reduce string allocations
|
44
|
+
- Source map metadata uses compressed form specified by the [source map v3 spec](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k). [#402] **[BREAKING]**
|
45
|
+
- Generate [index maps](https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit#heading=h.535es3xeprgt) when decoding source maps isn't necessary. [#402]
|
46
|
+
- Remove fingerprints from source map files. [#402]
|
47
|
+
|
48
|
+
## 4.0.0.beta4
|
49
|
+
|
50
|
+
- Changing the version now busts the digest of all assets [#404]
|
51
|
+
- Exporter interface added [#386]
|
52
|
+
- Using ENV vars in templates will recompile templates when the env vars change. [#365]
|
53
|
+
- Source maps for imported sass files with sassc is now fixed [#391]
|
54
|
+
- Load paths now in error messages [#322]
|
55
|
+
- Cache key added to babel processor [#387]
|
56
|
+
- `Environment#find_asset!` can now be used to raise an exception when asset could not be found [#379]
|
57
|
+
|
58
|
+
## 4.0.0.beta3
|
59
|
+
|
60
|
+
- Source Map fixes [#255] [#367]
|
61
|
+
- Performance improvements
|
62
|
+
|
63
|
+
## 4.0.0.beta2
|
64
|
+
|
65
|
+
- Fix load_paths on Sass processors [#223]
|
66
|
+
|
67
|
+
|
68
|
+
## 4.0.0.beta1
|
69
|
+
|
70
|
+
- Initial release of Sprockets 4
|
71
|
+
|
72
|
+
Please upgrade to the latest Sprockets 3 version before upgrading to Sprockets 4. Check the 3.x branch for previous changes https://github.com/rails/sprockets/blob/3.x/CHANGELOG.md.
|
data/README.md
ADDED
@@ -0,0 +1,665 @@
|
|
1
|
+
# Sprockets: Rack-based asset packaging
|
2
|
+
|
3
|
+
Sprockets is a Ruby library for compiling and serving web assets.
|
4
|
+
It features declarative dependency management for JavaScript and CSS
|
5
|
+
assets, as well as a powerful preprocessor pipeline that allows you to
|
6
|
+
write assets in languages like CoffeeScript, Sass and SCSS.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Install Sprockets from RubyGems:
|
11
|
+
|
12
|
+
``` sh
|
13
|
+
$ gem install sprockets
|
14
|
+
```
|
15
|
+
|
16
|
+
Or include it in your project's `Gemfile` with Bundler:
|
17
|
+
|
18
|
+
``` ruby
|
19
|
+
gem 'sprockets', '~> 4.0'
|
20
|
+
```
|
21
|
+
|
22
|
+
## Upgrading to Sprockets 4.x
|
23
|
+
|
24
|
+
These are the major features in Sprockets 4.x
|
25
|
+
|
26
|
+
- Source Maps
|
27
|
+
- Manifest.js
|
28
|
+
- ES6 support
|
29
|
+
- Deprecated processor interface in 3.x is removed in 4.x
|
30
|
+
|
31
|
+
Read more about them by referencing [Upgrading document](UPGRADING.md)
|
32
|
+
|
33
|
+
## Guides
|
34
|
+
|
35
|
+
For most people interested in using Sprockets, you will want to see the README below.
|
36
|
+
|
37
|
+
If you are a framework developer that is using Sprockets, see [Building an Asset Processing Framework](guides/building_an_asset_processing_framework.md).
|
38
|
+
|
39
|
+
If you are a library developer who is extending the functionality of Sprockets, see [Extending Sprockets](guides/extending_sprockets.md).
|
40
|
+
|
41
|
+
If you want to work on Sprockets or better understand how it works read [How Sprockets Works](guides/how_sprockets_works.md)
|
42
|
+
|
43
|
+
## Behavior Overview
|
44
|
+
|
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.
|
46
|
+
|
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.
|
48
|
+
|
49
|
+
#### Accessing Assets
|
50
|
+
|
51
|
+
Assets in Sprockets are always referenced by their *logical path*.
|
52
|
+
|
53
|
+
The logical path is the path of the asset source file relative to its
|
54
|
+
containing directory in the load path. For example, if your load path
|
55
|
+
contains the directory `app/assets/javascripts`:
|
56
|
+
|
57
|
+
<table>
|
58
|
+
<tr>
|
59
|
+
<th>Logical path</th>
|
60
|
+
<th>Source file on disk</th>
|
61
|
+
</tr>
|
62
|
+
<tr>
|
63
|
+
<td>application.js</td>
|
64
|
+
<td>app/assets/javascripts/application.js</td>
|
65
|
+
</tr>
|
66
|
+
<tr>
|
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>
|
73
|
+
</tr>
|
74
|
+
</table>
|
75
|
+
|
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.
|
77
|
+
|
78
|
+
### Directives
|
79
|
+
|
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.
|
81
|
+
|
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`:
|
83
|
+
|
84
|
+
```js
|
85
|
+
//= require jquery
|
86
|
+
|
87
|
+
$().ready({
|
88
|
+
// my custom code here
|
89
|
+
})
|
90
|
+
```
|
91
|
+
|
92
|
+
The directive processor understands comment blocks in three formats:
|
93
|
+
|
94
|
+
``` css
|
95
|
+
/* Multi-line comment blocks (CSS, SCSS, JavaScript)
|
96
|
+
*= require foo
|
97
|
+
*/
|
98
|
+
```
|
99
|
+
|
100
|
+
``` js
|
101
|
+
// Single-line comment blocks (SCSS, JavaScript)
|
102
|
+
//= require foo
|
103
|
+
```
|
104
|
+
|
105
|
+
``` coffee
|
106
|
+
# Single-line comment blocks (CoffeeScript)
|
107
|
+
#= require foo
|
108
|
+
```
|
109
|
+
|
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.
|
111
|
+
|
112
|
+
Here is a list of the available directives:
|
113
|
+
|
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
|
+
- [`stub`](#stub) - Ignore target file
|
123
|
+
|
124
|
+
You can see what each of these does below.
|
125
|
+
|
126
|
+
### Specifying Processors through File Extensions
|
127
|
+
|
128
|
+
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:
|
129
|
+
|
130
|
+
```
|
131
|
+
application.scss
|
132
|
+
```
|
133
|
+
|
134
|
+
Then Sprockets will by default run the sass processor (which implements scss). The output file will be converted to css.
|
135
|
+
|
136
|
+
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
|
137
|
+
|
138
|
+
```
|
139
|
+
application.scss.erb
|
140
|
+
```
|
141
|
+
|
142
|
+
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.
|
143
|
+
|
144
|
+
For a description of the processors that Sprockets has by default see the "default processors" section below. Other libraries may register additional processors.
|
145
|
+
|
146
|
+
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
|
147
|
+
|
148
|
+
```
|
149
|
+
asset_path("application.css")
|
150
|
+
```
|
151
|
+
|
152
|
+
Sprockets understands that `application.scss.erb` will compile down to a `application.css`. Ask for what you need, not what you have.
|
153
|
+
|
154
|
+
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).
|
155
|
+
|
156
|
+
## File Order Processing
|
157
|
+
|
158
|
+
By default files are processed in alphabetical order. This behavior can impact your asset compilation when one asset needs to be loaded before another.
|
159
|
+
|
160
|
+
For example if you have an `application.js` and it loads another directory
|
161
|
+
|
162
|
+
```js
|
163
|
+
//= require_directory my_javascript
|
164
|
+
```
|
165
|
+
|
166
|
+
The files in that directory will be loaded in alphabetical order. If the directory looks like this:
|
167
|
+
|
168
|
+
```sh
|
169
|
+
$ ls -1 my_javascript/
|
170
|
+
|
171
|
+
alpha.js
|
172
|
+
beta.js
|
173
|
+
jquery.js
|
174
|
+
```
|
175
|
+
|
176
|
+
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:
|
177
|
+
|
178
|
+
```js
|
179
|
+
//= require jquery
|
180
|
+
//= require alpha
|
181
|
+
//= require beta
|
182
|
+
```
|
183
|
+
|
184
|
+
Or you can use index files to proxy your folders.
|
185
|
+
|
186
|
+
### Index files are proxies for folders
|
187
|
+
|
188
|
+
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:
|
189
|
+
|
190
|
+
```erb
|
191
|
+
<%= asset_path("foo/index.js") %>
|
192
|
+
```
|
193
|
+
|
194
|
+
Instead you would need to use:
|
195
|
+
|
196
|
+
```erb
|
197
|
+
<%= asset_path("foo.js") %>
|
198
|
+
```
|
199
|
+
|
200
|
+
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 .`
|
201
|
+
|
202
|
+
```js
|
203
|
+
//= require_tree .
|
204
|
+
```
|
205
|
+
|
206
|
+
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.
|
207
|
+
|
208
|
+
For example, if you have an `application.js` and want all the files in the `foo/` folder you could do this:
|
209
|
+
|
210
|
+
```js
|
211
|
+
//= require foo.js
|
212
|
+
```
|
213
|
+
|
214
|
+
Then create a file `foo/index.js` that requires all the files in that folder in any order you want using relative references:
|
215
|
+
|
216
|
+
```js
|
217
|
+
//= require ./foo.min.js
|
218
|
+
//= require ./foo-ui.js
|
219
|
+
```
|
220
|
+
|
221
|
+
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.
|
222
|
+
|
223
|
+
## Cache
|
224
|
+
|
225
|
+
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.
|
226
|
+
|
227
|
+
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.
|
228
|
+
|
229
|
+
For example if you have an `application.js` and it is made up of `a.js`, `b.js`, all the way to `z.js`
|
230
|
+
|
231
|
+
```js
|
232
|
+
//= require a.js
|
233
|
+
//= require b.js
|
234
|
+
# ...
|
235
|
+
//= require z.js
|
236
|
+
```
|
237
|
+
|
238
|
+
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`).
|
239
|
+
|
240
|
+
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.
|
241
|
+
|
242
|
+
Directives such as `require`, `link`, and `depend_on` 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.
|
243
|
+
|
244
|
+
On Rails you can force a "clean" install by clearing the `public/assets` and `tmp/cache/assets` directories.
|
245
|
+
|
246
|
+
|
247
|
+
## Default Directives
|
248
|
+
|
249
|
+
Directives take a path or a path to a file. Paths for directive can be relative to the current file, for example:
|
250
|
+
|
251
|
+
```js
|
252
|
+
//= require ../foo.js
|
253
|
+
```
|
254
|
+
|
255
|
+
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
|
256
|
+
|
257
|
+
```js
|
258
|
+
//= require foo.js
|
259
|
+
```
|
260
|
+
|
261
|
+
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:
|
262
|
+
|
263
|
+
```js
|
264
|
+
//= require sub/path/foo.js
|
265
|
+
```
|
266
|
+
|
267
|
+
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.
|
268
|
+
|
269
|
+
Below is a section for each of the built in directive types supported by Sprockets.
|
270
|
+
|
271
|
+
### require
|
272
|
+
|
273
|
+
`require` *path* inserts the contents of the asset source file
|
274
|
+
specified by *path*. If the file is required multiple times, it will
|
275
|
+
appear in the bundle only once.
|
276
|
+
|
277
|
+
**Example:**
|
278
|
+
|
279
|
+
If you've got an `a.js`:
|
280
|
+
|
281
|
+
```js
|
282
|
+
var a = "A";
|
283
|
+
```
|
284
|
+
|
285
|
+
and a `b.js`;
|
286
|
+
|
287
|
+
```js
|
288
|
+
var b = "B";
|
289
|
+
```
|
290
|
+
|
291
|
+
Then you could require both of these in an `application.js`
|
292
|
+
|
293
|
+
```js
|
294
|
+
//= require a.js
|
295
|
+
//= require b.js
|
296
|
+
```
|
297
|
+
|
298
|
+
Which would generate one concatenated file:
|
299
|
+
|
300
|
+
```js
|
301
|
+
var a = "A";
|
302
|
+
var b = "B";
|
303
|
+
```
|
304
|
+
|
305
|
+
### require_self
|
306
|
+
|
307
|
+
`require_self` tells Sprockets to insert the body of the current
|
308
|
+
source file before any subsequent `require` directives.
|
309
|
+
|
310
|
+
**Example:**
|
311
|
+
|
312
|
+
If you've got an `a.js`:
|
313
|
+
|
314
|
+
```js
|
315
|
+
var a = "A";
|
316
|
+
```
|
317
|
+
|
318
|
+
And an `application.js`
|
319
|
+
|
320
|
+
```js
|
321
|
+
//= require_self
|
322
|
+
//= require 'a.js'
|
323
|
+
|
324
|
+
var app_name = "Sprockets";
|
325
|
+
```
|
326
|
+
|
327
|
+
Then this will take the contents of `application.js` (that come after the last require) and put them at the beginning of the file:
|
328
|
+
|
329
|
+
```js
|
330
|
+
var app_name = "Sprockets";
|
331
|
+
var a = "A";
|
332
|
+
```
|
333
|
+
|
334
|
+
### require_directory
|
335
|
+
|
336
|
+
`require_directory` *path* requires all source files of the same
|
337
|
+
format in the directory specified by *path*. Files are required in
|
338
|
+
alphabetical order.
|
339
|
+
|
340
|
+
**Example:**
|
341
|
+
|
342
|
+
If we've got a directory called `alphabet` with an `a.js` and `b.js` files like before, then our `application.js`
|
343
|
+
|
344
|
+
```js
|
345
|
+
//= require_directory alphabet
|
346
|
+
```
|
347
|
+
|
348
|
+
Would produce:
|
349
|
+
|
350
|
+
```js
|
351
|
+
var a = "A";
|
352
|
+
var b = "B";
|
353
|
+
```
|
354
|
+
|
355
|
+
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.
|
356
|
+
|
357
|
+
### require_tree
|
358
|
+
|
359
|
+
`require_tree` *path* works like `require_directory`, but operates
|
360
|
+
recursively to require all files in all subdirectories of the
|
361
|
+
directory specified by *path*.
|
362
|
+
|
363
|
+
### link
|
364
|
+
|
365
|
+
`link` *path* declares a dependency on the target *path* and adds it to a list
|
366
|
+
of subdependencies to be compiled when the asset is written out to
|
367
|
+
disk.
|
368
|
+
|
369
|
+
Example:
|
370
|
+
|
371
|
+
If you've got a `manifest.js` file and you want to specify that a `admin.js` source file should be
|
372
|
+
generated and made available to the public you can link it by including this in the `manifest.js` file:
|
373
|
+
|
374
|
+
```
|
375
|
+
//= link admin.js
|
376
|
+
```
|
377
|
+
|
378
|
+
The argument to `link` is a _logical path_, that is it will be resolved according to the
|
379
|
+
configured asset load paths. See [Accesing Assets](#accessing-assets) above. A path relative to
|
380
|
+
the current file won't work, it must be a logical path.
|
381
|
+
|
382
|
+
**Caution**: the "link" directive should always have an explicit extension on the end.
|
383
|
+
|
384
|
+
### link_directory
|
385
|
+
|
386
|
+
`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.
|
387
|
+
|
388
|
+
Files in subdirectories will not be linked (Compare to [link_tree](#link_tree)).
|
389
|
+
|
390
|
+
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:
|
391
|
+
|
392
|
+
```js
|
393
|
+
//= link_directory ../stylesheets
|
394
|
+
```
|
395
|
+
|
396
|
+
`link_directory` can take an optional second argument with an extension or content-type, with the
|
397
|
+
two arguments separated by a space:
|
398
|
+
|
399
|
+
```js
|
400
|
+
//= link_directory ../stylesheets text/css
|
401
|
+
//= link_directory ../more_stylesheets .css
|
402
|
+
```
|
403
|
+
|
404
|
+
This will limit the matching files to link to only files recognized as that type. An extension is
|
405
|
+
just a shortcut for the type referenced, it does not need to match the source file exactly, but
|
406
|
+
instead identifies the content-type the source file must be recognized as.
|
407
|
+
|
408
|
+
### link_tree
|
409
|
+
|
410
|
+
`link_tree` *path* works like [link_directory](#link_directory), but operates
|
411
|
+
recursively to link all files in all subdirectories of the
|
412
|
+
directory specified by *path*.
|
413
|
+
|
414
|
+
Example:
|
415
|
+
|
416
|
+
```js
|
417
|
+
//= link_tree ./path/to/folder
|
418
|
+
```
|
419
|
+
|
420
|
+
Like `link_directory`, the argument is path relative to the current file, it is *not* a 'logical path' tresolved against load paths.
|
421
|
+
|
422
|
+
|
423
|
+
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:
|
424
|
+
|
425
|
+
```js
|
426
|
+
//= link_tree ./path/to/folder text/javascript
|
427
|
+
//= link_tree ./path/to/other_folder .js
|
428
|
+
```
|
429
|
+
|
430
|
+
|
431
|
+
### depend_on
|
432
|
+
|
433
|
+
`depend_on` *path* declares a dependency on the given *path* without
|
434
|
+
including it in the bundle. This is useful when you need to expire an
|
435
|
+
asset's cache in response to a change in another file.
|
436
|
+
|
437
|
+
**Example:**
|
438
|
+
|
439
|
+
If you have a file such as `bar.data` and you're using data from that file in another file, then
|
440
|
+
you need to tell sprockets that it needs to re-compile the file if `bar.data` changes:
|
441
|
+
|
442
|
+
```js
|
443
|
+
//= depend_on "bar.data"
|
444
|
+
|
445
|
+
var bar = '<%= File.read("bar.data") %>'
|
446
|
+
```
|
447
|
+
|
448
|
+
### depend_on_asset
|
449
|
+
|
450
|
+
`depend_on_asset` *path* works like `depend_on`, but operates
|
451
|
+
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`.
|
452
|
+
|
453
|
+
### stub
|
454
|
+
|
455
|
+
`stub` *path* excludes that asset and its dependencies from the asset bundle.
|
456
|
+
The *path* must be a valid asset and may or may not already be part
|
457
|
+
of the bundle. `stub` should only be used at the top level bundle, not
|
458
|
+
within any subdependencies.
|
459
|
+
|
460
|
+
### Invoking Ruby with ERB
|
461
|
+
|
462
|
+
Sprockets provides an ERB engine for preprocessing assets using
|
463
|
+
embedded Ruby code. Append `.erb` to a CSS or JavaScript asset's
|
464
|
+
filename to enable the ERB engine.
|
465
|
+
|
466
|
+
For example if you have an `app/application/javascripts/app_name.js.erb`
|
467
|
+
you could have this in the template
|
468
|
+
|
469
|
+
```js
|
470
|
+
var app_name = "<%= ENV['APP_NAME'] %>";
|
471
|
+
```
|
472
|
+
|
473
|
+
Generated files are cached. If you're using an `ENV` var then
|
474
|
+
when you change then ENV var the asset will be forced to
|
475
|
+
recompile. This behavior is only true for environment variables,
|
476
|
+
if you are pulling a value from somewhere else, such as a database,
|
477
|
+
must manually invalidate the cache to see the change.
|
478
|
+
|
479
|
+
If you're using Rails, there are helpers you can use such as `asset_url`
|
480
|
+
that will cause a recompile if the value changes.
|
481
|
+
|
482
|
+
For example if you have this in your `application.css`
|
483
|
+
|
484
|
+
``` css
|
485
|
+
.logo {
|
486
|
+
background: url(<%= asset_url("logo.png") %>)
|
487
|
+
}
|
488
|
+
```
|
489
|
+
|
490
|
+
When you modify the `logo.png` on disk, it will force `application.css` to be
|
491
|
+
recompiled so that the fingerprint will be correct in the generated asset.
|
492
|
+
|
493
|
+
You can manually make sprockets depend on any other file that is generated
|
494
|
+
by sprockets by using the `depend_on` directive. Rails implements the above
|
495
|
+
feature by auto calling `depend_on` on the original asset when the `asset_url`
|
496
|
+
is used inside of an asset.
|
497
|
+
|
498
|
+
### Styling with Sass and SCSS
|
499
|
+
|
500
|
+
[Sass](http://sass-lang.com/) is a language that compiles to CSS and
|
501
|
+
adds features like nested rules, variables, mixins and selector
|
502
|
+
inheritance.
|
503
|
+
|
504
|
+
If the `sass` gem is available to your application, you can use Sass
|
505
|
+
to write CSS assets in Sprockets.
|
506
|
+
|
507
|
+
Sprockets supports both Sass syntaxes. For the original
|
508
|
+
whitespace-sensitive syntax, use the extension `.sass`. For the
|
509
|
+
new SCSS syntax, use the extension `.scss`.
|
510
|
+
|
511
|
+
In Rails if you have `app/application/stylesheets/foo.scss` it can
|
512
|
+
be referenced with `<%= asset_path("foo.css") %>`. When referencing
|
513
|
+
an asset in Rails, always specify the extension you want. Sprockets will
|
514
|
+
convert `foo.scss` to `foo.css`.
|
515
|
+
|
516
|
+
### Scripting with CoffeeScript
|
517
|
+
|
518
|
+
[CoffeeScript](http://jashkenas.github.io/coffeescript/) is a
|
519
|
+
language that compiles to the "good parts" of JavaScript, featuring a
|
520
|
+
cleaner syntax with array comprehensions, classes, and function
|
521
|
+
binding.
|
522
|
+
|
523
|
+
If the `coffee-script` gem is available to your application, you can
|
524
|
+
use CoffeeScript to write JavaScript assets in Sprockets. Note that
|
525
|
+
the CoffeeScript compiler is written in JavaScript, and you will need
|
526
|
+
an [ExecJS](https://github.com/rails/execjs)-supported runtime
|
527
|
+
on your system to invoke it.
|
528
|
+
|
529
|
+
To write JavaScript assets with CoffeeScript, use the extension
|
530
|
+
`.coffee`.
|
531
|
+
|
532
|
+
In Rails if you have `app/application/javascripts/foo.coffee` it can
|
533
|
+
be referenced with `<%= asset_path("foo.js") %>`. When referencing
|
534
|
+
an asset in Rails, always specify the extension you want. Sprockets will
|
535
|
+
convert `foo.coffee` to `foo.js`.
|
536
|
+
|
537
|
+
|
538
|
+
## ES6 Support
|
539
|
+
|
540
|
+
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:
|
541
|
+
|
542
|
+
```ruby
|
543
|
+
gem 'babel-transpiler'
|
544
|
+
```
|
545
|
+
|
546
|
+
Any asset with the extension `es6` will be treated as an ES6 file:
|
547
|
+
|
548
|
+
```es6
|
549
|
+
// app/assets/javascript/application.es6
|
550
|
+
|
551
|
+
var square = (n) => n * n
|
552
|
+
|
553
|
+
console.log(square);
|
554
|
+
```
|
555
|
+
|
556
|
+
Start a Rails server in development mode and visit `localhost:3000/assets/application.js`, and this asset will be transpiled to JavaScript:
|
557
|
+
|
558
|
+
```js
|
559
|
+
var square = function square(n) {
|
560
|
+
return n * n;
|
561
|
+
};
|
562
|
+
|
563
|
+
console.log(square);
|
564
|
+
```
|
565
|
+
|
566
|
+
|
567
|
+
### JavaScript Templating with EJS and Eco
|
568
|
+
|
569
|
+
Sprockets supports *JavaScript templates* for client-side rendering of
|
570
|
+
strings or markup. JavaScript templates have the special format
|
571
|
+
extension `.jst` and are compiled to JavaScript functions.
|
572
|
+
|
573
|
+
When loaded, a JavaScript template function can be accessed by its
|
574
|
+
logical path as a property on the global `JST` object. Invoke a
|
575
|
+
template function to render the template as a string. The resulting
|
576
|
+
string can then be inserted into the DOM.
|
577
|
+
|
578
|
+
```
|
579
|
+
<!-- templates/hello.jst.ejs -->
|
580
|
+
<div>Hello, <span><%= name %></span>!</div>
|
581
|
+
|
582
|
+
// application.js
|
583
|
+
//= require templates/hello
|
584
|
+
$("#hello").html(JST["templates/hello"]({ name: "Sam" }));
|
585
|
+
```
|
586
|
+
|
587
|
+
Sprockets supports two JavaScript template languages:
|
588
|
+
[EJS](https://github.com/sstephenson/ruby-ejs), for embedded
|
589
|
+
JavaScript, and [Eco](https://github.com/sstephenson/ruby-eco), for
|
590
|
+
embedded CoffeeScript. Both languages use the familiar `<% … %>`
|
591
|
+
syntax for embedding logic in templates.
|
592
|
+
|
593
|
+
If the `ejs` gem is available to your application, you can use EJS
|
594
|
+
templates in Sprockets. EJS templates have the extension `.jst.ejs`.
|
595
|
+
|
596
|
+
If the `eco` gem is available to your application, you can use [Eco
|
597
|
+
templates](https://github.com/sstephenson/eco) in Sprockets. Eco
|
598
|
+
templates have the extension `.jst.eco`. Note that the `eco` gem
|
599
|
+
depends on the CoffeeScript compiler, so the same caveats apply as
|
600
|
+
outlined above for the CoffeeScript engine.
|
601
|
+
|
602
|
+
### Minifying Assets
|
603
|
+
|
604
|
+
Several JavaScript and CSS minifiers are available through shorthand.
|
605
|
+
|
606
|
+
In Rails you will specify them with:
|
607
|
+
|
608
|
+
```ruby
|
609
|
+
config.assets.js_compressor = :uglify
|
610
|
+
config.assets.css_compressor = :scss
|
611
|
+
```
|
612
|
+
|
613
|
+
If you're not using Rails, configure this directly on the "environment".
|
614
|
+
|
615
|
+
``` ruby
|
616
|
+
environment.js_compressor = :uglify
|
617
|
+
environment.css_compressor = :scss
|
618
|
+
```
|
619
|
+
|
620
|
+
If you are using Sprockets directly with a Rack app, don't forget to add
|
621
|
+
the `uglifier` and `sass` gems to your Gemfile when using above options.
|
622
|
+
|
623
|
+
### Gzip
|
624
|
+
|
625
|
+
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.
|
626
|
+
|
627
|
+
For example if Sprockets is generating
|
628
|
+
|
629
|
+
```
|
630
|
+
application-12345.css
|
631
|
+
```
|
632
|
+
|
633
|
+
Then it will also generate a compressed copy in
|
634
|
+
|
635
|
+
```
|
636
|
+
application-12345.css.gz
|
637
|
+
```
|
638
|
+
|
639
|
+
This behavior can be disabled, refer to your framework specific documentation.
|
640
|
+
|
641
|
+
### Serving Assets
|
642
|
+
|
643
|
+
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`.
|
644
|
+
|
645
|
+
On Rails you can generate assets by running:
|
646
|
+
|
647
|
+
```term
|
648
|
+
$ RAILS_ENV=production rake assets:precompile
|
649
|
+
```
|
650
|
+
|
651
|
+
In development Rails will serve assets from `Sprockets::Server`.
|
652
|
+
|
653
|
+
## Contributing to Sprockets
|
654
|
+
|
655
|
+
Sprockets is the work of hundreds of contributors. You're encouraged to submit pull requests, propose
|
656
|
+
features and discuss issues.
|
657
|
+
|
658
|
+
See [CONTRIBUTING](CONTRIBUTING.md).
|
659
|
+
|
660
|
+
### Version History
|
661
|
+
|
662
|
+
Please see the [CHANGELOG](https://github.com/rails/sprockets/tree/master/CHANGELOG.md)
|
663
|
+
|
664
|
+
## License
|
665
|
+
Sprockets is released under the [MIT License](MIT-LICENSE).
|