propshaft 1.0.1 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 14ffc5b071d45f08f18e5f52fa2668f4630debf23d501c08e000d309c3a9b8fd
4
- data.tar.gz: c210e3cd12d58e3df3c2a20c2b42d182ee66e13ae3d6653fd05272b3ad513e37
3
+ metadata.gz: 963ec537db8637fe43844f1b46e290c600c04b5f8b170c79583d4370dbcefbbf
4
+ data.tar.gz: c6f48c154b847a715317ba44d041287f82f009ee760b9b9d8e7a319c7f354ff2
5
5
  SHA512:
6
- metadata.gz: bea78212ba31154621e6878b7e721abbc787e0594da6dadf6d6e587b213c9f587d6d554e74f5b79d361944ae43514c82efa30b49978d4faa776e21774ceb4fd6
7
- data.tar.gz: 46de0996122fe99868d6a74a1d4f3c37c8897c6b760e0a7de9db36cc8794995ea6c753d792fd21ddde22d2d9fa1fd599aef055d94a42da49b36e1badeb5f5ca9
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
 
@@ -5,6 +5,7 @@ require "propshaft/server"
5
5
  require "propshaft/processor"
6
6
  require "propshaft/compilers"
7
7
  require "propshaft/compiler/css_asset_urls"
8
+ require "propshaft/compiler/js_asset_urls"
8
9
  require "propshaft/compiler/source_mapping_urls"
9
10
 
10
11
  class Propshaft::Assembly
@@ -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
@@ -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::SourceMappingUrls ]
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! { |path| path.to_s.start_with?(Rails.root.to_s) ? 0 : 1 }
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 ||=
@@ -1,3 +1,3 @@
1
1
  module Propshaft
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
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.1
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-29 00:00:00.000000000 Z
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