-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add contenthash to js/css build files
Should prevent caching issues, see #99 (comment)
- Loading branch information
Showing
5 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
module EsbuildManifest | ||
FILE_LOCATION = Rails.public_path.join("build/manifest.json") | ||
|
||
module_function | ||
|
||
def [](entry) | ||
raise StandardError, "Entrypoint '#{entry}' not found" if entrypoints[entry].blank? && !Rails.env.test? | ||
|
||
"/#{entrypoints[entry]}" | ||
end | ||
|
||
def entrypoints | ||
@entrypoints ||= parse | ||
end | ||
|
||
def parse | ||
data = JSON.parse(FILE_LOCATION.read) | ||
available_entrypoints = data["outputs"].select { |_k, v| v["entryPoint"].present? } | ||
result = {} | ||
available_entrypoints.each do |entrypoint_path, entrypoint_data| | ||
relative_path = relative_to_public(entrypoint_path) | ||
entrypoint_name = Pathname.new(entrypoint_data["entryPoint"]).basename | ||
result[entrypoint_name.to_s] = relative_path.to_s | ||
result[entrypoint_name.sub_ext(".css").to_s] = relative_to_public(entrypoint_data["cssBundle"]).to_s if entrypoint_data["cssBundle"] | ||
end | ||
result | ||
rescue Errno::ENOENT, JSON::ParserError | ||
{} | ||
end | ||
|
||
def reset_cache | ||
@entrypoints = nil | ||
end | ||
|
||
def relative_to_public(input) | ||
Rails.root.join(input).relative_path_from(Rails.public_path) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class EsbuildManifestTest < ActiveSupport::TestCase | ||
test "it correctly parses the entrypoints" do | ||
manifest = <<~JSON | ||
{ | ||
"outputs": { | ||
"public/build/application-QTMXSKWO.js": { | ||
"entryPoint": "app/typescript/application.ts", | ||
"cssBundle": "public/build/application-D737BFOQ.css" | ||
}, | ||
"public/build/application-D737BFOQ.css": {} | ||
} | ||
} | ||
JSON | ||
expected = { | ||
"application.ts" => "build/application-QTMXSKWO.js", | ||
"application.css" => "build/application-D737BFOQ.css", | ||
} | ||
stub_const(EsbuildManifest, :FILE_LOCATION, StringIO.new(manifest)) do | ||
assert_equal(expected, EsbuildManifest.parse) | ||
end | ||
end | ||
end |