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
171 172 173 |
# File 'lib/shakapacker/dev_server.rb', line 171 def env_prefix config.dev_server.fetch(:env_prefix, DEFAULT_ENV_PREFIX) end |
#fetch(key) ⇒ Object
176 177 178 179 180 181 182 |
# File 'lib/shakapacker/dev_server.rb', line 176 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
145 146 147 |
# File 'lib/shakapacker/dev_server.rb', line 145 def hmr? fetch(:hmr) end |
#host ⇒ String
Returns the dev server host
69 70 71 |
# File 'lib/shakapacker/dev_server.rb', line 69 def host fetch(:host) end |
#host_with_port ⇒ String
Returns the host with port (e.g., "localhost:3035")
124 125 126 |
# File 'lib/shakapacker/dev_server.rb', line 124 def host_with_port "#{host}:#{port}" end |
#inline_css? ⇒ Boolean
Returns whether CSS should be inlined by the dev server
156 157 158 159 160 161 162 163 |
# File 'lib/shakapacker/dev_server.rb', line 156 def inline_css? case fetch(:inline_css) when false, "false" false else true end end |
#port ⇒ Integer
Returns the dev server port
78 79 80 |
# File 'lib/shakapacker/dev_server.rb', line 78 def port fetch(:port) end |
#pretty? ⇒ Boolean
Returns whether pretty output is enabled
134 135 136 |
# File 'lib/shakapacker/dev_server.rb', line 134 def pretty? fetch(:pretty) end |
#protocol ⇒ String
Alias for server
112 113 114 115 116 |
# File 'lib/shakapacker/dev_server.rb', line 112 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 58 59 60 61 62 |
# File 'lib/shakapacker/dev_server.rb', line 48 def running? if config.dev_server.present? socket = Socket.tcp(host, port, connect_timeout: connect_timeout) begin socket.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR).int.zero? ensure socket.close end else false end rescue false end |
#server ⇒ String
Returns the server type (http or https)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/shakapacker/dev_server.rb', line 88 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 |