Skip to content

Commit

Permalink
Allow loading webpack.config.ts if present (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdelStrother authored Oct 31, 2024
1 parent a59be7d commit f829152
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,24 @@ module.exports = generateWebpackConfig({
});
```

Optionally, your webpack config file itself can be written in Typescript:

``` bash
npm install ts-node @types/node @types/webpack
```

```ts
// config/webpack/webpack.config.ts
import { generateWebpackConfig } from "shakapacker";
import ForkTSCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";

const config = generateWebpackConfig({
plugins: [new ForkTSCheckerWebpackPlugin()],
});

export default config;
```

#### CSS

To enable CSS support in your application, add the following packages:
Expand Down
20 changes: 14 additions & 6 deletions lib/shakapacker/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,27 @@ 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

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
11 changes: 11 additions & 0 deletions spec/shakapacker/webpack_runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit f829152

Please sign in to comment.