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..2bff5361 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,18 @@ 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