From 3d1dbac8984f1bf10e1f9f75c69d5462bd1839eb Mon Sep 17 00:00:00 2001 From: Jonathan del Strother Date: Tue, 29 Oct 2024 13:46:13 +0000 Subject: [PATCH] Allow loading webpack.config.ts if present --- lib/shakapacker/runner.rb | 21 +++++++++++++++------ spec/shakapacker/webpack_runner_spec.rb | 11 +++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/lib/shakapacker/runner.rb b/lib/shakapacker/runner.rb index 27785a18..7edfc912 100644 --- a/lib/shakapacker/runner.rb +++ b/lib/shakapacker/runner.rb @@ -15,13 +15,8 @@ def initialize(argv) @argv = argv @app_path = File.expand_path(".", Dir.pwd) - @webpack_config = File.join(@app_path, "config/webpack/webpack.config.js") @shakapacker_config = ENV["SHAKAPACKER_CONFIG"] || File.join(@app_path, "config/shakapacker.yml") - - unless File.exist?(@webpack_config) - $stderr.puts "webpack config #{@webpack_config} not found, please run 'bundle exec rails shakapacker:install' to install Shakapacker with default configs or add the missing config file for your custom environment." - exit! - end + @webpack_config = find_webpack_config Shakapacker::Utils::Manager.error_unless_package_manager_is_obvious! end @@ -29,5 +24,19 @@ def initialize(argv) def package_json @package_json ||= PackageJson.read(@app_path) end + + private + + def find_webpack_config + possible_paths = %w[ts js].map do |ext| + File.join(@app_path, "config/webpack/webpack.config.#{ext}") + end + path = possible_paths.select { |f| File.exist?(f) }.first + unless path + $stderr.puts "webpack config #{possible_paths.last} not found, please run 'bundle exec rails shakapacker:install' to install Shakapacker with default configs or add the missing config file for your custom environment." + exit! + end + path + end end end diff --git a/spec/shakapacker/webpack_runner_spec.rb b/spec/shakapacker/webpack_runner_spec.rb index 09f79f9c..a9db479a 100644 --- a/spec/shakapacker/webpack_runner_spec.rb +++ b/spec/shakapacker/webpack_runner_spec.rb @@ -54,6 +54,17 @@ verify_command(cmd, argv: (["--watch"])) end + + it "loads webpack.config.ts if present" do + ts_config = "#{test_app_path}/config/webpack/webpack.config.ts" + FileUtils.touch(ts_config) + + cmd = package_json.manager.native_exec_command("webpack", ["--config", ts_config]) + + verify_command(cmd) + ensure + FileUtils.rm(ts_config) + end end end