Skip to content

Commit aba6f81

Browse files
committed
Summarize vendored assets into a single CSS and a single JS asset, with exceptions.
1 parent d45b31c commit aba6f81

18 files changed

+1728
-1686
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.min*

.rubocop.yml

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ inherit_gem:
44

55
AllCops:
66
Exclude:
7-
- test/db/schema.rb
8-
- vendor
7+
- test/db/schema.rb
8+
- vendor
99
SuggestExtensions: false
1010
NewCops: disable
1111

@@ -30,7 +30,7 @@ Layout/LineLength:
3030
Max: 100
3131
AllowedPatterns:
3232
# Allow long comments.
33-
- '^#.*'
33+
- "^#.*"
3434

3535
Layout/EndAlignment:
3636
Enabled: true
@@ -149,6 +149,9 @@ Style/DateTime:
149149
Style/FrozenStringLiteralComment:
150150
Enabled: false
151151

152+
Style/IfInsideElse:
153+
Enabled: false
154+
152155
Style/MultilineTernaryOperator:
153156
Enabled: false
154157

Rakefile

+25-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ task :maintain_assets do
1717
Dir.mkdir(File.expand_path("#{path_type}/rest_framework", assets_dir))
1818
end
1919

20-
# Vendor each asset.
20+
# Vendor each asset, summarizing assets into a single `external` asset unless they have
21+
# `extra_tag_attrs`.
22+
css_content = ""
23+
css_path = File.expand_path("stylesheets/#{RESTFramework::EXTERNAL_CSS_NAME}", assets_dir)
24+
js_content = ""
25+
js_path = File.expand_path("javascripts/#{RESTFramework::EXTERNAL_JS_NAME}", assets_dir)
2126
RESTFramework::EXTERNAL_ASSETS.each do |name, cfg|
2227
file_path = File.expand_path("#{cfg[:place]}/rest_framework/#{name}", assets_dir)
2328

@@ -57,8 +62,26 @@ task :maintain_assets do
5762
}
5863
end
5964

60-
File.write(file_path, content)
65+
# Normalize content leading/trailing whitespace.
66+
content = content.strip + "\n"
67+
68+
if cfg[:extra_tag_attrs].present?
69+
# If `extra_tag_attrs` is set, then we write the asset to its own file so the tag attributes
70+
# only affect that particular asset.
71+
File.write(file_path, content)
72+
else
73+
# Otherwise, we append to the external asset.
74+
if cfg[:place] == "stylesheets"
75+
css_content << content
76+
elsif cfg[:place] == "javascripts"
77+
js_content << content
78+
else
79+
raise "Unknown asset place."
80+
end
81+
end
6182
end
83+
File.write(css_path, css_content)
84+
File.write(js_path, js_content)
6285

6386
# Update shared css/js.
6487
shared = <<~SHARED

app/views/rest_framework/head/_external.html.erb

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
<% if RESTFramework.config.use_vendored_assets %>
2-
<% RESTFramework::EXTERNAL_ASSETS.each do |name, cfg| %>
2+
<%= javascript_include_tag RESTFramework::EXTERNAL_JS_NAME, defer: true %>
3+
<%= stylesheet_link_tag RESTFramework::EXTERNAL_CSS_NAME %>
4+
5+
<% RESTFramework::EXTERNAL_UNSUMMARIZED_ASSETS.each do |name, cfg| %>
36
<% if cfg[:place] == "javascripts" %>
47
<%= javascript_include_tag "rest_framework/#{name}", defer: true, **cfg[:extra_tag_attrs] %>
5-
<% else %>
8+
<% elsif cfg[:place] == "stylesheets" %>
69
<%= stylesheet_link_tag "rest_framework/#{name}", **cfg[:extra_tag_attrs] %>
10+
<% else %>
11+
<% raise "Unknown asset place." %>
712
<% end %>
813
<% end %>
914
<% else %>

lib/rest_framework.rb

+11-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ module RESTFramework
2020
destroy_all: :delete,
2121
}.freeze
2222

23+
# We put most vendored external assets into these files to make precompilation and serving faster.
24+
EXTERNAL_CSS_NAME = "rest_framework/external.min.css"
25+
EXTERNAL_JS_NAME = "rest_framework/external.min.js"
26+
2327
# We should always add the `.min` extension prefix even if the assets are not minified, to avoid
2428
# sprockets minifying the assets. We target propshaft, so we want these assets to just be passed
2529
# through.
@@ -81,7 +85,8 @@ module RESTFramework
8185
exclude_from_docs: true,
8286
},
8387
}.map { |name, cfg|
84-
if File.extname(name) == ".js"
88+
ext = File.extname(name)
89+
if ext == ".js"
8590
cfg[:place] = "javascripts"
8691
cfg[:extra_tag_attrs] ||= {}
8792
cfg[:tag_attrs] = {
@@ -93,7 +98,7 @@ module RESTFramework
9398
**cfg[:extra_tag_attrs],
9499
}
95100
cfg[:tag] = ActionController::Base.helpers.tag.script(**cfg[:tag_attrs])
96-
else
101+
elsif ext == ".css"
97102
cfg[:place] = "stylesheets"
98103
cfg[:extra_tag_attrs] ||= {}
99104
cfg[:tag_attrs] = {
@@ -104,12 +109,16 @@ module RESTFramework
104109
**cfg[:extra_tag_attrs],
105110
}
106111
cfg[:tag] = ActionController::Base.helpers.tag.link(**cfg[:tag_attrs])
112+
else
113+
raise "Unknown asset extension: #{ext}."
107114
end
108115

109116
[name, cfg]
110117
}.to_h.freeze
111118
# rubocop:enable Layout/LineLength
112119

120+
EXTERNAL_UNSUMMARIZED_ASSETS = EXTERNAL_ASSETS.select { |_, cfg| cfg[:extra_tag_attrs].present? }
121+
113122
# Global configuration should be kept minimal, as controller-level configurations allows multiple
114123
# APIs to be defined to behave differently.
115124
class Config

lib/rest_framework/engine.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ class RESTFramework::Engine < Rails::Engine
22
initializer "rest_framework.assets" do
33
config.after_initialize do |app|
44
if RESTFramework.config.use_vendored_assets
5-
app.config.assets.precompile += RESTFramework::EXTERNAL_ASSETS.keys.map do |name|
6-
"rest_framework/#{name}"
7-
end
5+
app.config.assets.precompile += [
6+
RESTFramework::EXTERNAL_CSS_NAME,
7+
RESTFramework::EXTERNAL_JS_NAME,
8+
*RESTFramework::EXTERNAL_UNSUMMARIZED_ASSETS.keys.map { |name| "rest_framework/#{name}" },
9+
]
810
end
911
end
1012
end

vendor/assets/javascripts/rest_framework/bootstrap.min.js

-6
This file was deleted.

vendor/assets/javascripts/rest_framework/external.min.js

+1,256
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/assets/javascripts/rest_framework/highlight-json.min.js

-7
This file was deleted.

vendor/assets/javascripts/rest_framework/highlight-xml.min.js

-29
This file was deleted.

0 commit comments

Comments
 (0)