Skip to content

Commit fb1ab34

Browse files
dhhskipkayhilrmacklin
authored
Make Sprockets more optional, offer Propshaft as alternative (rails#43261)
* Make Sprockets more optional, offer Propshaft as alternative * Whups, local reference * No longer used * Spacing * Need explicit sprockets-rails inclusion now * Manually require the sprockets railtie * Don't need these changes right now * Kick off another build * Fix tests * DRY up test * Require railtie when using sprockets * Introduce option to skip asset pipeline * No longer relevant * Always have to return * Gone * Add helper for skip_sprockets? * Fix guard statement * Use latest gems * Include propshaft * fix tests for rails#43261 (rails#43277) * help fix tests for rails#43261 skip_sprockets? should not be called on options :skip_sprockets is no longer a value in the option hash, so skip_sprockets? should not be called on it move --asset-pipeline to shared generator skip_sprockets? is defined on app_base, and used in the plugin generator to determine whether to add the engine's assets to the dummy sprockets manifest, so I believe it makes sense to include in both generators Because of this change, I also changed the shared test back to testing against non-sprockets add skip_sprockets to Gemfile template vars Mocking skip_sprockets? in app_base generator fix more generator tests * use skip_sprockets? everywhere * Use latest propshaft * Update `AssetUrlHelper` docs to list both asset pipeline gems (rails#43328) * Update to latest Propshaft * Bump Propshaft again * Ask for latest * Use latest propshaft Co-authored-by: Hartley McGuire <[email protected]> Co-authored-by: Richard Macklin <[email protected]>
1 parent 2ec2ee6 commit fb1ab34

File tree

20 files changed

+63
-44
lines changed

20 files changed

+63
-44
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ gemspec
99
# We need a newish Rake since Active Job sets its test tasks' descriptions.
1010
gem "rake", ">= 11.1"
1111

12+
gem "sprockets-rails", ">= 2.0.0"
13+
gem "propshaft", ">= 0.1.7"
1214
gem "capybara", ">= 3.26"
1315
gem "selenium-webdriver", ">= 4.0.0.alpha7"
1416

Gemfile.lock

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ PATH
9696
activesupport (= 7.0.0.alpha2)
9797
bundler (>= 1.15.0)
9898
railties (= 7.0.0.alpha2)
99-
sprockets-rails (>= 2.0.0)
10099
railties (7.0.0.alpha2)
101100
actionpack (= 7.0.0.alpha2)
102101
activesupport (= 7.0.0.alpha2)
@@ -184,8 +183,8 @@ GEM
184183
crack (0.4.5)
185184
rexml
186185
crass (1.0.6)
187-
cssbundling-rails (0.1.0)
188-
rails (>= 6.0.0)
186+
cssbundling-rails (0.2.2)
187+
railties (>= 6.0.0)
189188
curses (1.4.2)
190189
daemons (1.4.0)
191190
dalli (2.7.11)
@@ -291,14 +290,14 @@ GEM
291290
image_processing (1.12.1)
292291
mini_magick (>= 4.9.5, < 5)
293292
ruby-vips (>= 2.0.17, < 3)
294-
importmap-rails (0.5.1)
293+
importmap-rails (0.7.3)
295294
rails (>= 6.0.0)
296295
io-console (0.5.9)
297296
irb (1.3.7)
298297
reline (>= 0.2.7)
299298
jmespath (1.4.0)
300-
jsbundling-rails (0.1.0)
301-
rails (>= 6.0.0)
299+
jsbundling-rails (0.1.7)
300+
railties (>= 6.0.0)
302301
json (2.5.1)
303302
jwt (2.2.3)
304303
kindlerb (1.2.0)
@@ -352,6 +351,8 @@ GEM
352351
ast (~> 2.4.1)
353352
path_expander (1.1.0)
354353
pg (1.2.3)
354+
propshaft (0.1.7)
355+
rails (>= 7.0.0.alpha2)
355356
psych (3.3.2)
356357
public_suffix (4.0.6)
357358
puma (5.3.2)
@@ -476,7 +477,7 @@ GEM
476477
sprockets (>= 3.0.0)
477478
sqlite3 (1.4.2)
478479
stackprof (0.2.17)
479-
stimulus-rails (0.4.2)
480+
stimulus-rails (0.5.4)
480481
rails (>= 6.0.0)
481482
sucker_punch (3.0.1)
482483
concurrent-ruby (~> 1.0)
@@ -491,7 +492,7 @@ GEM
491492
thor (1.1.0)
492493
tilt (2.0.10)
493494
trailblazer-option (0.1.1)
494-
turbo-rails (0.7.11)
495+
turbo-rails (0.7.14)
495496
rails (>= 6.0.0)
496497
tzinfo (2.0.4)
497498
concurrent-ruby (~> 1.0)
@@ -561,6 +562,7 @@ DEPENDENCIES
561562
mysql2 (~> 0.5)!
562563
nokogiri (>= 1.8.1, != 1.11.0)
563564
pg (~> 1.1)
565+
propshaft (>= 0.1.7)
564566
psych (~> 3.0)
565567
puma
566568
que
@@ -587,6 +589,7 @@ DEPENDENCIES
587589
sidekiq
588590
sneakers
589591
sprockets-export
592+
sprockets-rails (>= 2.0.0)
590593
sqlite3 (~> 1.4)
591594
stackprof
592595
stimulus-rails
@@ -603,4 +606,4 @@ DEPENDENCIES
603606
websocket-client-simple!
604607

605608
BUNDLED WITH
606-
2.2.25
609+
2.2.28

actionview/lib/action_view/helpers/asset_url_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ module AssetUrlHelper
121121
URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}i
122122

123123
# This is the entry point for all assets.
124-
# When using the asset pipeline (i.e. sprockets and sprockets-rails), the
124+
# When using an asset pipeline gem (e.g. propshaft or sprockets-rails), the
125125
# behavior is "enhanced". You can bypass the asset pipeline by passing in
126126
# <tt>skip_pipeline: true</tt> to the options.
127127
#
@@ -130,7 +130,7 @@ module AssetUrlHelper
130130
# === With the asset pipeline
131131
#
132132
# All options passed to +asset_path+ will be passed to +compute_asset_path+
133-
# which is implemented by sprockets-rails.
133+
# which is implemented by asset pipeline gems.
134134
#
135135
# asset_path("application.js") # => "/assets/application-60aa4fdc5cea14baf5400fba1abf4f2a46a5166bad4772b1effe341570f07de9.js"
136136
# asset_path('application.js', host: 'example.com') # => "//example.com/assets/application.js"

rails.gemspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,5 @@ Gem::Specification.new do |s|
4141
s.add_dependency "actiontext", version
4242
s.add_dependency "railties", version
4343

44-
s.add_dependency "bundler", ">= 1.15.0"
45-
s.add_dependency "sprockets-rails", ">= 2.0.0"
44+
s.add_dependency "bundler", ">= 1.15.0"
4645
end

railties/lib/rails/all.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
action_mailbox/engine
1616
action_text/engine
1717
rails/test_unit/railtie
18-
sprockets/railtie
1918
).each do |railtie|
2019
begin
2120
require railtie

