Skip to content

Commit ccc4706

Browse files
committed
Allow loading webpack.config.ts if present
1 parent a59be7d commit ccc4706

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Changes since the last non-beta release.
1212

1313
### Changed
1414
- 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).
15+
- 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).
1516

1617
### Fixed
1718
- Fix error when rails environment is required from outside the rails root directory [PR 520](https://github.com/shakacode/shakapacker/pull/520)

lib/shakapacker/runner.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,27 @@ def initialize(argv)
1515
@argv = argv
1616

1717
@app_path = File.expand_path(".", Dir.pwd)
18-
@webpack_config = File.join(@app_path, "config/webpack/webpack.config.js")
1918
@shakapacker_config = ENV["SHAKAPACKER_CONFIG"] || File.join(@app_path, "config/shakapacker.yml")
20-
21-
unless File.exist?(@webpack_config)
22-
$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."
23-
exit!
24-
end
19+
@webpack_config = find_webpack_config
2520

2621
Shakapacker::Utils::Manager.error_unless_package_manager_is_obvious!
2722
end
2823

2924
def package_json
3025
@package_json ||= PackageJson.read(@app_path)
3126
end
27+
28+
private
29+
def find_webpack_config
30+
possible_paths = %w[ts js].map do |ext|
31+
File.join(@app_path, "config/webpack/webpack.config.#{ext}")
32+
end
33+
path = possible_paths.find { |f| File.exist?(f) }
34+
unless path
35+
$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."
36+
exit!
37+
end
38+
path
39+
end
3240
end
3341
end

spec/shakapacker/webpack_runner_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@
5454

5555
verify_command(cmd, argv: (["--watch"]))
5656
end
57+
58+
it "loads webpack.config.ts if present" do
59+
ts_config = "#{test_app_path}/config/webpack/webpack.config.ts"
60+
FileUtils.touch(ts_config)
61+
62+
cmd = package_json.manager.native_exec_command("webpack", ["--config", ts_config])
63+
64+
verify_command(cmd)
65+
ensure
66+
FileUtils.rm(ts_config)
67+
end
5768
end
5869
end
5970

0 commit comments

Comments
 (0)