Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimenting with optional vite integration. #1729

Merged
merged 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions public/vite-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"src/images/lucky_logo.png": {
"file": "images/lucky_logo.a54cc67e.png",
"src": "src/images/lucky_logo.png"
},
"src/assets/js/app.js": {
"file": "assets/js/app.a54cc67e.js",
"src": "src/assets/js/app.js"
}
}
54 changes: 50 additions & 4 deletions spec/lucky/asset_helpers_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ module Shared::ComponentWithAsset
asset("images/logo.png")
end

def dynamic_asset_inside_component
interpolated = "logo"
def dynamic_asset_inside_component(interpolated = "logo")
dynamic_asset("images/#{interpolated}.png")
end

def vite_asset_inside_component
asset("images/lucky_logo.png")
end
end

private class TestPage
Expand All @@ -23,14 +26,17 @@ private class TestPage
asset("images/inside-assets-folder.png")
end

def dynamic_asset_path
interpolated = "logo"
def dynamic_asset_path(interpolated = "logo")
dynamic_asset("images/#{interpolated}.png")
end

def missing_dynamic_asset_path
dynamic_asset("woops!.png")
end

def asset_path_from_vite
asset("images/lucky_logo.png")
end
end

describe Lucky::AssetHelpers do
Expand Down Expand Up @@ -79,4 +85,44 @@ describe Lucky::AssetHelpers do
end
end
end

describe "testing with vite manifest" do
it "returns the fingerprinted path" do
Lucky::AssetHelpers.asset("images/lucky_logo.png").should eq "/images/lucky_logo.a54cc67e.png"
end

it "works when included in another class" do
TestPage.new.asset_path_from_vite.should eq "/images/lucky_logo.a54cc67e.png"
end

it "works when used from an included module" do
TestPage.new.vite_asset_inside_component.should eq "/images/lucky_logo.a54cc67e.png"
end

it "prepends the asset_host configuration option" do
Lucky::Server.temp_config(asset_host: "https://production.com") do
TestPage.new.asset_path_from_vite.should eq "https://production.com/images/lucky_logo.a54cc67e.png"
end
end

it "returns the fingerprinted path" do
TestPage.new.dynamic_asset_path("lucky_logo").should eq "/images/lucky_logo.a54cc67e.png"
end

it "works inside included module" do
TestPage.new.dynamic_asset_inside_component("lucky_logo").should eq "/images/lucky_logo.a54cc67e.png"
end

it "raises a helpful error" do
expect_raises Exception, "Missing asset: woops!.png" do
TestPage.new.missing_dynamic_asset_path
end
end

it "prepends the asset_host configuration option" do
Lucky::Server.temp_config(asset_host: "https://production.com") do
TestPage.new.dynamic_asset_path("lucky_logo").should eq "https://production.com/images/lucky_logo.a54cc67e.png"
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ include RoutesHelper
Pulsar.enable_test_mode!

Lucky::AssetHelpers.load_manifest
Lucky::AssetHelpers.load_manifest("./public/vite-manifest.json", use_vite: true)
Comment on lines 10 to +11
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In practice you would never run this more than once, but this was the only way I could test it since it happens at compile-time.


Spec.before_each do
ARGV.clear
Expand Down
7 changes: 7 additions & 0 deletions src/lucky/asset_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ module Lucky::AssetHelpers
{% CONFIG[:has_loaded_manifest] = true %}
end

# EXPERIMENTAL: This feature is experimental. Use this to test
# vite integration with Lucky
macro load_manifest(manifest_file, use_vite)
{{ run "../run_macros/generate_asset_helpers", manifest_file, use_vite }}
{% CONFIG[:has_loaded_manifest] = true %}
end

# Return the string path to an asset
#
# ```
Expand Down
26 changes: 22 additions & 4 deletions src/run_macros/generate_asset_helpers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ private class AssetManifestBuilder
property retries
@retries : Int32 = 0
@manifest_path : String = File.expand_path("./public/mix-manifest.json")
@use_vite : Bool = false

def initialize
end

def initialize(@manifest_path)
def initialize(@manifest_path, @use_vite : Bool = false)
end

def build_with_retry
if manifest_exists?
build
if @use_vite
build_with_vite_manifest
else
build_with_mix_manifest
end
else
retry_or_raise_error
end
Expand All @@ -33,16 +38,28 @@ private class AssetManifestBuilder
end
end

private def build
private def build_with_mix_manifest
manifest_file = File.read(@manifest_path)
manifest = JSON.parse(manifest_file)

manifest.as_h.each do |key, value|
# "/js/app.js" => "js/app.js",
key = key.gsub(/^\//, "").gsub(/^assets\//, "")
puts %({% ::Lucky::AssetHelpers::ASSET_MANIFEST["#{key}"] = "#{value.as_s}" %})
end
end

private def build_with_vite_manifest
manifest_file = File.read(@manifest_path)
manifest = JSON.parse(manifest_file)

manifest.as_h.each do |key, value|
# "src/images/lucky_logo.png" => "images/lucky_logo.png"
key = key[4..]
puts %({% ::Lucky::AssetHelpers::ASSET_MANIFEST["#{key}"] = "/#{value["file"].as_s}" %})
end
end

private def manifest_exists?
File.exists?(@manifest_path)
end
Expand All @@ -55,11 +72,12 @@ end

begin
manifest_path = ARGV[0]
use_vite = ARGV[1]? == "true"

builder = if manifest_path.blank?
AssetManifestBuilder.new
else
AssetManifestBuilder.new(manifest_path)
AssetManifestBuilder.new(manifest_path, use_vite)
end

builder.build_with_retry
Expand Down