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

Update webpack dev server configuration #276

Merged
merged 14 commits into from
Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ _Please add entries here for your pull requests that are not yet released._

This change might be breaking for certain setups and edge cases. More information: [v7 Upgrade Guide](./docs/v7_upgrade.md) [PR157](https://github.com/shakacode/shakapacker/pull/157) by [ahangarha](https://github.com/ahangarha)

- Dev server configuration is modified to follow [webpack recommended configurations](https://webpack.js.org/configuration/dev-server/) for dev server. [PR276](https://github.com/shakacode/shakapacker/pull/276) by [ahangarha](https://github.com/ahangarha):
- Deprecated `https` entry is removed from the default configuration file, allowing to set `server` or `https` as per the project requirements. For more detail, check Webpack documentation. The `https` entry can be effective only if there is no `server` entry in the config file.
ahangarha marked this conversation as resolved.
Show resolved Hide resolved
- `allowed_hosts` is now set to `auto` instead of `all` by default.

### Removed
- Remove redundant enhancement for precompile task to run `yarn install` [PR 270](https://github.com/shakacode/shakapacker/pull/270) by [ahangarha](https://github.com/ahangarha).

### Added
- All standard Webpack entries with the camle-case format are now supported in `shakapacker.yml` in snake-case format. [PR276](https://github.com/shakacode/shakapacker/pull/276) by [ahangarha](https://github.com/ahangarha).
ahangarha marked this conversation as resolved.
Show resolved Hide resolved

## [v6.6.0] - March 7, 2023
### Improved
- Allow configuration of webpacker.yml through env variable. [PR 254](https://github.com/shakacode/shakapacker/pull/254) by [alecslupu](https://github.com/alecslupu).
Expand Down
8 changes: 6 additions & 2 deletions lib/install/config/shakapacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ development:
# Reference: https://webpack.js.org/configuration/dev-server/
# Keys not described there are documented inline and in https://github.com/shakacode/shakapacker/
dev_server:
https: false
# For running dev server with https, set `server: https`.
# You may use `https: true` instead but notice that it is deprecated in favor of `server: https`
# Ensure only one of these entries is set.
# server: https

host: localhost
port: 3035
# Hot Module Replacement updates modules while the application is running without a full reload
Expand All @@ -78,7 +82,7 @@ development:
# Should we use gzip compression?
compress: true
# Note that apps that do not check the host are vulnerable to DNS rebinding attacks
allowed_hosts: 'all'
allowed_hosts: 'auto'
# Shows progress and colorizes output of bin/shakapacker[-dev-server]
pretty: true
headers:
Expand Down
25 changes: 24 additions & 1 deletion lib/shakapacker/dev_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,29 @@ def https?
end
end

def server
server_value = fetch(:server)
server_type = server_value.is_a?(Hash) ? server_value[:type] : server_value

return server_type if ["http", "https"].include?(server_type)

return "http" if server_type.nil?

puts <<~MSG
WARNING:
`server: #{server_type}` is not a valid configuration in Shakapacker.
Falling back to default `server: http`.
MSG

"http"
rescue
ahangarha marked this conversation as resolved.
Show resolved Hide resolved
"http"
end

def protocol
https? ? "https" : "http"
return "https" if server == "https" || https? == true

"http"
end

def host_with_port
Expand Down Expand Up @@ -73,6 +94,8 @@ def fetch(key)
return nil unless config.dev_server.present?

ENV["#{env_prefix}_#{key.upcase}"] || config.dev_server.fetch(key, defaults[key])
rescue
nil
end

def defaults
Expand Down
2 changes: 1 addition & 1 deletion lib/shakapacker/dev_server_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def perform_request(env)
env["HTTP_X_FORWARDED_SERVER"] = dev_server.host_with_port
env["HTTP_PORT"] = env["HTTP_X_FORWARDED_PORT"] = dev_server.port.to_s
env["HTTP_X_FORWARDED_PROTO"] = env["HTTP_X_FORWARDED_SCHEME"] = dev_server.protocol
unless dev_server.https?
unless dev_server.protocol == "https"
env["HTTPS"] = env["HTTP_X_FORWARDED_SSL"] = "off"
end
env["SCRIPT_NAME"] = ""
Expand Down
2 changes: 1 addition & 1 deletion lib/shakapacker/dev_server_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def load_config
@hostname = dev_server.host
@port = dev_server.port
@pretty = dev_server.pretty?
@https = dev_server.https?
@https = dev_server.protocol == "https"
@hot = dev_server.hmr?

rescue Errno::ENOENT, NoMethodError
Expand Down
24 changes: 23 additions & 1 deletion package/environments/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if (runningWebpackDevServer) {
allowedHosts: devServer.allowed_hosts,
host: devServer.host,
port: devServer.port,
https: devServer.https,
server: devServer.server,
ahangarha marked this conversation as resolved.
Show resolved Hide resolved
hot: devServer.hmr,
liveReload,
historyApiFallback: { disableDotRule: true },
Expand All @@ -40,6 +40,28 @@ if (runningWebpackDevServer) {
devServerConfig.client = devServer.client
}

// If we have `server` entry, we ignore possible `https` one.
if (devServer.server) {
devServerConfig.server = devServer.server
} else if (devServer.https) {
ahangarha marked this conversation as resolved.
Show resolved Hide resolved
devServerConfig.https = devServer.https
}

const jsToRbKeyPairs = [
['magicHtml', 'magic_html'],
ahangarha marked this conversation as resolved.
Show resolved Hide resolved
['onAfterSetupMiddleware', 'on_after_setup_middleware'],
['onBeforeSetupMiddleware', 'on_before_setup_middleware'],
['onListening', 'on_listening'],
['setupExitSignals', 'setup_exit_signals'],
['setupMiddlewares', 'setup_middlewares'],
['watchFiles', 'watch_files'],
['webSocketServer', 'web_socket_server']
]

jsToRbKeyPairs.forEach(([jsKey, rbKey]) => {
if (devServer[rbKey]) devServerConfig[jsKey] = devServer[rbKey]
})

devConfig = merge(devConfig, {
stats: {
colors: true,
Expand Down
86 changes: 86 additions & 0 deletions spec/dev_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,90 @@
it "users SHAKAPACKER_DEV_SERVER for DEFAULT_ENV_PREFIX" do
expect(Shakapacker::DevServer::DEFAULT_ENV_PREFIX).to eq "SHAKAPACKER_DEV_SERVER"
end

context "#protocol in development environment" do
let(:dev_server) { Shakapacker.dev_server }

it "returns `http` by default (with unset `server` and `https`)" do
with_rails_env("development") do
expect(dev_server.protocol).to eq "http"
end
end

it "returns `https` when `server` is set to `https`" do
expect(dev_server).to receive(:server).and_return("https")

with_rails_env("development") do
expect(dev_server.protocol).to eq "https"
end
end

it "returns `https` with unset `server` and `https` set to `true`" do
expect(dev_server).to receive(:server).and_return("http")
expect(dev_server).to receive(:https?).and_return(true)

with_rails_env("development") do
expect(dev_server.protocol).to eq "https"
end
end
end

context "#server in development environment" do
let(:dev_server) { Shakapacker.dev_server }

it "returns `http` when unset" do
expect(dev_server).to receive(:fetch).with(:server).and_return(nil)

with_rails_env("development") do
expect(dev_server.server).to eq "http"
end
end

it "returns `http` when set to `https`" do
expect(dev_server).to receive(:fetch).with(:server).and_return("http")

with_rails_env("development") do
expect(dev_server.server).to eq "http"
end
end

it "returns `http` when set to a hash with `type: http`" do
expect(dev_server).to receive(:fetch).with(:server).and_return({
type: "http",
options: {}
})

with_rails_env("development") do
expect(dev_server.server).to eq "http"
end
end

it "returns `https` when set to `https`" do
expect(dev_server).to receive(:fetch).with(:server).and_return("https")

with_rails_env("development") do
expect(dev_server.server).to eq "https"
end
end

it "returns `https` when set to a hash with `type: https`" do
expect(dev_server).to receive(:fetch).with(:server).and_return({
type: "https",
options: {}
})

with_rails_env("development") do
expect(dev_server.server).to eq "https"
end
end

it "returns `http` when set to any value except `http` and `https`" do
expect(dev_server).to receive(:fetch).twice.with(:server).and_return("other-than-https")

with_rails_env("development") do
expect(dev_server.server).to eq "http"
expect { dev_server.server }.to output(/WARNING/).to_stdout
end
end
end
end
1 change: 0 additions & 1 deletion spec/test_app/config/shakapacker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ development:

# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: localhost
port: 3035
public: localhost:3035
Expand Down