propshaft 0.1.7 → 0.2.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: f01db7988c70a623917c5239f4b76cef03e3364b86f3bb1b9850691d1045d3e1
4
- data.tar.gz: 066dab4410e31551036e8f539cb351b8c4a6d7c53ae9bd02343b228be16fc85f
3
+ metadata.gz: 32316706959e2a09f2ac10010fb76ddc25ddfab8ba6a8b06cfb71dc0e75c0deb
4
+ data.tar.gz: 1b6c3074c0a8f4c0d21287d6c27689bb443e7a58c18a690420f932709a244c44
5
5
  SHA512:
6
- metadata.gz: 7b09d882ebf32f6afda261a3dbf32ab322e25b1756b802b70e4b6b16f73f3d91404e8b51be6e5c54a1335332744305d3d149712136cbbdabc67b258229178b9f
7
- data.tar.gz: 65a13d00417ff13f2678660038ebb7f33074e56b0277b7c51e9df170f9e393ec17cd5ff5ddc888b4e24c469df33ba4f5d8acb19ecf5a2b7626d41c22dac0d781
6
+ metadata.gz: 8a8cfb18503c100507681ee4da2e76d8493898de8f79cfab01b7b640b92affed80ab3a135f7eb8514a53d2e6d11943e3500c6681ac879d14367fcfa5d5f8432f
7
+ data.tar.gz: c5e47f07a0d52306a11bb132f0ad7139b208eeb5cd9396d6274423b4de8b2deed013895107027ced61f4887d5aaeee413ae9e41065a74c022a4c4529572d4048
@@ -32,6 +32,10 @@ class Propshaft::Asset
32
32
  end
33
33
  end
34
34
 
35
+ def fresh?(digest)
36
+ self.digest == digest || already_digested?
37
+ end
38
+
35
39
  def ==(other_asset)
36
40
  logical_path.hash == other_asset.logical_path.hash
37
41
  end
@@ -1,13 +1,30 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Propshaft::Compilers::CssAssetUrls
2
4
  attr_reader :assembly
3
5
 
6
+ ASSET_URL_PATTERN = /url\(\s*["']?(?!(?:\#|data|http))([^"'\s)]+)\s*["']?\)/
7
+
4
8
  def initialize(assembly)
5
9
  @assembly = assembly
6
10
  end
7
11
 
8
- def compile(input)
9
- input.gsub(/asset-path\(["']([^"')]+)["']\)/) do |match|
10
- %[url("#{assembly.config.prefix}/#{assembly.load_path.find($1).digested_path}")]
11
- end
12
+ def compile(logical_path, input)
13
+ input.gsub(ASSET_URL_PATTERN) { asset_url resolve_path(logical_path.dirname, $1) }
12
14
  end
15
+
16
+ private
17
+ def resolve_path(directory, filename)
18
+ if filename.start_with?("../")
19
+ Pathname.new(directory + filename).relative_path_from("").to_s
20
+ elsif filename.start_with?("/")
21
+ filename.delete_prefix("/").to_s
22
+ else
23
+ (directory + filename.delete_prefix("./")).to_s
24
+ end
25
+ end
26
+
27
+ def asset_url(resolved_path)
28
+ %[url("#{assembly.config.prefix}/#{assembly.load_path.find(resolved_path).digested_path}")]
29
+ end
13
30
  end
@@ -23,7 +23,7 @@ class Propshaft::Compilers
23
23
  if relevant_registrations = registrations[asset.content_type.to_s]
24
24
  asset.content.dup.tap do |input|
25
25
  relevant_registrations.each do |compiler|
26
- input.replace compiler.new(assembly).compile(input)
26
+ input.replace compiler.new(assembly).compile(asset.logical_path, input)
27
27
  end
28
28
  end
29
29
  else
@@ -16,6 +16,10 @@ class Propshaft::Processor
16
16
  compress_assets
17
17
  end
18
18
 
19
+ def clean
20
+ FileUtils.rm_r(output_path) if File.exist?(output_path)
21
+ end
22
+
19
23
  private
20
24
  def ensure_output_path_exists
21
25
  FileUtils.mkdir_p output_path
@@ -46,6 +46,11 @@ module Propshaft
46
46
  task precompile: :environment do
47
47
  Rails.application.assets.processor.process
48
48
  end
49
+
50
+ desc "Remove config.assets.output_path"
51
+ task clean: :environment do
52
+ Rails.application.assets.processor.clean
53
+ end
49
54
  end
50
55
  end
51
56
 
@@ -8,16 +8,17 @@ class Propshaft::Server
8
8
  def call(env)
9
9
  path, digest = extract_path_and_digest(env)
10
10
 
11
- if (asset = @assembly.load_path.find(path)) && asset.digest == digest
11
+ if (asset = @assembly.load_path.find(path)) && asset.fresh?(digest)
12
12
  compiled_content = @assembly.compilers.compile(asset)
13
13
 
14
14
  [
15
15
  200,
16
16
  {
17
- "Content-Length" => compiled_content.length.to_s,
18
- "Content-Type" => asset.content_type,
19
- "ETag" => asset.digest,
20
- "Cache-Control" => "public, max-age=31536000, immutable"
17
+ "Content-Length" => compiled_content.length.to_s,
18
+ "Content-Type" => asset.content_type.to_s,
19
+ "Accept-Encoding" => "Vary",
20
+ "ETag" => asset.digest,
21
+ "Cache-Control" => "public, max-age=31536000, immutable"
21
22
  },
22
23
  [ compiled_content ]
23
24
  ]
@@ -29,9 +30,9 @@ class Propshaft::Server
29
30
  private
30
31
  def extract_path_and_digest(env)
31
32
  full_path = Rack::Utils.unescape(env["PATH_INFO"].to_s.sub(/^\//, ""))
32
- digest = full_path[/-([0-9a-f]{7,128})\.[^.]+\z/, 1]
33
+ digest = full_path[/-([0-9a-f]{7,128})\.(?!digested)[^.]+\z/, 1]
33
34
  path = digest ? full_path.sub("-#{digest}", "") : full_path
34
-
35
+
35
36
  [ path, digest ]
36
37
  end
37
38
  end
@@ -1,3 +1,3 @@
1
1
  module Propshaft
2
- VERSION = "0.1.7"
2
+ VERSION = "0.2.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: 0.1.7
4
+ version: 0.2.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: 2021-10-09 00:00:00.000000000 Z
11
+ date: 2021-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails