@@ -21,25 +21,36 @@ defmodule Phoenix.React.Runtime.Bun do
2121 use Phoenix.React.Runtime
2222
2323 def start_link ( init_arg ) do
24+ IO . inspect ( init_arg )
2425 GenServer . start_link ( __MODULE__ , init_arg , name: __MODULE__ )
2526 end
2627
2728 @ impl true
2829 def init ( component_base: component_base , render_timeout: render_timeout ) do
29- { :ok , % Runtime { component_base: component_base , render_timeout: render_timeout } ,
30- { :continue , :start_port } }
30+ { :ok ,
31+ % Runtime {
32+ component_base: component_base ,
33+ render_timeout: render_timeout ,
34+ server_js: config ( ) [ :server_js ]
35+ } , { :continue , :start_port } }
3136 end
3237
3338 @ impl true
3439 @ spec handle_continue ( :start_port , Phoenix.React.Runtime . t ( ) ) ::
3540 { :noreply , Phoenix.React.Runtime . t ( ) }
3641 def handle_continue ( :start_port , % Runtime { component_base: component_base } = state ) do
42+ if config ( ) [ :env ] == :dev do
43+ start_file_watcher ( component_base )
44+ end
45+
3746 port = start ( component_base: component_base )
3847
3948 Logger . debug (
4049 "Bun.Server started on port: #{ inspect ( port ) } and OS pid: #{ get_port_os_pid ( port ) } "
4150 )
4251
52+ Phoenix.React.Server . set_runtime_process ( self ( ) )
53+
4354 { :noreply , % Runtime { state | port: port } }
4455 end
4556
@@ -64,8 +75,12 @@ defmodule Phoenix.React.Runtime.Bun do
6475 def start ( component_base: component_base ) do
6576 cd = config ( ) [ :cd ]
6677 cmd = config ( ) [ :cmd ]
67- args = [ "--port" , Integer . to_string ( config ( ) [ :port ] ) , config ( ) [ :server_js ] ]
68- bun_env = if ( config ( ) [ :env ] == :dev , do: "development" , else: "production" )
78+ bun_port = Integer . to_string ( config ( ) [ :port ] )
79+ args = [ "--port" , bun_port , config ( ) [ :server_js ] ]
80+
81+ is_dev = config ( ) [ :env ] == :dev
82+
83+ bun_env = if ( is_dev , do: "development" , else: "production" )
6984
7085 args =
7186 if config ( ) [ :env ] == :dev do
@@ -75,6 +90,9 @@ defmodule Phoenix.React.Runtime.Bun do
7590 end
7691
7792 env = [
93+ { ~c" no_proxy" , ~c" 10.*,127.*,192.168.*,172.16.0.0/12,localhost,127.0.0.1,::1" } ,
94+ { ~c" PORT" , ~c" #{ bun_port } " } ,
95+ { ~c" BUN_PORT" , ~c" #{ bun_port } " } ,
7896 { ~c" BUN_ENV" , ~c" #{ bun_env } " } ,
7997 { ~c" COMPONENT_BASE" , ~c" #{ component_base } " }
8098 ]
@@ -96,6 +114,36 @@ defmodule Phoenix.React.Runtime.Bun do
96114 end
97115
98116 @ impl true
117+ def start_file_watcher ( component_base ) do
118+ Logger . debug ( "Building server.js bundle" )
119+
120+ Mix.Task . run ( "phx.react.bun.bundle" , [
121+ "--component-base" ,
122+ component_base ,
123+ "--output" ,
124+ config ( ) [ :server_js ]
125+ ] )
126+
127+ Logger . debug ( "Starting file watcher" )
128+ Runtime . start_file_watcher ( ref: self ( ) , path: component_base )
129+ end
130+
131+ @ impl true
132+ def handle_info ( { :component_base_changed , path } , state ) do
133+ Logger . debug ( "component_base changed: #{ path } " )
134+
135+ Task . async ( fn ->
136+ Mix.Task . run ( "phx.react.bun.bundle" , [
137+ "--component-base" ,
138+ state . component_base ,
139+ "--output" ,
140+ config ( ) [ :server_js ]
141+ ] )
142+ end )
143+
144+ { :noreply , state }
145+ end
146+
99147 def handle_info ( { _port , { :data , msg } } , state ) do
100148 Logger . debug ( msg )
101149 { :noreply , state }
0 commit comments