From 7b3161f00da6ef4a953d8005896b242aead865a4 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 --- CHANGELOG.md | 1 + lib/shakapacker/runner.rb | 21 +++++++++++++++------ spec/shakapacker/webpack_runner_spec.rb | 11 +++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b83e664c..28659ac8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Changes since the last non-beta release. ### Changed - Changed internal `require`s to `require_relative` to make code less dependent on the load path. [PR 516](https://github.com/shakacode/shakapacker/pull/516) by [tagliala](https://github.com/tagliala). +- Allow configuring webpack from a Typescript file (`config/webpack/webpack.config.ts`). [PR 524](https://github.com/shakacode/shakapacker/pull/524) by [jdelStrother](https://github.com/jdelStrother). ### Fixed - Fix error when rails environment is required from outside the rails root directory [PR 520](https://github.com/shakacode/shakapacker/pull/520) diff --git a/lib/shakapacker/runner.rb b/lib/shakapacker/runner.rb index 27785a18..d6ffa879 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.find { |f| File.exist?(f) } + 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