Class: Shakapacker::DevServer
- Inherits:
-
Object
- Object
- Shakapacker::DevServer
- Defined in:
- lib/shakapacker/dev_server.rb,
sig/shakapacker/dev_server.rbs
Overview
Development server status and configuration
Provides methods to query the status and configuration of the webpack-dev-server or rspack-dev-server.
Constant Summary collapse
- DEFAULT_ENV_PREFIX =
Default environment variable prefix for dev server settings
"SHAKAPACKER_DEV_SERVER".freeze
Instance Attribute Summary collapse
-
#config ⇒ Configuration
readonly
The Shakapacker configuration.
Class Method Summary collapse
-
.connect_timeout ⇒ Float
Configure dev server connection timeout (in seconds).
-
.connect_timeout= ⇒ Float
Sets the connection timeout.
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.
- #fetch(key) ⇒ Object
-
#hmr? ⇒ Boolean
Returns whether Hot Module Replacement (HMR) is enabled.
-
#host ⇒ String
Returns the dev server host.
-
#host_with_port ⇒ String
Returns the host with port (e.g., "localhost:3035").
-
#initialize(config) ⇒ DevServer
constructor
Creates a new dev server instance.
-
#inline_css? ⇒ Boolean
Returns whether CSS should be inlined by the dev server.
-
#port ⇒ Integer
Returns the dev server port.
-
#pretty? ⇒ Boolean
Returns whether pretty output is enabled.
-
#protocol ⇒ String
Alias for server.
-
#running? ⇒ Boolean
Returns whether the dev server is currently running.
-
#server ⇒ String
Returns the server type (http or https).
Constructor Details
#initialize(config) ⇒ 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 ⇒ Configuration (readonly)
The Shakapacker configuration
32 33 34 |
# File 'lib/shakapacker/dev_server.rb', line 32 def config @config end |
Class Method Details
.connect_timeout ⇒ Float
Configure dev server connection timeout (in seconds)
10 |
# File 'sig/shakapacker/dev_server.rbs', line 10
def self.connect_timeout: () -> Float
|
.connect_timeout= ⇒ Float
Sets the connection timeout
13 |
# File 'sig/shakapacker/dev_server.rbs', line 13
def self.connect_timeout=: (Float | Integer timeout) -> Float
|
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
166 167 168 |
# File 'lib/shakapacker/dev_server.rb', line 166 def env_prefix config.dev_server.fetch(:env_prefix, DEFAULT_ENV_PREFIX) end |
#fetch(key) ⇒ Object
171 172 173 174 175 176 177 |
# File 'lib/shakapacker/dev_server.rb', line 171 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 |
#hmr? ⇒ Boolean
Returns whether Hot Module Replacement (HMR) is enabled
140 141 142 |
# File 'lib/shakapacker/dev_server.rb', line 140 def hmr? fetch(:hmr) end |
#host ⇒ String
Returns the dev server host
64 65 66 |
# File 'lib/shakapacker/dev_server.rb', line 64 def host fetch(:host) end |
#host_with_port ⇒ String
Returns the host with port (e.g., "localhost:3035")
119 120 121 |
# File 'lib/shakapacker/dev_server.rb', line 119 def host_with_port "#{host}:#{port}" end |
#inline_css? ⇒ Boolean
Returns whether CSS should be inlined by the dev server
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
73 74 75 |
# File 'lib/shakapacker/dev_server.rb', line 73 def port fetch(:port) end |
#pretty? ⇒ Boolean
Returns whether pretty output is enabled
129 130 131 |
# File 'lib/shakapacker/dev_server.rb', line 129 def pretty? fetch(:pretty) end |
#protocol ⇒ String
Alias for server
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
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)
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 |