darkroom 0.0.2 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +141 -21
- data/VERSION +1 -1
- data/lib/darkroom/asset.rb +239 -97
- data/lib/darkroom/darkroom.rb +54 -13
- data/lib/darkroom/delegates/css.rb +39 -0
- data/lib/darkroom/delegates/html.rb +53 -0
- data/lib/darkroom/delegates/htx.rb +21 -0
- data/lib/darkroom/delegates/javascript.rb +16 -0
- data/lib/darkroom/errors/asset_error.rb +33 -0
- data/lib/darkroom/errors/asset_not_found_error.rb +10 -21
- data/lib/darkroom/errors/circular_reference_error.rb +21 -0
- data/lib/darkroom/errors/duplicate_asset_error.rb +4 -4
- data/lib/darkroom/errors/invalid_path_error.rb +28 -0
- data/lib/darkroom/errors/missing_library_error.rb +3 -3
- data/lib/darkroom/errors/unrecognized_extension_error.rb +21 -0
- data/lib/darkroom/version.rb +1 -1
- data/lib/darkroom.rb +21 -28
- metadata +15 -6
- data/lib/darkroom/errors/spec_not_defined_error.rb +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c212cd979a1c11e4eb93a920a0b88bb20aae573fc8a0d290431203b77528eb3
|
4
|
+
data.tar.gz: a20ba0476f0508f574924e5d3d848a29f4adb550b4b5a1dd9e1ae16812c67a4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8b038fe72b9e644eb323d3b2d8afecd3d525e7796687827cc621d556c1e593ffb1b95529e4336070819f837031b8b1ad45d1241465365336ec59f51d494367e
|
7
|
+
data.tar.gz: fc3407836eb5a01317fb20177dc52ac1ea1e8a114cf955c136e46de2cf8d800ed344219e558f2baa5bb3ef1d3bce960d0b34d38f0574d00288de1fbef0fcd7f0
|
data/LICENSE
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright 2021 Nate Pickens
|
1
|
+
Copyright 2021-2022 Nate Pickens
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
4
4
|
documentation files (the "Software"), to deal in the Software without restriction, including without
|
data/README.md
CHANGED
@@ -6,6 +6,24 @@ performed for upload to a CDN or proxy server in production environments); this
|
|
6
6
|
simple and performant in development. Darkroom also supports asset bundling for CSS and JavaScript using
|
7
7
|
each language's native import statement syntax.
|
8
8
|
|
9
|
+
The following file types are supported out of the box, though support for others can be added (see the
|
10
|
+
[Extending](#extending) section):
|
11
|
+
|
12
|
+
| Name | Content Type | Extension(s) |
|
13
|
+
|------------|------------------|--------------|
|
14
|
+
| CSS | text/css | .css |
|
15
|
+
| HTML | text/html | .htm, .html |
|
16
|
+
| HTX | text/javascript | .htx |
|
17
|
+
| ICO | image/x-icon | .ico |
|
18
|
+
| JavaScript | text/javascript | .js |
|
19
|
+
| JPEG | image/jpeg | .jpg, .jpeg |
|
20
|
+
| JSON | application/json | .json |
|
21
|
+
| PNG | image/png | .png |
|
22
|
+
| SVG | image/svg+xml | .svg |
|
23
|
+
| Text | text/plain | .txt |
|
24
|
+
| WOFF | font/woff | .woff |
|
25
|
+
| WOFF2 | font/woff2 | .woff2 |
|
26
|
+
|
9
27
|
## Installation
|
10
28
|
|
11
29
|
Add this line to your Gemfile:
|
@@ -20,6 +38,17 @@ Or install manually on the command line:
|
|
20
38
|
gem install darkroom
|
21
39
|
```
|
22
40
|
|
41
|
+
Darkroom depends on a few other gems for compilation and minification of certain asset types, but does not
|
42
|
+
explicitly include them as dependencies since need for them varies from project to project. As such, if your
|
43
|
+
project includes HTX templates or you wish to minify CSS and/or JavaScript assets, the following will need
|
44
|
+
to be added to your Gemfile:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
gem('htx') # HTX compilation
|
48
|
+
gem('sassc') # CSS minification
|
49
|
+
gem('uglifier') # JavaScript and HTX minification
|
50
|
+
```
|
51
|
+
|
23
52
|
## Usage
|
24
53
|
|
25
54
|
To create and start using a Darkroom instance, specify one or more load paths (all other arguments are
|
@@ -27,50 +56,70 @@ optional):
|
|
27
56
|
|
28
57
|
```ruby
|
29
58
|
darkroom = Darkroom.new('app/assets', 'vendor/assets', '...',
|
30
|
-
hosts: [
|
59
|
+
hosts: [ # Hosts to prepend to asset paths (useful in production
|
60
|
+
'https://cname1.cdn.com', # when assets are served from a CDN with multiple
|
61
|
+
'https://cname2.cdn.com', # cnames); hosts are chosen round-robin per thread
|
62
|
+
'...',
|
63
|
+
],
|
31
64
|
prefix: '/static', # Prefix to add to all asset paths
|
32
|
-
pristine: ['/google-verify.html'], # Paths with no prefix or versioning (
|
65
|
+
pristine: ['/google-verify.html'], # Paths with no prefix or versioning (/favicon.ico,
|
66
|
+
# /mask-icon.svg, /humans.txt, and /robots.txt are
|
67
|
+
# included automatically)
|
33
68
|
minify: true, # Minify assets that can be minified
|
34
|
-
minified_pattern: /(\.|-)min\.\w+$/, # Files
|
35
|
-
internal_pattern: /^\/components\//, # Files
|
69
|
+
minified_pattern: /(\.|-)min\.\w+$/, # Files to skip minification on when minify: true
|
70
|
+
internal_pattern: /^\/components\//, # Files to disallow direct external access to (they can
|
71
|
+
# still be imported into other assets)
|
36
72
|
min_process_interval: 1, # Minimum time that must elapse between process calls
|
37
73
|
)
|
74
|
+
```
|
38
75
|
|
39
|
-
|
40
|
-
|
76
|
+
Note that assets paths across all load path directories must be globally unique (e.g. the existence of both
|
77
|
+
`app/assets/app.js` and `vendor/assets/app.js` will result in an error).
|
78
|
+
|
79
|
+
Darkroom will never update assets without explicitly being told to do so. The following call should be made
|
80
|
+
once when the app is started and additionally at the beginning of each web request in development to refresh
|
81
|
+
any modified assets:
|
82
|
+
|
83
|
+
```ruby
|
41
84
|
darkroom.process
|
85
|
+
```
|
86
|
+
|
87
|
+
Alternatively, assets can be dumped to disk when deploying to a production environment where assets will be
|
88
|
+
uploaded to and served from a CDN or proxy server:
|
42
89
|
|
43
|
-
|
44
|
-
# uploaded to and served from a CDN or proxy server.
|
90
|
+
```ruby
|
45
91
|
darkroom.dump('output/dir',
|
46
92
|
clear: true, # Delete contents of output/dir before dumping
|
47
93
|
include_pristine: true, # Include pristine assets (if preparing for CDN upload, files like
|
48
94
|
) # /favicon.ico or /robots.txt should be left out)
|
49
95
|
```
|
50
96
|
|
51
|
-
Note that assets paths across all load path directories must be globally unique (e.g. the existance of both
|
52
|
-
`app/assets/app.js` and `vendor/assets/app.js` will result in an error).
|
53
|
-
|
54
97
|
To work with assets:
|
55
98
|
|
56
99
|
```ruby
|
57
|
-
#
|
58
|
-
path = darkroom.asset_path('/js/app.js')
|
100
|
+
# A Darkroom instance has a few convenience helper methods.
|
101
|
+
path = darkroom.asset_path('/js/app.js') # => '/static/js/app-[fingerprint].js'
|
102
|
+
integrity = darkroom.asset_integrity('/js/app.js') # => 'sha384-[hash]'
|
59
103
|
|
60
104
|
# Retrieve the Asset object associated with a path.
|
61
105
|
asset = darkroom.asset(path)
|
62
106
|
|
63
|
-
#
|
64
|
-
assest.path
|
65
|
-
assest.
|
107
|
+
# Prefix (if set on the Darkroom instance) is included in the unversioned and versioned paths.
|
108
|
+
assest.path # => '/js/app.js'
|
109
|
+
assest.path_unversioned # => '/static/js/app.js'
|
110
|
+
assest.path_versioned # => '/static/js/app-[fingerprint].js'
|
66
111
|
|
67
|
-
asset.content_type
|
68
|
-
asset.content
|
112
|
+
asset.content_type # => 'text/javascript'
|
113
|
+
asset.content # Content of processed /js/app.js file
|
69
114
|
|
70
|
-
asset.headers # => {'Content-Type' => '
|
115
|
+
asset.headers # => {'Content-Type' => 'text/javascript',
|
71
116
|
# 'Cache-Control' => 'public, max-age=31536000'}
|
72
|
-
asset.headers(versioned: false) # => {'Content-Type' => '
|
73
|
-
# 'ETag' => '
|
117
|
+
asset.headers(versioned: false) # => {'Content-Type' => 'text/javascript',
|
118
|
+
# 'ETag' => '[fingerprint]'}
|
119
|
+
|
120
|
+
asset.integrity # => 'sha384-[hash]'
|
121
|
+
asset.integrity(:sha256) # => 'sha256-[hash]'
|
122
|
+
asset.integrity(:sha512) # => 'sha512-[hash]'
|
74
123
|
```
|
75
124
|
|
76
125
|
## Asset Bundling
|
@@ -129,6 +178,77 @@ Imports can even be cyclical. If `asset-a.css` imports `asset-b.css` and vice-ve
|
|
129
178
|
contain the content of both of those assets (though order will be different as an asset's own content always
|
130
179
|
comes after any imported assets' contents).
|
131
180
|
|
181
|
+
## Asset References
|
182
|
+
|
183
|
+
Asset paths and content can be inserted into an asset by referencing an asset's path and including a query
|
184
|
+
parameter.
|
185
|
+
|
186
|
+
| String | Result |
|
187
|
+
|----------------------------------|-----------------------------------|
|
188
|
+
| /logo.svg?asset-path | /prefix/logo-[fingerprint].svg |
|
189
|
+
| /logo.svg?asset-path=versioned | /prefix/logo-[fingerprint].svg |
|
190
|
+
| /logo.svg?asset-path=unversioned | /prefix/logo.svg |
|
191
|
+
| /logo.svg?asset-content | data:image/svg+xml;base64,[data] |
|
192
|
+
| /logo.svg?asset-content=base64 | data:image/svg+xml;base64,[data] |
|
193
|
+
| /logo.svg?asset-content=utf8 | data:image/svg+xml;utf8,\<svg>... |
|
194
|
+
|
195
|
+
Where these get recognized is specific to each asset type.
|
196
|
+
|
197
|
+
* **CSS** - Within `url(...)`, which may be unquoted or quoted with single or double quotes.
|
198
|
+
* **HTML** - Values of `href` and `src` attributes on `a`, `area`, `audio`, `base`, `embed`, `iframe`,
|
199
|
+
`img`, `input`, `link`, `script`, `source`, `track`, and `video` tags.
|
200
|
+
* **HTX** - Same behavior as HTML.
|
201
|
+
|
202
|
+
HTML assets additionally support the `?asset-content=displace` query parameter for use with `<link>`,
|
203
|
+
`<script>`, and `<img>` tags with CSS, JavaScript, and SVG asset references, respectively. The entire tag is
|
204
|
+
replaced appropriately.
|
205
|
+
|
206
|
+
```html
|
207
|
+
<!-- Source -->
|
208
|
+
<head>
|
209
|
+
<title>My App</title>
|
210
|
+
<link href='/app.css?asset-content=displace' type='text/css'>
|
211
|
+
<script src='/app.js?asset-content=displace'></script>
|
212
|
+
</head>
|
213
|
+
|
214
|
+
<body>
|
215
|
+
<img src='/logo.svg?asset-content-displace'>
|
216
|
+
</body>
|
217
|
+
|
218
|
+
<!-- Result -->
|
219
|
+
<head>
|
220
|
+
<title>My App</title>
|
221
|
+
<style>/* Content of /app.css */</style>
|
222
|
+
<script>/* Content of /app.js */</script>
|
223
|
+
</head>
|
224
|
+
|
225
|
+
<body>
|
226
|
+
<svg><!-- ... --></svg>
|
227
|
+
</body>
|
228
|
+
```
|
229
|
+
|
230
|
+
## Extending
|
231
|
+
|
232
|
+
Darkroom is extensible. Support for arbitrary file types can be added as follows (all named parameters are
|
233
|
+
optional):
|
234
|
+
|
235
|
+
```ruby
|
236
|
+
# Simple type with no special behavior.
|
237
|
+
Darkroom.register('.extension1', 'extension2', '...', 'content/type')
|
238
|
+
|
239
|
+
# Complex type with special behavior.
|
240
|
+
Darkroom.register('.extension1', 'extension2', '...',
|
241
|
+
content_type: 'content/type', # HTTP MIME type string
|
242
|
+
import_regex: /import (?<path>.*)/, # Regex for identifying imports for bundling
|
243
|
+
reference_regex: /ref=(?<path>.*)/, # Regex for identifying references to other assets
|
244
|
+
compile_lib: 'some-compile-lib', # Name of library required for compilation
|
245
|
+
compile: ->(path, content) { '...' }, # Lambda that returns compiled content
|
246
|
+
minify_lib: 'some-minify-lib', # Name of library required for minification
|
247
|
+
minify: ->(content) { '...' }, # Lambda that returns minified content
|
248
|
+
)
|
249
|
+
|
250
|
+
```
|
251
|
+
|
132
252
|
## Contributing
|
133
253
|
|
134
254
|
Bug reports and pull requests are welcome on GitHub at https://github.com/npickens/darkroom.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|