Skip to content

Commit af7428c

Browse files
David Heinemeier Hanssonghiculescuandrehjrabhaynikamguilleiguaran
authored
Replace webpack with importmapped Hotwire as default js (rails#42999)
* Turbolinks is being replaced with Hotwire * Make --webpack opt-in * Don't use specific webpacker installers any more in preparation for next Webpacker * Update railties/lib/rails/app_updater.rb Co-authored-by: Alex Ghiculescu <[email protected]> * Trailing whitespace * Convert to Turbo data attribute for tracking * Default is no webpack, no hotwire * Swap out turbolinks references for hotwire * Drop explicit return * Only generate package.json if using webpack * Only create package.json in webpack mode * Only create app/javascript in webpack mode * Generate correct style/js links based on js mode * Fix tests from changed output format Not sure why these are showing up in this PR, though. * Rubocopping * Stick with webpack for the test app for now * Adjust tests * Replace minitest-reporters with minitest-ci (rails#43016) minitest-reporters is used to create junit xml reports on CI. But when it loads before rails minitest plugin makes `Rails::TestUnitReporter` not being added as a reporter. minitest-ci is now only loaded at ci and does not interferes with rails minitest plugins. And keeps junit reports workings * Too heavy handed to actually run bundle Just like we don't auto-migrate * Pin js frameworks in importmap Instead of having importmap preconfigure it. * Match updated app/javascript path * No need for the explaining comment * Fixes test cases for replace webpack with importmapped Hotwire as default js (rails#42999) * Fix rubocop issues * Fix more railities test cases * Fix plugin generator railties shared test cases * Fix Action Text install generator asset pipeline spec * They're modules, not files * Let dev use the latest release as well So we don't have to replace unexisting dev releases with latest release * Make Webpack responsible for generating all the JS files it needs Webpacker 6 has already moved from app/javascript to app/packs. * Don't add rails/ujs by default any longer All the ajax/form functionality has been superseded by Turbo. The rest lives in a weird inbetween land we need to address through other means. * Use new importmap location * Switch to using turbo-rails and stimulus-rails directly The hotwire-rails gem does not offer enough value for its indirection * Use latest Webpacker * Prevent version resolution requests from getting swallowed * Use ESM syntax for imports * Move management of yarn, package.json, etc to Webpacker 6 * Update for Webpacker 6 * Move bin/setup addition to Webpacker as well * Remove dead tests * Bump to Webpacker 6.0.0.rc.2 * No longer relevant given the new default is no webpacker * Rely on Webpacker 6 * No longer relevant * No longer relevant * Make cable channel generator work for both webpacker and importmap setups * Fix tests * For tests testing importmap way * Use Webpacker 6 dummy * RuboCopping * One more bump to fix webpack-dev-server * Another bump. Hopefully the last one! * Also enough to not want turbo tracking on * Fix tests * Latest * Fix tests * Fix more tests * Fix tests Co-authored-by: Alex Ghiculescu <[email protected]> Co-authored-by: André Luis Leal Cardoso Junior <[email protected]> Co-authored-by: Abhay Nikam <[email protected]> Co-authored-by: Guillermo Iguaran <[email protected]>
1 parent 39f15e1 commit af7428c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+328
-1482
lines changed

Gemfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ gem "selenium-webdriver", ">= 4.0.0.alpha7"
1414

1515
gem "rack-cache", "~> 1.2"
1616
gem "sass-rails"
17-
gem "turbolinks", "~> 5"
18-
gem "webpacker", "~> 5.0", require: ENV["SKIP_REQUIRE_WEBPACKER"] != "true"
17+
gem "stimulus-rails"
18+
gem "turbo-rails"
19+
gem "webpacker", "~> 6.0.0.rc.5", require: ENV["SKIP_REQUIRE_WEBPACKER"] != "true"
20+
gem "importmap-rails"
1921
# require: false so bcrypt is loaded only when has_secure_password is used.
2022
# This is to avoid Active Model (and by extension the entire framework)
2123
# being dependent on a binary library.
@@ -111,8 +113,8 @@ instance_eval File.read local_gemfile if File.exist? local_gemfile
111113

112114
group :test do
113115
gem "minitest-bisect"
116+
gem "minitest-ci", require: false
114117
gem "minitest-retry"
115-
gem "minitest-reporters"
116118

117119
platforms :mri do
118120
gem "stackprof"

Gemfile.lock

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ GEM
110110
addressable (2.7.0)
111111
public_suffix (>= 2.0.2, < 5.0)
112112
amq-protocol (2.3.2)
113-
ansi (1.5.0)
114113
ast (2.4.2)
115114
aws-eventstream (1.1.1)
116115
aws-partitions (1.469.0)
@@ -287,6 +286,8 @@ GEM
287286
image_processing (1.12.1)
288287
mini_magick (>= 4.9.5, < 5)
289288
ruby-vips (>= 2.0.17, < 3)
289+
importmap-rails (0.3.4)
290+
rails (>= 6.0.0)
290291
jmespath (1.4.0)
291292
json (2.5.1)
292293
jwt (2.2.3)
@@ -312,11 +313,8 @@ GEM
312313
minitest-bisect (1.5.1)
313314
minitest-server (~> 1.0)
314315
path_expander (~> 1.1)
315-
minitest-reporters (1.4.3)
316-
ansi
317-
builder
318-
minitest (>= 5.0)
319-
ruby-progressbar
316+
minitest-ci (3.4.0)
317+
minitest (>= 5.0.6)
320318
minitest-retry (0.2.2)
321319
minitest (>= 5.0)
322320
minitest-server (1.0.6)
@@ -479,6 +477,8 @@ GEM
479477
sprockets (>= 3.0.0)
480478
sqlite3 (1.4.2)
481479
stackprof (0.2.17)
480+
stimulus-rails (0.3.9)
481+
rails (>= 6.0.0)
482482
sucker_punch (3.0.1)
483483
concurrent-ruby (~> 1.0)
484484
terser (1.1.4)
@@ -490,9 +490,8 @@ GEM
490490
thor (1.1.0)
491491
tilt (2.0.10)
492492
trailblazer-option (0.1.1)
493-
turbolinks (5.2.1)
494-
turbolinks-source (~> 5.2)
495-
turbolinks-source (5.2.0)
493+
turbo-rails (0.7.4)
494+
rails (>= 6.0.0)
496495
tzinfo (2.0.4)
497496
concurrent-ruby (~> 1.0)
498497
uber (0.1.0)
@@ -512,7 +511,7 @@ GEM
512511
addressable (>= 2.3.6)
513512
crack (>= 0.3.2)
514513
hashdiff (>= 0.4.0, < 2.0.0)
515-
webpacker (5.4.0)
514+
webpacker (6.0.0.rc.5)
516515
activesupport (>= 5.2)
517516
rack-proxy (>= 0.6.1)
518517
railties (>= 5.2)
@@ -553,12 +552,13 @@ DEPENDENCIES
553552
google-cloud-storage (~> 1.11)
554553
hiredis
555554
image_processing (~> 1.2)
555+
importmap-rails
556556
json (>= 2.0.0)
557557
kindlerb (~> 1.2.0)
558558
libxml-ruby
559559
listen (~> 3.3)
560560
minitest-bisect
561-
minitest-reporters
561+
minitest-ci
562562
minitest-retry
563563
mysql2 (~> 0.5)!
564564
nokogiri (>= 1.8.1, != 1.11.0)
@@ -592,17 +592,18 @@ DEPENDENCIES
592592
sprockets-export
593593
sqlite3 (~> 1.4)
594594
stackprof
595+
stimulus-rails
595596
sucker_punch
596597
terser (>= 1.1.4)
597-
turbolinks (~> 5)
598+
turbo-rails
598599
tzinfo-data
599600
w3c_validators (~> 1.3.6)
600601
wdm (>= 0.1.0)
601602
webdrivers
602603
webmock
603-
webpacker (~> 5.0)
604+
webpacker (~> 6.0.0.rc.5)
604605
webrick
605606
websocket-client-simple!
606607

607608
BUNDLED WITH
608-
2.2.19
609+
2.2.25

actioncable/lib/action_cable/helpers/action_cable_helper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ module ActionCableHelper
88
#
99
# <head>
1010
# <%= action_cable_meta_tag %>
11-
# <%= javascript_include_tag 'application', 'data-turbolinks-track' => 'reload' %>
11+
# <%= javascript_include_tag 'application', 'data-turbo-track' => 'reload' %>
1212
# </head>
1313
#
1414
# This is then used by Action Cable to determine the URL of your WebSocket server.
1515
# Your JavaScript can then connect to the server without needing to specify the
1616
# URL directly:
1717
#
18-
# window.Cable = require("@rails/actioncable")
18+
# import Cable from "@rails/actioncable"
19+
# window.Cable = Cable
1920
# window.App = {}
2021
# App.cable = Cable.createConsumer()
2122
#

actioncable/lib/rails/generators/channel/USAGE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Example:
1010
creates a Chat channel class, test and JavaScript asset:
1111
Channel: app/channels/chat_channel.rb
1212
Test: test/channels/chat_channel_test.rb
13-
Assets: app/javascript/channels/chat_channel.js
13+
Assets: $JAVASCRIPT_PATH/channels/chat_channel.js

actioncable/lib/rails/generators/channel/channel_generator.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,23 @@ def create_channel_file
1818

1919
if options[:assets]
2020
if behavior == :invoke
21-
template "javascript/index.js", "app/javascript/channels/index.js"
22-
template "javascript/consumer.js", "app/javascript/channels/consumer.js"
21+
if defined?(Webpacker::Engine)
22+
template "javascript/index.js", "#{Webpacker.config.source_path}/channels/index.js"
23+
template "javascript/consumer.js", "#{Webpacker.config.source_path}/channels/consumer.js"
24+
else
25+
template "javascript/consumer.js", "app/javascript/channels/consumer.js"
26+
end
2327
end
2428

25-
js_template "javascript/channel", File.join("app/javascript/channels", class_path, "#{file_name}_channel")
29+
if defined?(Webpacker::Engine)
30+
js_template "javascript/channel", File.join(Webpacker.config.source_path, "channels", class_path, "#{file_name}_channel")
31+
else
32+
channel_js_path = File.join("app/javascript/channels", class_path, "#{file_name}_channel")
33+
js_template "javascript/channel", channel_js_path
34+
gsub_file "#{channel_js_path}.js", /\.\/consumer/, "channels/consumer"
35+
36+
append_to_file "app/javascript/application.js", %(\nimport "channels/#{file_name}_channel"\n)
37+
end
2638
end
2739

2840
generate_application_cable_files

actiontext/lib/generators/action_text/install/install_generator.rb

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,52 @@ class InstallGenerator < ::Rails::Generators::Base
1010

1111
def install_javascript_dependencies
1212
if defined?(Webpacker::Engine)
13-
rails_command "app:binstub:yarn", inline: true
14-
1513
say "Installing JavaScript dependencies", :green
16-
yarn_command "add #{js_dependencies.map { |name, version| "#{name}@#{version}" }.join(" ")}", capture: true
14+
yarn_command "add #{js_dependencies.map { |name, version| "#{name}@#{version}" }.join(" ")}"
1715
end
1816
end
1917

2018
def append_javascript_dependencies
2119
if defined?(Webpacker::Engine)
22-
in_root do
23-
if (app_javascript_pack_path = Pathname.new("app/javascript/packs/application.js")).exist?
24-
js_dependencies.each_key do |dependency|
25-
line = %[require("#{dependency}")]
26-
27-
unless app_javascript_pack_path.read.include? line
28-
say "Adding #{dependency} to #{app_javascript_pack_path}", :green
29-
append_to_file app_javascript_pack_path, "\n#{line}"
30-
end
20+
if (app_javascript_pack_path = Pathname.new("#{Webpacker.config.source_entry_path}/application.js")).exist?
21+
js_dependencies.each_key do |dependency|
22+
line = %[import "#{dependency}"]
23+
24+
unless app_javascript_pack_path.read.include? line
25+
say "Adding #{dependency} to #{app_javascript_pack_path}", :green
26+
append_to_file app_javascript_pack_path, "\n#{line}"
3127
end
32-
else
33-
say <<~WARNING, :red
34-
WARNING: Action Text can't locate your JavaScript bundle to add its package dependencies.
28+
end
29+
else
30+
say <<~WARNING, :red
31+
WARNING: Action Text can't locate your JavaScript bundle to add its package dependencies.
3532
36-
Add these lines to any bundles:
33+
Add these lines to any bundles:
3734
38-
require("trix")
39-
require("@rails/actiontext")
35+
import "trix"
36+
import "@rails/actiontext"
4037
41-
Alternatively, install and setup the webpacker gem then rerun `bin/rails action_text:install`
42-
to have these dependencies added automatically.
43-
WARNING
44-
end
38+
Alternatively, install and setup the webpacker gem then rerun `bin/rails action_text:install`
39+
to have these dependencies added automatically.
40+
WARNING
4541
end
4642
else
47-
if (application_javascript_path = Rails.root.join("app/assets/javascripts/application.js")).exist?
43+
if (application_javascript_path = Rails.root.join("app/javascript/application.js")).exist?
4844
insert_into_file application_javascript_path.to_s, %(\nimport "trix"\nimport "@rails/actiontext")
4945
else
5046
say <<~INSTRUCTIONS, :green
51-
You must import the @rails/actiontext.js and trix.js JavaScript files in your application entrypoint.
47+
You must import the @rails/actiontext and trix JavaScript modules in your application entrypoint.
48+
INSTRUCTIONS
49+
end
50+
51+
if (importmap_path = Rails.root.join("config/importmap.rb")).exist?
52+
insert_into_file \
53+
importmap_path.to_s,
54+
%( pin "trix"\n pin "@rails/actiontext", to: "actiontext.js"\n\n),
55+
after: "Rails.application.config.importmap.draw do\n"
56+
else
57+
say <<~INSTRUCTIONS, :green
58+
You must add @rails/actiontext and trix to your importmap to reference them via ESM.
5259
INSTRUCTIONS
5360
end
5461
end
@@ -66,9 +73,8 @@ def create_actiontext_files
6673

6774
def enable_image_processing_gem
6875
if (gemfile_path = Rails.root.join("Gemfile")).exist?
69-
say "Ensure image_processing gem has been enabled so image uploads will work"
76+
say "Ensure image_processing gem has been enabled so image uploads will work (remember to bundle!)"
7077
uncomment_lines gemfile_path, /gem "image_processing"/
71-
run "bundle install"
7278
end
7379
end
7480

guides/source/action_text_overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ After the installation is complete, a Rails app using Webpacker should have the
5555

5656
```js
5757
// application.js
58-
require("trix")
59-
require("@rails/actiontext")
58+
import "trix"
59+
import "@rails/actiontext"
6060
```
6161

6262
2. The `trix` stylesheet should be imported into `actiontext.scss`.

guides/source/asset_pipeline.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,12 @@ familiar `javascript_include_tag` and `stylesheet_link_tag`:
317317
```
318318

319319
If using the turbolinks gem, which is included by default in Rails, then
320-
include the 'data-turbolinks-track' option which causes turbolinks to check if
320+
include the 'data-turbo-track' option which causes Turbo to check if
321321
an asset has been updated and if so loads it into the page:
322322

323323
```erb
324-
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => "reload" %>
325-
<%= javascript_include_tag "application", "data-turbolinks-track" => "reload" %>
324+
<%= stylesheet_link_tag "application", media: "all", "data-turbo-track" => "reload" %>
325+
<%= javascript_include_tag "application", "data-turbo-track" => "reload" %>
326326
```
327327

328328
In regular views you can access images in the `app/assets/images` directory

guides/source/getting_started.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ of the files and folders that Rails creates by default:
206206
|Gemfile<br>Gemfile.lock|These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see the [Bundler website](https://bundler.io).|
207207
|lib/|Extended modules for your application.|
208208
|log/|Application log files.|
209-
|package.json|This file allows you to specify what npm dependencies are needed for your Rails application. This file is used by Yarn. For more information about Yarn, see the [Yarn website](https://yarnpkg.com/lang/en/).|
210209
|public/|Contains static files and compiled assets. When your app is running, this directory will be exposed as-is.|
211210
|Rakefile|This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing `Rakefile`, you should add your own tasks by adding files to the `lib/tasks` directory of your application.|
212211
|README.md|This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on.|

guides/source/upgrading_ruby_on_rails.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Repeat this process until you reach your target Rails version.
4444
To move between versions:
4545

4646
1. Change the Rails version number in the `Gemfile` and run `bundle update`.
47-
2. Change the versions for Rails JavaScript packages in `package.json` and run `yarn install`.
47+
2. Change the versions for Rails JavaScript packages in `package.json` and run `yarn install`, if running on Webpacker.
4848
3. Run the [Update task](#the-update-task).
4949
4. Run your tests.
5050

0 commit comments

Comments
 (0)