Skip to content

Commit 6f8ecbc

Browse files
committed
Fix channel generator import statement:
- Fix rails#54241 - ### Problem The `import` statement generated is incorrect for Rails applications using importmap and a css processor. ### Context Rails applications using `importmap` and having a `package.json`, would get a `channel.js` file generated with a relative path, whereas the correct import statement needs to match the name of the importmap json keys. This is because the generator assumes that if a `package.json` exists in the project, this means it uses a javascript bundling tool. This assumption is not correct as one can be using importmap while still having `package.json` for processing css. This is the case when you generate your application like this: `rails new blorgh --css postcss` ### Solution Add the right import statement by checking if the project uses importmap.
1 parent 0f9de98 commit 6f8ecbc

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ def create_shared_channel_javascript_files
5959
def create_channel_javascript_file
6060
channel_js_path = File.join("app/javascript/channels", class_path, "#{file_name}_channel")
6161
js_template "javascript/channel", channel_js_path
62-
gsub_file "#{channel_js_path}.js", /\.\/consumer/, "channels/consumer" unless using_js_runtime?
62+
gsub_file "#{channel_js_path}.js", /\.\/consumer/, "channels/consumer" if using_importmap?
6363
end
6464

6565
def import_channels_in_javascript_entrypoint
6666
append_to_file "app/javascript/application.js",
67-
using_js_runtime? ? %(import "./channels"\n) : %(import "channels"\n)
67+
using_importmap? ? %(import "channels"\n) : %(import "./channels"\n)
6868
end
6969

7070
def import_channel_in_javascript_entrypoint
7171
append_to_file "app/javascript/channels/index.js",
72-
using_js_runtime? ? %(import "./#{file_name}_channel"\n) : %(import "channels/#{file_name}_channel"\n)
72+
using_importmap? ? %(import "channels/#{file_name}_channel"\n) : %(import "./#{file_name}_channel"\n)
7373
end
7474

7575
def install_javascript_dependencies

railties/test/generators/channel_generator_test.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ class ChannelGeneratorTest < Rails::Generators::TestCase
7878
end
7979
end
8080

81+
test "import channels in javascript entrypoint when using a css processor" do
82+
FileUtils.touch("#{destination_root}/package.json")
83+
84+
run_generator ["books"]
85+
86+
assert_file "app/javascript/application.js" do |entrypoint|
87+
assert_match %r|import "channels"|, entrypoint
88+
end
89+
end
90+
8191
test "import channels in javascript entrypoint under node" do
8292
use_under_node
8393
generator(["chat"]).stub(:install_javascript_dependencies, true) do

0 commit comments

Comments
 (0)