propshaft 1.2.0 → 1.3.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: 8ea8c536e8ee4cd0229dd706a6689d8e52d13a63533fb1f2d5e19835f3a853f2
4
- data.tar.gz: fdd5faee6f39306a3bd12f0d3b008b31554b2576637f14497640dd886d38d865
3
+ metadata.gz: 88560fd134482bd4d44518bde9a6813be01d538f16409be833e36664a3c998bc
4
+ data.tar.gz: df495a737f633966e4aa8f7837400353ec2bc9166142e38a94b7d50350c2e092
5
5
  SHA512:
6
- metadata.gz: 684f7db0156395a7376f7f1788576a7f5bbfaef1ccbe44af0fb2bf2ce315c6acdc45e2f5e368df3d580e6e54c741a80d50bd7cb0db6976909662b40a65184f0c
7
- data.tar.gz: ad626da00dd0f678abe3d5ea4ce26975c38e02906fdc14de6aa244cdb0902382dd25caf495e9d32e0da3d89ad9b260dce2bdf992a1f925a65fdaeea0494bf355
6
+ metadata.gz: f27ba3bd99cbbaeb643f466c8d37051a988809c1415ccdec2eff5577506eeeed8f4c772167e8afe461b349bb4b4e75aa22bedde7c256d023796929296d1c00e1
7
+ data.tar.gz: 571d8052e3b16c7d779c55433d27c268f0ceb387213ca4b0220a200dc30885e15accc855075e4fbcf95ea71c887ff071bfa25caaa8330f0e9a5c87c33c10076f
@@ -34,8 +34,11 @@ class Propshaft::Assembly
34
34
  end
35
35
  end
36
36
 
37
- def server
38
- Propshaft::Server.new(self)
37
+ def prefix
38
+ @prefix ||= begin
39
+ prefix = config.prefix || "/"
40
+ prefix.end_with?("/") ? prefix : "#{prefix}/"
41
+ end
39
42
  end
40
43
 
41
44
  def processor
@@ -70,7 +70,9 @@ module Propshaft
70
70
  #
71
71
  # stylesheet_link_tag :all # All stylesheets in load path
72
72
  # stylesheet_link_tag :app # Only app/assets stylesheets
73
- def stylesheet_link_tag(*sources, **options)
73
+ def stylesheet_link_tag(*sources)
74
+ options = sources.extract_options!
75
+
74
76
  case sources.first
75
77
  when :all
76
78
  sources = all_stylesheets_paths
@@ -95,7 +97,9 @@ module Propshaft
95
97
  # javascript_include_tag "application", integrity: true
96
98
  # # => <script src="/assets/application-abc123.js"
97
99
  # # integrity="sha256-xyz789..."></script>
98
- def javascript_include_tag(*sources, **options)
100
+ def javascript_include_tag(*sources)
101
+ options = sources.extract_options!
102
+
99
103
  _build_asset_tags(sources, options, :javascript) { |source, opts| super(source, opts) }
100
104
  end
101
105
 
@@ -134,6 +134,19 @@ module Propshaft
134
134
  @entries[logical_path]
135
135
  end
136
136
 
137
+ # Removes a manifest entry by its logical path.
138
+ #
139
+ # ==== Parameters
140
+ #
141
+ # * +logical_path+ - The logical path of the asset to remove
142
+ #
143
+ # ==== Returns
144
+ #
145
+ # The removed manifest entry, or +nil+ if not found.
146
+ def delete(logical_path)
147
+ @entries.delete(logical_path)
148
+ end
149
+
137
150
  # Converts the manifest to JSON format.
138
151
  #
139
152
  # The JSON representation maps logical paths to hash representations of
@@ -42,14 +42,6 @@ module Propshaft
42
42
  Pathname.new(File.join(app.config.paths["public"].first, app.config.assets.prefix))
43
43
  config.assets.manifest_path ||= config.assets.output_path.join(".manifest.json")
44
44
 
45
- app.assets = Propshaft::Assembly.new(app.config.assets)
46
-
47
- if config.assets.server
48
- app.routes.prepend do
49
- mount app.assets.server, at: app.assets.config.prefix
50
- end
51
- end
52
-
53
45
  ActiveSupport.on_load(:action_view) do
