Class: Esbuild::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/esbuild/service.rb

Instance Method Summary collapse

Constructor Details

#initializeService

TODO: plugins



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/esbuild/service.rb', line 13

def initialize
  @request_id = 0
  @serve_id = 0
  @build_key = 0
  @first_packet = true
  @response_callbacks = Concurrent::Map.new
  @plugin_callbacks = Concurrent::Map.new
  @watch_callbacks = Concurrent::Map.new
  @serve_callbacks = Concurrent::Map.new
  @buffer = String.new(encoding: Encoding::BINARY)

  child_read, child_stdout = create_pipes
  child_stdin, @child_write = create_pipes

  bin = binary_path
  pid = spawn(bin, "--service=#{ESBUILD_VERSION}", "--ping", out: child_stdout, err: :err, in: child_stdin)
  child_stdin.close
  child_stdout.close

  Thread.new { worker_thread(pid, child_read) }
end

Instance Method Details

#build_or_serve(options, serve_options = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/esbuild/service.rb', line 35

def build_or_serve(options, serve_options = nil)
  key = @build_key
  @build_key += 1
  opts = Flags.flags_for_build_options(options)
  on_rebuild = opts[:watch]&.fetch(:on_rebuild, nil)
  request = {
    "command" => "build",
    "key" => key,
    "entries" => opts[:entries],
    "flags" => opts[:flags],
    "write" => opts[:write],
    "stdinContents" => opts[:stdin_contents],
    "stdinResolveDir" => opts[:stdin_resolve_dir],
    "absWorkingDir" => opts[:abs_working_dir] || Dir.pwd,
    "incremental" => opts[:incremental],
    "nodePaths" => opts[:node_paths],
    "hasOnRebuild" => !!on_rebuild
  }
  serve = serve_options && build_serve_data(serve_options, request)

  response = send_request(request)
  if serve
    ServeResult.new(response, serve[:wait], serve[:stop])
  else
    build_state = BuildState.new(self, on_rebuild)
    build_state.response_to_result(response)
  end
end

#send_request(request) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/esbuild/service.rb', line 92

def send_request(request)
  @request_id += 1
  id = @request_id
  encoder = StdioProtocol::PacketEncoder.new
  encoded = encoder.encode_packet(Packet.new(id, true, request))
  @child_write.write encoded
  ivar = Concurrent::IVar.new
  @response_callbacks[id] = ivar
  ivar.wait!
  ivar.value
end

#start_watch(watch_id, proc) ⇒ Object



64
65
66
# File 'lib/esbuild/service.rb', line 64

def start_watch(watch_id, proc)
  @watch_callbacks[watch_id] = proc
end

#stop_watch(watch_id) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/esbuild/service.rb', line 68

def stop_watch(watch_id)
  @watch_callbacks.delete(watch_id)
  send_request(
    "command" => "watch-stop",
    "watchID" => watch_id
  )
end

#transform(input, options) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/esbuild/service.rb', line 76

def transform(input, options)
  flags = Flags.flags_for_transform_options(options)
  res = send_request(
    "command" => "transform",
    "flags" => flags,
    "inputFS" => false,
    "input" => input
  )

  unless res["errors"].empty?
    raise BuildFailureError.new(res["errors"], res["warnings"])
  end

  TransformResult.new(res)
end