Skip to content

Commit

Permalink
fix: Handle task event and add cd to build.
Browse files Browse the repository at this point in the history
  • Loading branch information
GSMLG-BOT committed Feb 14, 2025
1 parent 5b4da7c commit 9838205
Show file tree
Hide file tree
Showing 15 changed files with 50,618 additions and 217,283 deletions.
20 changes: 17 additions & 3 deletions lib/phoenix/mix/build/bun.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Mix.Tasks.Phx.React.Bun.Bundle do
@shortdoc "Bundle components into server.js"
def run(args) do
{opts, _argv} =
OptionParser.parse!(args, strict: [component_base: :string, output: :string])
OptionParser.parse!(args, strict: [component_base: :string, output: :string, cd: :string])

component_base = Keyword.get(opts, :component_base)
base_dir = Path.absname(component_base, File.cwd!()) |> Path.expand()
Expand Down Expand Up @@ -44,14 +44,28 @@ defmodule Mix.Tasks.Phx.React.Bun.Bundle do
tmp_file = "#{File.cwd!()}/server.js"
File.write!(tmp_file, result)

cd = Keyword.get(opts, :cd, File.cwd!())

outdir = Path.dirname(output)

if File.exists?(output) do
File.rm!(output)
end

{out, code} =
System.cmd("bun", ["build", "--target=bun", "--outdir=#{Path.dirname(output)}", tmp_file])
System.cmd("bun", ["build", "--target=bun", "--outdir=#{outdir}", tmp_file], cd: cd)

IO.puts(~s[cd #{cd}; bun build --target=bun --outdir=#{outdir} #{tmp_file}])
IO.puts("out #{code}: #{out}")

if code != 0 do
IO.puts(out)
throw("bun build failed(#{code})")
end

if Path.join(outdir, "server.js") != output do
File.rename(Path.join(outdir, "server.js"), output)
end

File.rm!(tmp_file)
rescue
error ->
Expand Down
3 changes: 2 additions & 1 deletion lib/phoenix/react/runtime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ defmodule Phoenix.React.Runtime do
DynamicSupervisor.start_child(__MODULE__, spec)
end

defstruct [:component_base, :port, :server_js, render_timeout: 300_000]
defstruct [:component_base, :port, :server_js, :cd, render_timeout: 300_000]

@type t :: %__MODULE__{
render_timeout: integer(),
component_base: path(),
server_js: path(),
cd: path(),
port: port()
}

Expand Down
23 changes: 16 additions & 7 deletions lib/phoenix/react/runtime/bun.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ defmodule Phoenix.React.Runtime.Bun do
%Runtime{
component_base: component_base,
render_timeout: render_timeout,
server_js: config()[:server_js]
server_js: config()[:server_js],
cd: config()[:cd]
}, {:continue, :start_port}}
end

Expand Down Expand Up @@ -119,11 +120,13 @@ defmodule Phoenix.React.Runtime.Bun do
def start_file_watcher(component_base) do
Logger.debug("Building server.js bundle")

Mix.Task.run("phx.react.bun.bundle", [
Mix.Tasks.Phx.React.Bun.Bundle.run([
"--component-base",
component_base,
"--output",
config()[:server_js]
config()[:server_js],
"--cd",
config()[:cd]
])

Logger.debug("Starting file watcher")
Expand All @@ -132,20 +135,26 @@ defmodule Phoenix.React.Runtime.Bun do

@impl true
def handle_info({:component_base_changed, path}, state) do
Logger.debug("component_base changed: #{path}")

Task.async(fn ->
Mix.Task.run("phx.react.bun.bundle", [
Logger.debug("component_base changed: #{path}, rebuilding...")

Mix.Tasks.Phx.React.Bun.Bundle.run([
"--component-base",
state.component_base,
"--output",
config()[:server_js]
state.server_js,
"--cd",
state.cd
])
end)

{:noreply, state}
end

def handle_info({_ref, :ok}, state) do
{:noreply, state}
end

def handle_info({_port, {:data, msg}}, state) do
Logger.debug(msg)
{:noreply, state}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Phoenix.React.Mixfile do
use Mix.Project

@source_url "https://github.com/gsmlg-dev/phoenix-react.git"
@version "0.5.0"
@version "0.5.1"

def project do
[
Expand Down
12 changes: 12 additions & 0 deletions react_demo/assets/component/markdown.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import * as React from 'react';
import Markdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
import MDEditor from '@uiw/react-md-editor';

import {Prism as SyntaxHighlighter} from 'react-syntax-highlighter';
import {dark} from 'react-syntax-highlighter/dist/esm/styles/prism';

export const Component = (props = {}) => {

// return (
// <MDEditor.Markdown
// source={props.data}
// style={{
// whiteSpace: 'pre-wrap',
// padding: '1rem',
// }}
// />
// );
return (
<Markdown
className="markdown-body"
Expand Down
2 changes: 1 addition & 1 deletion react_demo/config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ config :phoenix_react_server, Phoenix.React,
config :phoenix_react_server, Phoenix.React.Runtime.Bun,
cd: Path.expand("..", __DIR__),
cmd: System.find_executable("bun"),
server_js: Path.expand("../priv/react/dev/server.js", __DIR__),
server_js: Path.expand("../priv/react/server.js", __DIR__),
env: :dev
1 change: 0 additions & 1 deletion react_demo/config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ config :react_demo, ReactDemoWeb.Endpoint,


config :phoenix_react_server, Phoenix.React.Runtime.Bun,
cd: Path.expand("..", __DIR__),
cmd: System.find_executable("bun"),
server_js: Path.expand("../priv/react/server.js", __DIR__),
port: 5124,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="card">
<div class="card-body">
<h3 class="card-title justify-center">This markdown is rendered with <code>react-dom/server</code></h3>
<div class="prose m-auto min-w-max">
<div class="m-auto min-w-max">
<.react_markdown
data={@markdown_doc}
/>
Expand Down
Loading

0 comments on commit 9838205

Please sign in to comment.