Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow loading webpack.config.ts if present #524

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading