Class: Shakapacker::DevServer

Inherits:
Object
  • Object
show all
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.

Examples:

Checking dev server status

dev_server = Shakapacker.dev_server
dev_server.running?
#=> true
dev_server.host_with_port
#=> "localhost:3035"
dev_server.hmr?
#=> true

See Also:

Constant Summary collapse

DEFAULT_ENV_PREFIX =

Default environment variable prefix for dev server settings

"SHAKAPACKER_DEV_SERVER".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Shakapacker::DevServer

Creates a new dev server instance

Parameters:



38
39
40
# File 'lib/shakapacker/dev_server.rb', line 38

def initialize(config)
  @config = config
end

Instance Attribute Details

#configShakapacker::Configuration (readonly)

The Shakapacker configuration

Returns:



32
33
34
# File 'lib/shakapacker/dev_server.rb', line 32

def config
  @config
end

Instance Method Details

#connect_timeoutFloat

Configure dev server connection timeout (in seconds), default: 0.1

Examples:

Shakapacker::DevServer.connect_timeout = 1

Returns:

  • (Float)

    the connection timeout in seconds



28
# File 'lib/shakapacker/dev_server.rb', line 28

cattr_accessor(:connect_timeout) { 0.1 }

#env_prefixString

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.

Returns:

  • (String)

    the env var prefix (typically “SHAKAPACKER_DEV_SERVER”)



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.

Returns:

  • (Boolean)

    true if HMR is enabled



140
141
142
# File 'lib/shakapacker/dev_server.rb', line 140

def hmr?
  fetch(:hmr)
end

#hostString

Returns the dev server host

Can be overridden via SHAKAPACKER_DEV_SERVER_HOST environment variable.

Returns:

  • (String)

    the host (e.g., “localhost”, “0.0.0.0”)



64
65
66
# File 'lib/shakapacker/dev_server.rb', line 64

def host
  fetch(:host)
end

#host_with_portString

Returns the host and port as a single string

Examples:

dev_server.host_with_port
#=> "localhost:3035"

Returns:

  • (String)

    the host:port combination (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 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.

Returns:

  • (Boolean)

    true if CSS should be inlined



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

#portInteger

Returns the dev server port

Can be overridden via SHAKAPACKER_DEV_SERVER_PORT environment variable.

Returns:

  • (Integer)

    the port number (typically 3035)



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.

Returns:

  • (Boolean)

    true if pretty output is enabled



129
130
131
# File 'lib/shakapacker/dev_server.rb', line 129

def pretty?
  fetch(:pretty)
end

#protocolString

Returns the protocol for the dev server

This is an alias that returns “https” if server is “https”, otherwise “http”.

Returns:

  • (String)

    “http” or “https”



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.

Returns:

  • (Boolean)

    true if the dev server is 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

#serverString

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.

Returns:

  • (String)

    “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