propshaft 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9bdd01776a4cf66e324271ccb59f41d8473b2863c75ace4d2d75f7295e2fad36
4
- data.tar.gz: c1e8d1e3a3bfaee8ee77bd53b760bc272df4d25f4fc4c494eb14e99bc125acd9
3
+ metadata.gz: 963ec537db8637fe43844f1b46e290c600c04b5f8b170c79583d4370dbcefbbf
4
+ data.tar.gz: c6f48c154b847a715317ba44d041287f82f009ee760b9b9d8e7a319c7f354ff2
5
5
  SHA512:
6
- metadata.gz: fddd9f7d38b01958b449f966143894f065d8a0c35a51dcd0b5234230daf3ac7897107289af615f6484d253f883d6c2c5351a6281b1fd0f9465efd3df0d92e6ad
7
- data.tar.gz: 26f5105052b942c55f41f6f049eb3e28c1200231a51edad58c41645b936f51fbe98721ae1823b2ec293243c369cadb657368be4afee35747450fb01d948cee93
6
+ metadata.gz: 5e08613dd985ef975477b9a1886ffa5def8aef4889c6d4cbfd585d716c711e5806265d52370c819642ebcade755062c08358809823bbb4bc4ecece4892dbe3b5
7
+ data.tar.gz: 16f7245a2a4c9aa69f066a566e9781a1cb1327141b6daf865fd6917f3b1d5eaec560f5ee1cf8600c484e43f60d3acea55fca4b8262dd8ec88ef714839a57ee61
data/README.md CHANGED
@@ -12,7 +12,7 @@ So that's what Propshaft doesn't do. Here's what it does provide:
12
12
 
13
13
  ## Installation
14
14
 
15
- With Rails 7+, you can start a new application with propshaft using `rails new myapp -a propshaft`. For existing applications, check the [upgrade guide](https://github.com/rails/propshaft/blob/main/UPGRADING.md) which contains step-by-step instructions.
15
+ With Rails 8, Propshaft is the default asset pipeline for new applications. With Rails 7, you can start a new application with propshaft using `rails new myapp -a propshaft`. For existing applications, check the [upgrade guide](https://github.com/rails/propshaft/blob/main/UPGRADING.md) which contains step-by-step instructions.
16
16
 
17
17
  ## Usage
18
18
 
@@ -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
 
@@ -47,11 +70,6 @@ On the other hand, if you're already bundling JavaScript and CSS through a Node-
47
70
  But for greenfield apps using the default import-map approach, Propshaft can also work well, if you're able to deal with vanilla CSS.
48
71
 
49
72
 
50
- ## Will Propshaft replace Sprockets as the Rails default?
51
-
52
- Most likely, but Sprockets needs to be supported as well for a long time to come. Plenty of apps and gems were built on Sprocket features, and they won't be migrating soon. Still working out the compatibility story. This is very much beta software at the moment.
53
-
54
-
55
73
  ## License
56
74
 
57
75
  Propshaft is released under the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -6,8 +6,8 @@ class Propshaft::Asset
6
6
 
7
7
  class << self
8
8
  def extract_path_and_digest(digested_path)
9
- digest = digested_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)([^.]|.map)+\z/, 1]
10
- path = digest ? digested_path.sub("-#{digest}", "") : digested_path
9
+ digest = digested_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)([^.]|.map)+\z/, 1]
10
+ path = digest ? digested_path.sub("-#{digest}", "") : digested_path
11
11
 
12
12
  [path, digest]
13
13
  end
@@ -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
@@ -13,7 +13,8 @@ module Propshaft
13
13
  config.assets.compilers = [
14
14
  [ "text/css", Propshaft::Compiler::CssAssetUrls ],
15
15
  [ "text/css", Propshaft::Compiler::SourceMappingUrls ],
16
- [ "text/javascript", Propshaft::Compiler::SourceMappingUrls ]
16
+ [ "text/javascript", Propshaft::Compiler::JsAssetUrls ],
17
+ [ "text/javascript", Propshaft::Compiler::SourceMappingUrls ],
17
18
  ]
18
19
  config.assets.sweep_cache = Rails.env.development?
19
20
  config.assets.server = Rails.env.development? || Rails.env.test?
@@ -31,6 +32,9 @@ module Propshaft
31
32
  end
32
33
 
33
34
  config.after_initialize do |app|
35
+ # Prioritize assets from within the application over assets of the same path from engines/gems.
36
+ config.assets.paths.sort_by!.with_index { |path, i| [path.to_s.start_with?(Rails.root.to_s) ? 0 : 1, i] }
37
+
34
38
  config.assets.relative_url_root ||= app.config.relative_url_root
35
39
  config.assets.output_path ||=
36
40
  Pathname.new(File.join(app.config.paths["public"].first, app.config.assets.prefix))
@@ -1,3 +1,3 @@
1
1
  module Propshaft
2
- VERSION = "1.0.0"
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.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-09 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