Class: Shakapacker::DevServer
- Inherits:
-
Object
- Object
- Shakapacker::DevServer
- Defined in:
- lib/shakapacker/dev_server.rb
Overview
Development server status and configuration
Provides methods to query the status and configuration of the webpack-dev-server or rspack-dev-server. This includes checking if the server is running, accessing its host/port, and querying features like HMR (Hot Module Replacement).
The dev server runs during development to provide live reloading and hot module replacement. In production, the dev server is not used.
Constant Summary collapse
- DEFAULT_ENV_PREFIX =
Default environment variable prefix for dev server settings
"SHAKAPACKER_DEV_SERVER".freeze
Instance Attribute Summary collapse
-
#config ⇒ Shakapacker::Configuration
readonly
The Shakapacker configuration.
Instance Method Summary collapse
-
#connect_timeout ⇒ Float
Configure dev server connection timeout (in seconds), default: 0.1.
-
#env_prefix ⇒ String
Returns the environment variable prefix for dev server settings.
-
#hmr? ⇒ Boolean
Returns whether Hot Module Replacement (HMR) is enabled.
-
#host ⇒ String
Returns the dev server host.
-
#host_with_port ⇒ String
Returns the host and port as a single string.
-
#initialize(config) ⇒ Shakapacker::DevServer
constructor
Creates a new dev server instance.
-
#inline_css? ⇒ Boolean
Returns whether CSS inlining is enabled.
-
#port ⇒ Integer
Returns the dev server port.
-
#pretty? ⇒ Boolean
Returns whether pretty output is enabled.
-
#protocol ⇒ String
Returns the protocol for the dev server.
-
#running? ⇒ Boolean
Returns whether the dev server is currently running.
-
#server ⇒ String
Returns the server type (http or https).
Constructor Details
#initialize(config) ⇒ Shakapacker::DevServer
Creates a new dev server instance
38 39 40 |
# File 'lib/shakapacker/dev_server.rb', line 38 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Shakapacker::Configuration (readonly)
The Shakapacker configuration
32 33 34 |
# File 'lib/shakapacker/dev_server.rb', line 32 def config @config end |
Instance Method Details
#connect_timeout ⇒ Float
Configure dev server connection timeout (in seconds), default: 0.1
28 |
# File 'lib/shakapacker/dev_server.rb', line 28 cattr_accessor(:connect_timeout) { 0.1 } |
#env_prefix ⇒ String
Returns the environment variable prefix for dev server settings
Environment variables for dev server settings use this prefix (default: “SHAKAPACKER_DEV_SERVER”). For example, SHAKAPACKER_DEV_SERVER_PORT sets the port.
166 167 168 |
# File 'lib/shakapacker/dev_server.rb', line 166 def env_prefix config.dev_server.fetch(:env_prefix, DEFAULT_ENV_PREFIX) end |
#hmr? ⇒ Boolean
Returns whether Hot Module Replacement (HMR) is enabled
When true, the dev server updates modules in the browser without a full page reload, preserving application state. Can be overridden via SHAKAPACKER_DEV_SERVER_HMR environment variable.
140 141 142 |
# File 'lib/shakapacker/dev_server.rb', line 140 def hmr? fetch(:hmr) end |
#host ⇒ String
Returns the dev server host
Can be overridden via SHAKAPACKER_DEV_SERVER_HOST environment variable.
64 65 66 |
# File 'lib/shakapacker/dev_server.rb', line 64 def host fetch(:host) end |
#host_with_port ⇒ String
Returns the host and port as a single string
119 120 121 |
# File 'lib/shakapacker/dev_server.rb', line 119 def host_with_port "#{host}:#{port}" end |
#inline_css? ⇒ Boolean
Returns whether CSS inlining is enabled
When true, CSS is injected inline via JavaScript instead of being loaded as separate stylesheet files. This enables HMR for CSS. Can be overridden via SHAKAPACKER_DEV_SERVER_INLINE_CSS environment variable.
151 152 153 154 155 156 157 158 |
# File 'lib/shakapacker/dev_server.rb', line 151 def inline_css? case fetch(:inline_css) when false, "false" false else true end end |
#port ⇒ Integer
Returns the dev server port
Can be overridden via SHAKAPACKER_DEV_SERVER_PORT environment variable.
73 74 75 |
# File 'lib/shakapacker/dev_server.rb', line 73 def port fetch(:port) end |
#pretty? ⇒ Boolean
Returns whether pretty output is enabled
When true, the dev server produces prettier, more readable output. Can be overridden via SHAKAPACKER_DEV_SERVER_PRETTY environment variable.
129 130 131 |
# File 'lib/shakapacker/dev_server.rb', line 129 def pretty? fetch(:pretty) end |
#protocol ⇒ String
Returns the protocol for the dev server
This is an alias that returns “https” if server is “https”, otherwise “http”.
107 108 109 110 111 |
# File 'lib/shakapacker/dev_server.rb', line 107 def protocol return "https" if server == "https" "http" end |
#running? ⇒ Boolean
Returns whether the dev server is currently running
Checks by attempting to open a TCP connection to the configured host and port. Returns false if the connection fails or if dev server is not configured.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/shakapacker/dev_server.rb', line 48 def running? if config.dev_server.present? Socket.tcp(host, port, connect_timeout: connect_timeout).close true else false end rescue false end |
#server ⇒ String
Returns the server type (http or https)
Can be overridden via SHAKAPACKER_DEV_SERVER_SERVER environment variable. Validates that the value is “http” or “https”, falling back to “http” if invalid.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/shakapacker/dev_server.rb', line 83 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 KeyError "http" end |