Skip to content

Commit 39dc680

Browse files
committed
Avoid undefined RSC client references
1 parent 184460a commit 39dc680

2 files changed

Lines changed: 84 additions & 2 deletions

File tree

react_on_rails/lib/generators/react_on_rails/rsc_setup.rb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ def check_rsc_server_config
489489
missing = []
490490
missing << "RSCWebpackPlugin in serverWebpackConfig.js" unless content.include?("RSCWebpackPlugin")
491491
missing << "rscBundle parameter in serverWebpackConfig.js" unless content.include?("rscBundle")
492+
missing.concat(check_rsc_client_references(content, "serverWebpackConfig.js"))
492493
missing
493494
end
494495

@@ -497,7 +498,10 @@ def check_rsc_client_config
497498
return [] unless File.exist?(path)
498499

499500
content = File.read(path)
500-
content.include?("RSCWebpackPlugin") ? [] : ["RSCWebpackPlugin in clientWebpackConfig.js"]
501+
missing = []
502+
missing << "RSCWebpackPlugin in clientWebpackConfig.js" unless content.include?("RSCWebpackPlugin")
503+
missing.concat(check_rsc_client_references(content, "clientWebpackConfig.js"))
504+
missing
501505
end
502506

503507
def check_rsc_scob_config
@@ -521,7 +525,7 @@ def rsc_client_references_js
521525
def update_existing_rsc_webpack_config(config_path, content, is_server:)
522526
return if content.include?("clientReferences: rscClientReferences")
523527

524-
add_rsc_client_references_setup(config_path, content, is_server: is_server)
528+
return unless add_rsc_client_references_setup(config_path, content, is_server: is_server)
525529

526530
gsub_file(
527531
config_path,
@@ -549,7 +553,23 @@ def add_rsc_client_references_setup(config_path, content, is_server:)
549553
%r{(const commonWebpackConfig = require\('\./commonWebpackConfig'\);)}
550554
end
551555

556+
return false unless content.match?(import_pattern)
557+
552558
gsub_file(config_path, import_pattern, injected_imports)
559+
true
560+
end
561+
562+
def check_rsc_client_references(content, filename)
563+
return [] unless content.include?("RSCWebpackPlugin")
564+
565+
missing = []
566+
unless content.include?("clientReferences: rscClientReferences")
567+
missing << "clientReferences option in #{filename}"
568+
end
569+
if content.include?("clientReferences: rscClientReferences") && !content.include?("const rscClientReferences =")
570+
missing << "rscClientReferences definition in #{filename}"
571+
end
572+
missing
553573
end
554574

555575
def shakapacker_config_imported?(content)

react_on_rails/spec/react_on_rails/generators/rsc_generator_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,68 @@
303303
end
304304
end
305305

306+
context "when existing RSC webpack configs have customized import anchors" do
307+
before(:all) do
308+
prepare_destination
309+
simulate_existing_rails_files(package_json: true)
310+
simulate_npm_files(package_json: true)
311+
simulate_existing_file("config/initializers/react_on_rails_pro.rb", <<~RUBY)
312+
ReactOnRailsPro.configure do |config|
313+
config.server_renderer = "NodeRenderer"
314+
end
315+
RUBY
316+
simulate_existing_file("Procfile.dev", "rails: bin/rails s\n")
317+
simulate_pro_webpack_files
318+
simulate_existing_file("config/webpack/serverWebpackConfig.js", <<~JS)
319+
const { RSCWebpackPlugin } = require('react-on-rails-rsc/WebpackPlugin');
320+
const webpack = require('webpack');
321+
322+
const configureServer = (rscBundle = false) => {
323+
const serverWebpackConfig = { plugins: [] };
324+
325+
if (!rscBundle) {
326+
serverWebpackConfig.plugins.push(new RSCWebpackPlugin({ isServer: true }));
327+
}
328+
329+
return serverWebpackConfig;
330+
};
331+
332+
module.exports = { default: configureServer };
333+
JS
334+
simulate_existing_file("config/webpack/clientWebpackConfig.js", <<~JS)
335+
const { RSCWebpackPlugin } = require('react-on-rails-rsc/WebpackPlugin');
336+
337+
const configureClient = () => {
338+
const clientConfig = { plugins: [] };
339+
clientConfig.plugins.push(new RSCWebpackPlugin({ isServer: false }));
340+
341+
return clientConfig;
342+
};
343+
344+
module.exports = configureClient;
345+
JS
346+
347+
Dir.chdir(destination_root) do
348+
run_generator(["--force"])
349+
end
350+
end
351+
352+
it "does not add undefined rscClientReferences references" do
353+
assert_file "config/webpack/serverWebpackConfig.js" do |content|
354+
expect(content).not_to include("clientReferences: rscClientReferences")
355+
expect(content).not_to include("const rscClientReferences =")
356+
end
357+
assert_file "config/webpack/clientWebpackConfig.js" do |content|
358+
expect(content).not_to include("clientReferences: rscClientReferences")
359+
expect(content).not_to include("const rscClientReferences =")
360+
end
361+
362+
warning_text = GeneratorMessages.messages.join("\n")
363+
expect(warning_text).to include("clientReferences option in serverWebpackConfig.js")
364+
expect(warning_text).to include("clientReferences option in clientWebpackConfig.js")
365+
end
366+
end
367+
306368
context "when the client webpack config uses aliased imports" do
307369
before(:all) do
308370
prepare_destination

0 commit comments

Comments
 (0)