54
46
  include Propshaft::Helper
55
47
  end
@@ -71,6 +63,13 @@ module Propshaft
71
63
  end
72
64
  end
73
65
 
66
+ initializer "propshaft.assets_middleware", group: :all do |app|
67
+ app.assets = Propshaft::Assembly.new(app.config.assets)
68
+ if config.assets.server
69
+ app.middleware.insert_after ::ActionDispatch::Static, Propshaft::Server, app.assets
70
+ end
71
+ end
72
+
74
73
  rake_tasks do
75
74
  load "propshaft/railties/assets.rake"
76
75
  end
@@ -2,30 +2,39 @@ require "rack/utils"
2
2
  require "rack/version"
3
3
 
4
4
  class Propshaft::Server
5
- def initialize(assembly)
5
+ def initialize(app, assembly)
6
+ @app = app
6
7
  @assembly = assembly
7
8
  end
8
9
 
9
10
  def call(env)
10
11
  execute_cache_sweeper_if_updated
11
- path, digest = extract_path_and_digest(env)
12
-
13
- if (asset = @assembly.load_path.find(path)) && asset.fresh?(digest)
14
- compiled_content = asset.compiled_content
15
-
16
- [
17
- 200,
18
- {
19
- Rack::CONTENT_LENGTH => compiled_content.length.to_s,
20
- Rack::CONTENT_TYPE => asset.content_type.to_s,
21
- VARY => "Accept-Encoding",
22
- Rack::ETAG => "\"#{asset.digest}\"",
23
- Rack::CACHE_CONTROL => "public, max-age=31536000, immutable"
24
- },
25
- [ compiled_content ]
26
- ]
12
+
13
+ path = env["PATH_INFO"]
14
+ method = env["REQUEST_METHOD"]
15
+
16
+ if (method == "GET" || method == "HEAD") && path.start_with?(@assembly.prefix)
17
+ path, digest = extract_path_and_digest(path)
18
+
19
+ if (asset = @assembly.load_path.find(path)) && asset.fresh?(digest)
20
+ compiled_content = asset.compiled_content
21
+
22
+ [
23
+ 200,
24
+ {
25
+ Rack::CONTENT_LENGTH => compiled_content.length.to_s,
26
+ Rack::CONTENT_TYPE => asset.content_type.to_s,
27
+ VARY => "Accept-Encoding",
28
+ Rack::ETAG => "\"#{asset.digest}\"",
29
+ Rack::CACHE_CONTROL => "public, max-age=31536000, immutable"
30
+ },
31
+ method == "HEAD" ? [] : [ compiled_content ]
32
+ ]
33
+ else
34
+ [ 404, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "9" }, [ "Not found" ] ]
35
+ end
27
36
  else
28
- [ 404, { Rack::CONTENT_TYPE => "text/plain", Rack::CONTENT_LENGTH => "9" }, [ "Not found" ] ]
37
+ @app.call(env)
29
38
  end
30
39
  end
31
40
 
@@ -34,10 +43,11 @@ class Propshaft::Server
34
43
  end
35
44
 
36
45
  private
37
- def extract_path_and_digest(env)
38
- full_path = Rack::Utils.unescape(env["PATH_INFO"].to_s.sub(/^\//, ""))
46
+ def extract_path_and_digest(path)
47
+ path = path.delete_prefix(@assembly.prefix)
48
+ path = Rack::Utils.unescape(path)
39
49
 
40
- Propshaft::Asset.extract_path_and_digest(full_path)
50
+ Propshaft::Asset.extract_path_and_digest(path)
41
51
  end
42
52
 
43
53
  if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3")
@@ -47,6 +57,8 @@ class Propshaft::Server
47
57
  end
48
58
 
49
59
  def execute_cache_sweeper_if_updated
50
- @assembly.load_path.cache_sweeper.execute_if_updated
60
+ if @assembly.config.sweep_cache
61
+ @assembly.load_path.cache_sweeper.execute_if_updated
62
+ end
51
63
  end
52
64
  end
@@ -1,3 +1,3 @@
1
1
  module Propshaft
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: propshaft
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson