Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/react_on_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -429,18 +429,23 @@ def configure_generated_assets_dirs_deprecation
end

def ensure_webpack_generated_files_exists
return unless webpack_generated_files.empty?

files = ["manifest.json", server_bundle_js_file]
all_required_files = ["manifest.json", server_bundle_js_file]

if ReactOnRails::Utils.react_on_rails_pro?
pro_config = ReactOnRailsPro.configuration
files << pro_config.rsc_bundle_js_file
files << pro_config.react_client_manifest_file
files << pro_config.react_server_client_manifest_file
all_required_files << pro_config.rsc_bundle_js_file
all_required_files << pro_config.react_client_manifest_file
all_required_files << pro_config.react_server_client_manifest_file
end

self.webpack_generated_files = files.compact_blank
all_required_files = all_required_files.compact_blank

if webpack_generated_files.empty?
self.webpack_generated_files = all_required_files
else
missing_files = all_required_files.reject { |file| webpack_generated_files.include?(file) }
self.webpack_generated_files += missing_files if missing_files.any?
end
end

def configure_skip_display_none_deprecation
Expand Down
122 changes: 122 additions & 0 deletions spec/react_on_rails/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,17 @@ module ReactOnRails
context "with Pro license" do
before do
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(true)

# Mock Pro configuration to avoid dependency on Pro being installed
pro_module = Module.new
pro_config = double("ProConfiguration") # rubocop:disable RSpec/VerifiedDoubles
allow(pro_config).to receive_messages(
rsc_bundle_js_file: "",
react_client_manifest_file: "react-client-manifest.json",
react_server_client_manifest_file: "react-server-client-manifest.json"
)
pro_module.define_singleton_method(:configuration) { pro_config }
stub_const("ReactOnRailsPro", pro_module)
end

it "defaults to :async" do
Expand Down Expand Up @@ -626,6 +637,117 @@ module ReactOnRails
end
end
end

describe "#ensure_webpack_generated_files_exists" do
let(:config) { described_class.new }

before do
# Reset to test defaults
config.server_bundle_js_file = "server-bundle.js"
allow(ReactOnRails::Utils).to receive(:react_on_rails_pro?).and_return(false)
end

context "when webpack_generated_files has default manifest.json only" do
it "automatically includes server bundle when configured" do
config.webpack_generated_files = %w[manifest.json]

config.send(:ensure_webpack_generated_files_exists)

expect(config.webpack_generated_files).to eq(%w[manifest.json server-bundle.js])
end

it "does not duplicate manifest.json" do
config.webpack_generated_files = %w[manifest.json]

config.send(:ensure_webpack_generated_files_exists)

expect(config.webpack_generated_files.count("manifest.json")).to eq(1)
end
end

context "when webpack_generated_files is empty" do
it "populates with all required files" do
config.webpack_generated_files = []

config.send(:ensure_webpack_generated_files_exists)

expect(config.webpack_generated_files).to eq(%w[manifest.json server-bundle.js])
end
end

context "when server bundle already included" do
it "does not duplicate entries" do
config.webpack_generated_files = %w[manifest.json server-bundle.js]

config.send(:ensure_webpack_generated_files_exists)

expect(config.webpack_generated_files).to eq(%w[manifest.json server-bundle.js])
expect(config.webpack_generated_files.count("server-bundle.js")).to eq(1)
end
end

context "when custom files are configured" do
it "preserves custom files and adds missing critical files" do
config.webpack_generated_files = %w[manifest.json custom-bundle.js]

config.send(:ensure_webpack_generated_files_exists)

expect(config.webpack_generated_files).to include("manifest.json")
expect(config.webpack_generated_files).to include("custom-bundle.js")
expect(config.webpack_generated_files).to include("server-bundle.js")
end
end

context "when server bundle is not configured" do
it "does not add empty server bundle" do
config.server_bundle_js_file = ""
config.webpack_generated_files = %w[manifest.json]

config.send(:ensure_webpack_generated_files_exists)

expect(config.webpack_generated_files).not_to include("")
expect(config.webpack_generated_files).to eq(%w[manifest.json])
end

it "does not add nil server bundle" do
config.server_bundle_js_file = nil
config.webpack_generated_files = %w[manifest.json]

config.send(:ensure_webpack_generated_files_exists)

expect(config.webpack_generated_files).not_to include(nil)
expect(config.webpack_generated_files).to eq(%w[manifest.json])
end
end

context "when ensuring server bundle monitoring for RSpec optimization" do
it "ensures server bundle in private directory is monitored with default config" do
# Simulate default generator configuration
config.webpack_generated_files = %w[manifest.json]
config.server_bundle_js_file = "server-bundle.js"
config.server_bundle_output_path = "ssr-generated"

config.send(:ensure_webpack_generated_files_exists)

# Critical: server bundle must be included for RSpec helper optimization to work
expect(config.webpack_generated_files).to include("server-bundle.js")
end

it "handles all files being in different directories" do
# Simulate cross-directory scenario
config.webpack_generated_files = %w[manifest.json]
config.server_bundle_js_file = "server-bundle.js"
config.server_bundle_output_path = "ssr-generated"
config.generated_assets_dir = "public/packs"

config.send(:ensure_webpack_generated_files_exists)

# All critical files should be monitored regardless of directory
expect(config.webpack_generated_files).to include("manifest.json")
expect(config.webpack_generated_files).to include("server-bundle.js")
end
end
end
end
end

Expand Down
Loading