Class: EmberCli::DevServer

Inherits:
Object
  • Object
show all
Defined in:
lib/ember_cli/dev_server.rb

Overview

Manages the Vite development server backing a single Ember application.

The server is booted lazily, and the Ember application's index.html and assets are read from it over HTTP instead of from the dist directory written by ember build.

Constant Summary collapse

DEFAULT_HOST =
"127.0.0.1".freeze
DEFAULT_TIMEOUT =
30
POLL_INTERVAL =
0.1
CONNECT_TIMEOUT =
1

Instance Method Summary collapse

Constructor Details

#initialize(name:, paths:, shell:, options: {}) ⇒ DevServer

Returns a new instance of DevServer.



20
21
22
23
24
25
26
# File 'lib/ember_cli/dev_server.rb', line 20

def initialize(name:, paths:, shell:, options: {})
  @name = name
  @paths = paths
  @shell = shell
  @options = options.respond_to?(:fetch) ? options : {}
  @monitor = Monitor.new
end

Instance Method Details

#get(path, headers = {}) ⇒ Object



81
82
83
# File 'lib/ember_cli/dev_server.rb', line 81

def get(path, headers = {})
  request(Net::HTTP::Get, path, headers)
end

#head(path, headers = {}) ⇒ Object



85
86
87
# File 'lib/ember_cli/dev_server.rb', line 85

def head(path, headers = {})
  request(Net::HTTP::Head, path, headers)
end

#hostObject



28
29
30
# File 'lib/ember_cli/dev_server.rb', line 28

def host
  @host ||= option(:host) { DEFAULT_HOST }.to_s
end

#index_htmlObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ember_cli/dev_server.rb', line 65

def index_html
  response = get("/")

  unless response.is_a?(Net::HTTPSuccess)
    fail BuildError.new(<<~MSG)
      #{name.inspect} failed to serve an `index.html` file.

      #{origin}/ responded with #{response.code} #{response.message}:

      #{response.body}
    MSG
  end

  response.body.to_s
end

#options_request(path, headers = {}) ⇒ Object



89
90
91
# File 'lib/ember_cli/dev_server.rb', line 89

def options_request(path, headers = {})
  request(Net::HTTP::Options, path, headers)
end

#originObject

The origin the browser loads the application's assets from. It defaults to the address the development server binds to, but can be configured separately for setups where the two differ — a development server bound to 0.0.0.0 inside a container, reached by the browser through a published port, for instance.



45
46
47
# File 'lib/ember_cli/dev_server.rb', line 45

def origin
  @origin ||= option(:origin) { "http://#{host}:#{port}" }.to_s.chomp("/")
end

#portObject



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

def port
  @port ||= option(:port) { available_port }.to_i
end

#startObject

Boots the development server unless something is already listening on its address, and blocks until it accepts connections.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ember_cli/dev_server.rb', line 51

def start
  return true if listening?

  # Requests are served concurrently, so make sure only one of them boots
  # the development server.
  monitor.synchronize do
    return true if listening?

    shell.start_dev_server(host: host, port: port)

    wait_until_listening!
  end
end

#timeoutObject



36
37
38
# File 'lib/ember_cli/dev_server.rb', line 36

def timeout
  @timeout ||= option(:timeout) { DEFAULT_TIMEOUT }.to_f
end