railties/lib/rails/app_updater.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def generator_options
2525
options[:skip_active_storage] = !defined?(ActiveStorage::Engine) || !defined?(ActiveRecord::Railtie)
2626
options[:skip_action_mailer] = !defined?(ActionMailer::Railtie)
2727
options[:skip_action_cable] = !defined?(ActionCable::Engine)
28-
options[:skip_sprockets] = !defined?(Sprockets::Railtie)
28+
options[:skip_asset_pipeline] = !defined?(Sprockets::Railtie) && !defined?(Propshaft::Railtie)
2929
options[:skip_bootsnap] = !defined?(Bootsnap)
3030
options[:updating] = true
3131
options

railties/lib/rails/generators/app_base.rb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ def self.add_shared_options_for(name)
5858
class_option :skip_action_cable, type: :boolean, aliases: "-C", default: false,
5959
desc: "Skip Action Cable files"
6060

61-
class_option :skip_sprockets, type: :boolean, aliases: "-S", default: false,
62-
desc: "Skip Sprockets files"
61+
class_option :skip_asset_pipeline, type: :boolean, aliases: "-A", default: false
62+
63+
class_option :asset_pipeline, type: :string, aliases: "-a", default: "sprockets",
64+
desc: "Choose your asset pipeline [options: sprockets (default), propshaft]"
6365

