propshaft 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -0
- data/lib/propshaft/assembly.rb +1 -0
- data/lib/propshaft/compiler/js_asset_urls.rb +45 -0
- data/lib/propshaft/railtie.rb +3 -3
- data/lib/propshaft/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 963ec537db8637fe43844f1b46e290c600c04b5f8b170c79583d4370dbcefbbf
|
4
|
+
data.tar.gz: c6f48c154b847a715317ba44d041287f82f009ee760b9b9d8e7a319c7f354ff2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e08613dd985ef975477b9a1886ffa5def8aef4889c6d4cbfd585d716c711e5806265d52370c819642ebcade755062c08358809823bbb4bc4ecece4892dbe3b5
|
7
|
+
data.tar.gz: 16f7245a2a4c9aa69f066a566e9781a1cb1327141b6daf865fd6917f3b1d5eaec560f5ee1cf8600c484e43f60d3acea55fca4b8262dd8ec88ef714839a57ee61
|
data/README.md
CHANGED
@@ -22,6 +22,29 @@ You can however exempt directories that have been added through the `config.asse
|
|
22
22
|
|
23
23
|
These assets can be referenced through their logical path using the normal helpers like `asset_path`, `image_tag`, `javascript_include_tag`, and all the other asset helper tags. These logical references are automatically converted into digest-aware paths in production when `assets:precompile` has been run (through a JSON mapping file found in `public/assets/.manifest.json`).
|
24
24
|
|
25
|
+
## Referencing digested assets in CSS and JavaScript
|
26
|
+
|
27
|
+
Propshaft will automatically convert asset references in CSS to use the digested file names. So `background: url("/bg/pattern.svg")` is converted to `background: url("/assets/bg/pattern-2169cbef.svg")` before the stylesheet is served.
|
28
|
+
|
29
|
+
For JavaScript, you'll have to manually trigger this transformation by using the `RAILS_ASSET_URL` pseudo-method. It's used like this:
|
30
|
+
|
31
|
+
```javascript
|
32
|
+
export default class extends Controller {
|
33
|
+
init() {
|
34
|
+
this.img = RAILS_ASSET_URL("/icons/trash.svg")
|
35
|
+
}
|
36
|
+
}
|
37
|
+
```
|
38
|
+
|
39
|
+
That'll turn into:
|
40
|
+
|
41
|
+
```javascript
|
42
|
+
export default class extends Controller {
|
43
|
+
init() {
|
44
|
+
this.img = "/assets/icons/trash-54g9cbef.svg"
|
45
|
+
}
|
46
|
+
}
|
47
|
+
```
|
25
48
|
|
26
49
|
## Bypassing the digest step
|
27
50
|
|
data/lib/propshaft/assembly.rb
CHANGED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "propshaft/compiler"
|
4
|
+
|
5
|
+
class Propshaft::Compiler::JsAssetUrls < Propshaft::Compiler
|
6
|
+
ASSET_URL_PATTERN = %r{RAILS_ASSET_URL\(\s*["']?(?!(?:\#|%23|data|http|//))([^"'\s?#)]+)([#?][^"')]+)?\s*["']?\)}
|
7
|
+
|
8
|
+
def compile(asset, input)
|
9
|
+
input.gsub(ASSET_URL_PATTERN) { asset_url(resolve_path(asset.logical_path.dirname, $1), asset.logical_path, $2, $1) }
|
10
|
+
end
|
11
|
+
|
12
|
+
def referenced_by(asset, references: Set.new)
|
13
|
+
asset.content.scan(ASSET_URL_PATTERN).each do |referenced_asset_url, _|
|
14
|
+
referenced_asset = load_path.find(resolve_path(asset.logical_path.dirname, referenced_asset_url))
|
15
|
+
|
16
|
+
if referenced_asset && references.exclude?(referenced_asset)
|
17
|
+
references << referenced_asset
|
18
|
+
references.merge referenced_by(referenced_asset, references: references)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
references
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
def resolve_path(directory, filename)
|
27
|
+
if filename.start_with?("../")
|
28
|
+
Pathname.new(directory + filename).relative_path_from("").to_s
|
29
|
+
elsif filename.start_with?("/")
|
30
|
+
filename.delete_prefix("/").to_s
|
31
|
+
else
|
32
|
+
(directory + filename.delete_prefix("./")).to_s
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def asset_url(resolved_path, logical_path, fingerprint, pattern)
|
37
|
+
asset = load_path.find(resolved_path)
|
38
|
+
if asset
|
39
|
+
%["#{url_prefix}/#{asset.digested_path}#{fingerprint}"]
|
40
|
+
else
|
41
|
+
Propshaft.logger.warn("Unable to resolve '#{pattern}' for missing asset '#{resolved_path}' in #{logical_path}")
|
42
|
+
%["#{pattern}"]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/propshaft/railtie.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
require "rails"
|
2
2
|
require "active_support/ordered_options"
|
3
|
-
require "propshaft"
|
4
3
|
require "propshaft/quiet_assets"
|
5
4
|
|
6
5
|
module Propshaft
|
@@ -14,7 +13,8 @@ module Propshaft
|
|
14
13
|
config.assets.compilers = [
|
15
14
|
[ "text/css", Propshaft::Compiler::CssAssetUrls ],
|
16
15
|
[ "text/css", Propshaft::Compiler::SourceMappingUrls ],
|
17
|
-
[ "text/javascript", Propshaft::Compiler::
|
16
|
+
[ "text/javascript", Propshaft::Compiler::JsAssetUrls ],
|
17
|
+
[ "text/javascript", Propshaft::Compiler::SourceMappingUrls ],
|
18
18
|
]
|
19
19
|
config.assets.sweep_cache = Rails.env.development?
|
20
20
|
config.assets.server = Rails.env.development? || Rails.env.test?
|
@@ -33,7 +33,7 @@ module Propshaft
|
|
33
33
|
|
34
34
|
config.after_initialize do |app|
|
35
35
|
# Prioritize assets from within the application over assets of the same path from engines/gems.
|
36
|
-
config.assets.paths.sort_by
|
36
|
+
config.assets.paths.sort_by!.with_index { |path, i| [path.to_s.start_with?(Rails.root.to_s) ? 0 : 1, i] }
|
37
37
|
|
38
38
|
config.assets.relative_url_root ||= app.config.relative_url_root
|
39
39
|
config.assets.output_path ||=
|
data/lib/propshaft/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: propshaft
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heinemeier Hansson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-09-
|
11
|
+
date: 2024-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- lib/propshaft/asset.rb
|
81
81
|
- lib/propshaft/compiler.rb
|
82
82
|
- lib/propshaft/compiler/css_asset_urls.rb
|
83
|
+
- lib/propshaft/compiler/js_asset_urls.rb
|
83
84
|
- lib/propshaft/compiler/source_mapping_urls.rb
|
84
85
|
- lib/propshaft/compilers.rb
|
85
86
|
- lib/propshaft/errors.rb
|