Skip to content

Commit

Permalink
Merge branch 'main' into custom_headers_configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
fauresebast authored Jan 10, 2024
2 parents 5336800 + 40ce740 commit 0103f67
Show file tree
Hide file tree
Showing 26 changed files with 85 additions and 75 deletions.
19 changes: 11 additions & 8 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Ruby",
"build": {
"dockerfile": "Dockerfile",
"args": {
"args": {
// Update 'VARIANT' to pick a Ruby version: 3, 3.0, 2, 2.7, 2.6
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local on arm64/Apple Silicon.
Expand All @@ -14,14 +14,17 @@
}
},

// Set *default* container specific settings.json values on container create.
"settings": {},
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"Shopify.ruby-lsp"
]
}
},

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"rebornix.Ruby"
],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
continue-on-error: ${{ matrix.continue-on-error }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Install Ruby
uses: ruby/setup-ruby@v1
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
propshaft (0.7.0)
propshaft (0.8.0)
actionpack (>= 7.0.0)
activesupport (>= 7.0.0)
rack
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ But for greenfield apps using the default import-map approach, Propshaft can als

## Will Propshaft replace Sprockets as the Rails default?

Most likely, but Sprockets need 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.
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.


## License
Expand Down
4 changes: 2 additions & 2 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ Rails.application.assets.load_path.find('logo.svg').content

As Rails escapes html tags in views by default, in order to output a rendered svg you will need to specify rails not to escape the string using [html_safe](https://api.rubyonrails.org/classes/String.html#method-i-html_safe) or [raw](https://api.rubyonrails.org/classes/ActionView/Helpers/OutputSafetyHelper.html#method-i-raw).
```ruby
Rails.application.assets.load_path.find('logo.svg').html_safe
raw Rails.application.assets.load_path.find('logo.svg')
Rails.application.assets.load_path.find('logo.svg').content.html_safe
raw Rails.application.assets.load_path.find('logo.svg').content
```

**Precompilation in development**
Expand Down
4 changes: 2 additions & 2 deletions lib/propshaft/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def length
end

def digest
@digest ||= Digest::SHA1.hexdigest("#{content}#{version}")
@digest ||= Digest::SHA1.hexdigest("#{content}#{version}").first(8)
end

def digested_path
Expand All @@ -42,6 +42,6 @@ def ==(other_asset)

private
def already_digested?
logical_path.to_s =~ /-([0-9a-zA-Z]{7,128})\.digested/
logical_path.to_s =~ /-([0-9a-zA-Z_-]{7,128})\.digested/
end
end
6 changes: 3 additions & 3 deletions lib/propshaft/compiler/source_mapping_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Propshaft::Compiler::SourceMappingUrls < Propshaft::Compiler
SOURCE_MAPPING_PATTERN = %r{(//|/\*)# sourceMappingURL=(.+\.map)(\s*?\*\/)?\s*?\Z}

def compile(logical_path, input)
input.gsub(SOURCE_MAPPING_PATTERN) { source_mapping_url(asset_path($2, logical_path), $1, $3) }
input.gsub(SOURCE_MAPPING_PATTERN) { source_mapping_url(logical_path, asset_path($2, logical_path), $1, $3) }
end

private
Expand All @@ -18,11 +18,11 @@ def asset_path(source_mapping_url, logical_path)
end
end

def source_mapping_url(resolved_path, comment_start, comment_end)
def source_mapping_url(logical_path, resolved_path, comment_start, comment_end)
if asset = assembly.load_path.find(resolved_path)
"#{comment_start}# sourceMappingURL=#{url_prefix}/#{asset.digested_path}#{comment_end}"
else
Propshaft.logger.warn "Removed sourceMappingURL comment for missing asset '#{resolved_path}' from #{resolved_path}"
Propshaft.logger.warn "Removed sourceMappingURL comment for missing asset '#{resolved_path}' from #{logical_path}"
"#{comment_start}#{comment_end}"
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/propshaft/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def clobber
FileUtils.rm_r(output_path) if File.exist?(output_path)
end

def clean
Propshaft::OutputPath.new(output_path, load_path.manifest).clean(2, 1.hour)
def clean(count)
Propshaft::OutputPath.new(output_path, load_path.manifest).clean(count, 1.hour)
end

private
Expand Down
5 changes: 3 additions & 2 deletions lib/propshaft/railties/assets.rake
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ namespace :assets do
end

desc "Removes old files in config.assets.output_path"
task clean: :environment do
Rails.application.assets.processor.clean
task :clean, [:count] => [:environment] do |_, args|
count = args.fetch(:count, 2)
Rails.application.assets.processor.clean(count.to_i)
end

desc "Print all the assets available in config.assets.paths"
Expand Down
1 change: 1 addition & 0 deletions lib/propshaft/server.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "rack/utils"
require "rack/version"

class Propshaft::Server
def initialize(assembly)
Expand Down
2 changes: 1 addition & 1 deletion lib/propshaft/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Propshaft
VERSION = "0.7.0"
VERSION = "0.8.0"
end
2 changes: 1 addition & 1 deletion test/fixtures/output/.manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{ "one.txt": "one-f2e1ec14d6856e1958083094170ca6119c529a73.txt" }
{ "one.txt": "one-f2e1ec14.txt" }
File renamed without changes.
20 changes: 10 additions & 10 deletions test/propshaft/asset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ class Propshaft::AssetTest < ActiveSupport::TestCase
end

test "digest" do
assert_equal "f2e1ec14d6856e1958083094170ca6119c529a73", find_asset("one.txt").digest
assert_equal "f2e1ec14", find_asset("one.txt").digest
end

test "fresh" do
assert find_asset("one.txt").fresh?("f2e1ec14d6856e1958083094170ca6119c529a73")
assert_not find_asset("one.txt").fresh?("e206c34fe404c8e2f25d60dd8303f61c02b8d381")
assert find_asset("one.txt").fresh?("f2e1ec14")
assert_not find_asset("one.txt").fresh?("e206c34f")

assert find_asset("file-already-abcdefVWXYZ0123456789.digested.css").fresh?(nil)
assert find_asset("file-already-abcdefVWXYZ0123456789_-.digested.css").fresh?(nil)
end

test "digested path" do
assert_equal "one-f2e1ec14d6856e1958083094170ca6119c529a73.txt",
assert_equal "one-f2e1ec14.txt",
find_asset("one.txt").digested_path.to_s

assert_equal "file-already-abcdefVWXYZ0123456789.digested.css",
find_asset("file-already-abcdefVWXYZ0123456789.digested.css").digested_path.to_s
assert_equal "file-already-abcdefVWXYZ0123456789_-.digested.css",
find_asset("file-already-abcdefVWXYZ0123456789_-.digested.css").digested_path.to_s

assert_equal "file-already-abcdefVWXYZ0123456789.digested.debug.css",
find_asset("file-already-abcdefVWXYZ0123456789.digested.debug.css").digested_path.to_s
assert_equal "file-already-abcdefVWXYZ0123456789_-.digested.debug.css",
find_asset("file-already-abcdefVWXYZ0123456789_-.digested.debug.css").digested_path.to_s

assert_equal "file-not.digested-e206c34fe404c8e2f25d60dd8303f61c02b8d381.css",
assert_equal "file-not.digested-e206c34f.css",
find_asset("file-not.digested.css").digested_path.to_s
end

Expand Down
32 changes: 16 additions & 16 deletions test/propshaft/compiler/css_asset_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,62 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase

test "basic" do
compiled = compile_asset_with_content(%({ background: url(file.jpg); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "blank spaces around name" do
compiled = compile_asset_with_content(%({ background: url( file.jpg ); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "quotes around name" do
compiled = compile_asset_with_content(%({ background: url("file.jpg"); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "single quotes around name" do
compiled = compile_asset_with_content(%({ background: url('file.jpg'); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "root directory" do
compiled = compile_asset_with_content(%({ background: url('/file.jpg'); }))
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "same directory" do
compiled = compile_asset_with_content(%({ background: url('./file.jpg'); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "subdirectory" do
compiled = compile_asset_with_content(%({ background: url('./images/file.jpg'); }))
assert_match(/{ background: url\("\/assets\/foobar\/source\/images\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/source\/images\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "parent directory" do
compiled = compile_asset_with_content(%({ background: url('../file.jpg'); }))
assert_match(/{ background: url\("\/assets\/foobar\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "grandparent directory" do
compiled = compile_asset_with_content(%({ background: url('../../file.jpg'); }))
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "sibling directory" do
compiled = compile_asset_with_content(%({ background: url('../sibling/file.jpg'); }))
assert_match(/{ background: url\("\/assets\/foobar\/sibling\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/foobar\/sibling\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

test "mixed" do
compiled = compile_asset_with_content(%({ mask-image: image(url(file.jpg), skyblue, linear-gradient(rgba(0, 0, 0, 1.0), transparent)); }))
assert_match(/{ mask-image: image\(url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\), skyblue, linear-gradient\(rgba\(0, 0, 0, 1.0\), transparent\)\); }/, compiled)
assert_match(/{ mask-image: image\(url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\), skyblue, linear-gradient\(rgba\(0, 0, 0, 1.0\), transparent\)\); }/, compiled)
end

test "multiple" do
compiled = compile_asset_with_content(%({ content: url(file.svg) url(file.svg); }))
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg"\) url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg"\); }/, compiled)
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.svg"\) url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.svg"\); }/, compiled)
end

test "url" do
Expand All @@ -95,12 +95,12 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase

test "fingerprint" do
compiled = compile_asset_with_content(%({ background: url('/file.jpg?30af91bf14e37666a085fb8a161ff36d'); }))
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{40}.jpg\?30af91bf14e37666a085fb8a161ff36d"\); }/, compiled)
assert_match(/{ background: url\("\/assets\/file-[a-z0-9]{8}.jpg\?30af91bf14e37666a085fb8a161ff36d"\); }/, compiled)
end

test "svg anchor" do
compiled = compile_asset_with_content(%({ content: url(file.svg#rails); }))
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg#rails"\); }/, compiled)
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.svg#rails"\); }/, compiled)
end

test "svg mask encoded anchor" do
Expand All @@ -110,7 +110,7 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase

test "non greedy anchors" do
compiled = compile_asset_with_content(%({ content: url(file.svg#demo) url(file.svg#demo); }))
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg#demo"\) url\("\/assets\/foobar\/source\/file-[a-z0-9]{40}.svg#demo"\); }/, compiled)
assert_match(/{ content: url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.svg#demo"\) url\("\/assets\/foobar\/source\/file-[a-z0-9]{8}.svg#demo"\); }/, compiled)
end

test "missing asset" do
Expand All @@ -122,7 +122,7 @@ class Propshaft::Compiler::CssAssetUrlsTest < ActiveSupport::TestCase
@options.relative_url_root = "/url-root"

compiled = compile_asset_with_content(%({ background: url(file.jpg); }))
assert_match(/{ background: url\("\/url-root\/assets\/foobar\/source\/file-[a-z0-9]{40}.jpg"\); }/, compiled)
assert_match(/{ background: url\("\/url-root\/assets\/foobar\/source\/file-[a-z0-9]{8}.jpg"\); }/, compiled)
end

private
Expand Down
12 changes: 6 additions & 6 deletions test/propshaft/compiler/source_mapping_urls_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase
end

test "matching source map" do
assert_match %r{//# sourceMappingURL=/assets/source.js-[a-z0-9]{40}\.map},
assert_match %r{//# sourceMappingURL=/assets/source.js-[a-z0-9]{8}\.map},
compile_asset(find_asset("source.js", fixture_path: "mapped"))
assert_match %r{/\*# sourceMappingURL=/assets/source.css-[a-z0-9]{40}\.map},
assert_match %r{/\*# sourceMappingURL=/assets/source.css-[a-z0-9]{8}\.map},
compile_asset(find_asset("source.css", fixture_path: "mapped"))
end

test "matching nested source map" do
assert_match %r{//# sourceMappingURL=/assets/nested/another-source.js-[a-z0-9]{40}\.map},
assert_match %r{//# sourceMappingURL=/assets/nested/another-source.js-[a-z0-9]{8}\.map},
compile_asset(find_asset("nested/another-source.js", fixture_path: "mapped"))
end

Expand All @@ -38,9 +38,9 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase
end

test "sourceMappingURL not at the beginning of the line, but at end of file, is processed" do
assert_match %r{//# sourceMappingURL=/assets/sourceMappingURL-not-at-start.js-[a-z0-9]{40}\.map},
assert_match %r{//# sourceMappingURL=/assets/sourceMappingURL-not-at-start.js-[a-z0-9]{8}\.map},
compile_asset(find_asset("sourceMappingURL-not-at-start.js", fixture_path: "mapped"))
assert_match %r{/\*# sourceMappingURL=/assets/sourceMappingURL-not-at-start.css-[a-z0-9]{40}\.map \*/},
assert_match %r{/\*# sourceMappingURL=/assets/sourceMappingURL-not-at-start.css-[a-z0-9]{8}\.map \*/},
compile_asset(find_asset("sourceMappingURL-not-at-start.css", fixture_path: "mapped"))
end

Expand All @@ -56,7 +56,7 @@ class Propshaft::Compiler::SourceMappingUrlsTest < ActiveSupport::TestCase
test "relative url root" do
@options.relative_url_root = "/url-root"

assert_match %r{//# sourceMappingURL=/url-root/assets/source.js-[a-z0-9]{40}\.map},
assert_match %r{//# sourceMappingURL=/url-root/assets/source.js-[a-z0-9]{8}\.map},
compile_asset(find_asset("source.js", fixture_path: "mapped"))
end

Expand Down
2 changes: 1 addition & 1 deletion test/propshaft/compilers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Propshaft::CompilersTest < ActiveSupport::TestCase

test "replace asset-path function in css with digested url" do
@assembly.compilers.register "text/css", Propshaft::Compiler::CssAssetUrls
assert_match(/"\/assets\/archive-[a-z0-9]{40}.svg/, @assembly.compilers.compile(find_asset("another.css")))
assert_match(/"\/assets\/archive-[a-z0-9]{8}.svg/, @assembly.compilers.compile(find_asset("another.css")))
end

private
Expand Down
8 changes: 4 additions & 4 deletions test/propshaft/load_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ class Propshaft::LoadPathTest < ActiveSupport::TestCase

test "manifest" do
@load_path.manifest.tap do |manifest|
assert_equal "one-f2e1ec14d6856e1958083094170ca6119c529a73.txt", manifest["one.txt"]
assert_equal "nested/three-6c2b86a0206381310375abdd9980863c2ea7b2c3.txt", manifest["nested/three.txt"]
assert_equal "one-f2e1ec14.txt", manifest["one.txt"]
assert_equal "nested/three-6c2b86a0.txt", manifest["nested/three.txt"]
end
end

test "manifest with version" do
@load_path = Propshaft::LoadPath.new(@load_path.paths, version: "1")
@load_path.manifest.tap do |manifest|
assert_equal "one-c9373b685d5a63e4a1de7c6836a73239df552e2b.txt", manifest["one.txt"]
assert_equal "nested/three-a41a5d38da5afe428eca74b243f50405f28a6b54.txt", manifest["nested/three.txt"]
assert_equal "one-c9373b68.txt", manifest["one.txt"]
assert_equal "nested/three-a41a5d38.txt", manifest["nested/three.txt"]
end
end

Expand Down
6 changes: 3 additions & 3 deletions test/propshaft/output_path_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class Propshaft::OutputPathTest < ActiveSupport::TestCase
setup do
@manifest = {
".manifest.json": ".manifest.json",
"one.txt": "one-f2e1ec14d6856e1958083094170ca6119c529a73.txt"
"one.txt": "one-f2e1ec14.txt"
}.stringify_keys
@output_path = Propshaft::OutputPath.new(Pathname.new("#{__dir__}/../fixtures/output"), @manifest)
end

test "files" do
files = @output_path.files

file = files["one-f2e1ec14d6856e1958083094170ca6119c529a73.txt"]
file = files["one-f2e1ec14.txt"]
assert_equal "one.txt", file[:logical_path]
assert_equal "f2e1ec14d6856e1958083094170ca6119c529a73", file[:digest]
assert_equal "f2e1ec14", file[:digest]
assert file[:mtime].is_a?(Time)
end

Expand Down
Loading

0 comments on commit 0103f67

Please sign in to comment.