6466
class_option :skip_javascript, type: :boolean, aliases: "-J", default: name == "plugin",
6567
desc: "Skip JavaScript files"
@@ -106,6 +108,7 @@ def initialize(*)
106108
private
107109
def gemfile_entries # :doc:
108110
[rails_gemfile_entry,
111+
asset_pipeline_gemfile_entry,
109112
database_gemfile_entry,
110113
web_server_gemfile_entry,
111114
javascript_gemfile_entry,
@@ -165,13 +168,25 @@ def web_server_gemfile_entry # :doc:
165168
GemfileEntry.new "puma", "~> 5.0", "Use the Puma web server [https://github.com/puma/puma]"
166169
end
167170

171+
def asset_pipeline_gemfile_entry
172+
return [] if options[:skip_asset_pipeline]
173+
174+
if options[:asset_pipeline] == "sprockets"
175+
GemfileEntry.version "sprockets-rails", ">= 2.0.0",
176+
"The traditional bundling and transpiling asset pipeline for Rails."
177+
elsif options[:asset_pipeline] == "propshaft"
178+
GemfileEntry.version "propshaft", ">= 0.1.7", "The modern asset pipeline for Rails."
179+
else
180+
[]
181+
end
182+
end
183+
168184
def include_all_railties? # :doc:
169185
[
170186
options.values_at(
171187
:skip_active_record,
172188
:skip_action_mailer,
173189
:skip_test,
174-
:skip_sprockets,
175190
:skip_action_cable,
176191
:skip_active_job
177192
),
@@ -218,6 +233,11 @@ def skip_dev_gems? # :doc:
218233
options[:skip_dev_gems]
219234
end
220235

236+
def skip_sprockets?
237+
options[:skip_asset_pipeline] || options[:asset_pipeline] != "sprockets"
238+
end
239+
240+
221241
class GemfileEntry < Struct.new(:name, :version, :comment, :options, :commented_out)
222242
def initialize(name, version, comment, options = {}, commented_out = false)
223243
super

railties/lib/rails/generators/rails/app/app_generator.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,15 @@ def config_when_updating
138138
template "config/storage.yml"
139139
end
140140

141-
if options[:skip_sprockets] && !assets_config_exist
141+
if skip_sprockets? && !assets_config_exist
142142
remove_file "config/initializers/assets.rb"
143143
end
144144

145-
if options[:skip_sprockets] && !asset_manifest_exist
145+
if skip_sprockets? && !asset_manifest_exist
146146
remove_file "app/assets/config/manifest.js"
147147
end
148148

149-
if options[:skip_sprockets] && !asset_app_stylesheet_exist
149+
if skip_sprockets? && !asset_app_stylesheet_exist
150150
remove_file "app/assets/stylesheets/application.css"
151151
end
152152

@@ -163,6 +163,10 @@ def config_when_updating
163163
remove_file "config/initializers/permissions_policy.rb"
164164
end
165165
end
166+
167+
if !skip_sprockets?
168+
insert_into_file "config/application.rb", %(require "sprockets/railtie"), after: /require\(["']rails\/all["']\)\n/
169+
end
166170
end
167171

168172
def master_key
@@ -275,7 +279,7 @@ def initialize(*args)
275279
# Force sprockets and JavaScript to be skipped when generating API only apps.
276280
# Can't modify options hash as it's frozen by default.
277281
if options[:api]
278-
self.options = options.merge(skip_sprockets: true, skip_javascript: true).freeze
282+
self.options = options.merge(skip_asset_pipeline: true, skip_javascript: true).freeze
279283
end
280284

281285
if options[:minimal]
@@ -440,7 +444,7 @@ def delete_public_files_if_api_option
440444
end
441445

442446
def delete_assets_initializer_skipping_sprockets
443-
if options[:skip_sprockets]
447+
if skip_sprockets?
444448
remove_file "config/initializers/assets.rb"
445449
remove_file "app/assets/config/manifest.js"
446450
remove_file "app/assets/stylesheets/application.css"

railties/lib/rails/generators/rails/app/templates/Gemfile.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
2626
# Reduces boot times through caching; required in config/boot.rb
2727
gem "bootsnap", ">= 1.4.4", require: false
2828
<% end -%>
29-
<% unless options.skip_sprockets? || options.minimal? -%>
29+
<% unless skip_sprockets? || options.minimal? -%>
3030

3131
# Use Sass to process CSS
3232
# gem "sassc-rails", "~> 2.1"

railties/lib/rails/generators/rails/app/templates/config/application.rb.tt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ require "action_controller/railtie"
1515
<%= comment_if :skip_action_text %>require "action_text/engine"
1616
require "action_view/railtie"
1717
<%= comment_if :skip_action_cable %>require "action_cable/engine"
18-
<%= comment_if :skip_sprockets %>require "sprockets/railtie"
1918
<%= comment_if :skip_test %>require "rails/test_unit/railtie"
2019
<% end -%>
2120

0 commit comments

Comments
 (0)