Skip to content

Commit e502031

Browse files
✨ Pass NODE_PATH to esbuild for node_modules support.
1 parent 3c624d4 commit e502031

File tree

6 files changed

+108
-141
lines changed

6 files changed

+108
-141
lines changed

manifest.toml

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/lustre_dev_tools/cli/build.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ fn bundle_tailwind(
316316
}
317317

318318
fn exec_tailwind(root: String, options: List(String)) -> Result(String, Error) {
319-
cmd.exec("./build/.lustre/bin/tailwind", in: root, with: options)
319+
cmd.exec("./build/.lustre/bin/tailwind", in: root, env: [], with: options)
320320
|> result.map_error(fn(pair) { BundleError(pair.1) })
321321
}
322322

src/lustre_dev_tools/cmd.gleam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import gleam/dynamic.{type Dynamic}
99
@external(erlang, "lustre_dev_tools_ffi", "exec")
1010
pub fn exec(
1111
run command: String,
12+
env env: List(#(String, String)),
1213
with args: List(String),
1314
in in: String,
1415
) -> Result(String, #(Int, String))

src/lustre_dev_tools/esbuild.gleam

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,12 @@ fn set_file_permissions(file: String) -> Result(Nil, Error) {
259259
}
260260

261261
fn exec_esbuild(root: String, options: List(String)) -> Result(String, Error) {
262-
cmd.exec("./build/.lustre/bin/esbuild", in: root, with: options)
262+
cmd.exec(
263+
"./build/.lustre/bin/esbuild",
264+
in: root,
265+
env: [#("NODE_PATH", "./node_modules")],
266+
with: options,
267+
)
263268
|> result.map_error(fn(pair) { BundleError(pair.1) })
264269
}
265270

src/lustre_dev_tools/project.gleam

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ fn do_otp_version() -> String
4949
/// Compile the current project running the `gleam build` command.
5050
///
5151
pub fn build() -> Result(Nil, Error) {
52-
cmd.exec(run: "gleam", in: ".", with: ["build", "--target", "javascript"])
52+
cmd.exec(run: "gleam", in: ".", env: [], with: [
53+
"build", "--target", "javascript",
54+
])
5355
|> result.map_error(fn(err) { BuildError(pair.second(err)) })
5456
|> result.replace(Nil)
5557
}
@@ -60,7 +62,7 @@ pub fn interface() -> Result(Interface, Error) {
6062
let args = ["export", "package-interface", "--out", out]
6163

6264
use _ <- result.try(
63-
cmd.exec(run: "gleam", in: ".", with: args)
65+
cmd.exec(run: "gleam", in: ".", env: [], with: args)
6466
|> result.map_error(fn(err) { BuildError(pair.second(err)) }),
6567
)
6668

src/lustre_dev_tools_ffi.erl

Lines changed: 96 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
-module(lustre_dev_tools_ffi).
2-
-export([
3-
check_live_reloading/0,
4-
fs_start_link/2,
5-
get_cwd/0,
6-
get_cpu/0,
7-
get_esbuild/1,
8-
get_tailwind/1,
9-
get_os/0,
10-
otp_version/0,
11-
unzip_esbuild/1,
12-
exec/3
13-
]).
2+
3+
-export([check_live_reloading/0, fs_start_link/2, get_cwd/0, get_cpu/0, get_esbuild/1,
4+
get_tailwind/1, get_os/0, otp_version/0, unzip_esbuild/1, exec/4]).
145

156
otp_version() ->
167
Version = erlang:system_info(otp_release),
178
list_to_binary(Version).
189

1910
get_cwd() ->
2011
case file:get_cwd() of
21-
{ok, Cwd} -> {ok, unicode:characters_to_binary(Cwd)};
22-
{error, Reason} -> {error, Reason}
12+
{ok, Cwd} ->
13+
{ok, unicode:characters_to_binary(Cwd)};
14+
{error, Reason} ->
15+
{error, Reason}
2316
end.
2417

2518
get_os() ->
2619
case os:type() of
27-
{win32, _} -> <<"win32">>;
28-
{unix, darwin} -> <<"darwin">>;
29-
{unix, linux} -> <<"linux">>;
30-
{_, Unknown} -> atom_to_binary(Unknown, utf8)
20+
{win32, _} ->
21+
<<"win32">>;
22+
{unix, darwin} ->
23+
<<"darwin">>;
24+
{unix, linux} ->
25+
<<"linux">>;
26+
{_, Unknown} ->
27+
atom_to_binary(Unknown, utf8)
3128
end.
3229

3330
get_cpu() ->
3431
case erlang:system_info(os_type) of
3532
{unix, _} ->
36-
[Arch, _] = string:split(erlang:system_info(system_architecture), "-"),
33+
[Arch, _] =
34+
string:split(
35+
erlang:system_info(system_architecture), "-"),
3736
list_to_binary(Arch);
3837
{win32, _} ->
3938
case erlang:system_info(wordsize) of
40-
4 -> <<"ia32">>;
41-
8 -> <<"x64">>
39+
4 ->
40+
<<"ia32">>;
41+
8 ->
42+
<<"x64">>
4243
end
4344
end.
4445

@@ -47,72 +48,87 @@ get_esbuild(Url) ->
4748
ssl:start(),
4849

4950
case httpc:request(get, {Url, []}, [], [{body_format, binary}]) of
50-
{ok, {{_, 200, _}, _, Zip}} -> {ok, Zip};
51-
{ok, Res} -> {error, Res};
52-
{error, Err} -> {error, Err}
51+
{ok, {{_, 200, _}, _, Zip}} ->
52+
{ok, Zip};
53+
{ok, Res} ->
54+
{error, Res};
55+
{error, Err} ->
56+
{error, Err}
5357
end.
5458

5559
get_tailwind(Url) ->
5660
inets:start(),
5761
ssl:start(),
5862

5963
case httpc:request(get, {Url, []}, [], [{body_format, binary}]) of
60-
{ok, {{_, 200, _}, _, Bin}} -> {ok, Bin};
61-
{ok, Res} -> {error, Res};
62-
{error, Err} -> {error, Err}
64+
{ok, {{_, 200, _}, _, Bin}} ->
65+
{ok, Bin};
66+
{ok, Res} ->
67+
{error, Res};
68+
{error, Err} ->
69+
{error, Err}
6370
end.
6471

6572
unzip_esbuild(Zip) ->
6673
Filepath =
6774
case os:type() of
68-
{win32, _} -> "package/esbuild.exe";
69-
_ -> "package/bin/esbuild"
75+
{win32, _} ->
76+
"package/esbuild.exe";
77+
_ ->
78+
"package/bin/esbuild"
7079
end,
7180

72-
Result =
73-
erl_tar:extract({binary, Zip}, [
74-
memory, compressed, {files, [Filepath]}
75-
]),
81+
Result = erl_tar:extract({binary, Zip}, [memory, compressed, {files, [Filepath]}]),
7682

7783
case Result of
78-
{ok, [{_, Esbuild}]} -> {ok, Esbuild};
79-
{ok, Res} -> {error, Res};
80-
{error, Err} -> {error, Err}
84+
{ok, [{_, Esbuild}]} ->
85+
{ok, Esbuild};
86+
{ok, Res} ->
87+
{error, Res};
88+
{error, Err} ->
89+
{error, Err}
8190
end.
8291

83-
exec(Command, Args, Cwd) ->
92+
exec(Command, Env, Args, Cwd) ->
8493
Command_ = binary_to_list(Command),
94+
Env_ = lists:map(fun({K, V}) -> {binary_to_list(K), binary_to_list(V)} end, Env),
8595
Args_ = lists:map(fun(Arg) -> binary_to_list(Arg) end, Args),
8696
Cwd_ = binary_to_list(Cwd),
8797

88-
Name = case Command_ of
89-
"./" ++ _ -> {spawn_executable, Command_};
90-
"/" ++ _ -> {spawn_executable, Command_};
91-
_ -> {spawn_executable, os:find_executable(Command_)}
92-
end,
93-
94-
Port = open_port(Name, [
95-
exit_status,
96-
binary,
97-
hide,
98-
stream,
99-
eof,
100-
stderr_to_stdout, % We need this to hide the process' stdout
101-
{args, Args_},
102-
{cd, Cwd_}
103-
]),
98+
Name =
99+
case Command_ of
100+
"./" ++ _ ->
101+
{spawn_executable, Command_};
102+
"/" ++ _ ->
103+
{spawn_executable, Command_};
104+
_ ->
105+
{spawn_executable, os:find_executable(Command_)}
106+
end,
107+
108+
Port =
109+
open_port(Name,
110+
[exit_status,
111+
binary,
112+
hide,
113+
stream,
114+
eof,
115+
stderr_to_stdout, % We need this to hide the process' stdout
116+
{env, Env_},
117+
{args, Args_},
118+
{cd, Cwd_}]),
104119

105120
do_exec(Port, []).
106121

107122
do_exec(Port, Acc) ->
108123
receive
109-
{Port, {data, Data}} -> do_exec(Port, [Data | Acc]);
124+
{Port, {data, Data}} ->
125+
do_exec(Port, [Data | Acc]);
110126
{Port, {exit_status, 0}} ->
111-
port_close(Port),
112-
{ok, list_to_binary(lists:reverse(Acc))};
127+
port_close(Port),
128+
{ok, list_to_binary(lists:reverse(Acc))};
113129
{Port, {exit_status, Code}} ->
114-
port_close(Port),
115-
{error, {Code, list_to_binary(lists:reverse(Acc))}}
130+
port_close(Port),
131+
{error, {Code, list_to_binary(lists:reverse(Acc))}}
116132
end.
117133

118134
fs_start_link(Id, Path) ->
@@ -135,21 +151,32 @@ fs_start_link(Id, Path) ->
135151
check_live_reloading() ->
136152
Watcher =
137153
case os:type() of
138-
{unix, darwin} -> fsevents;
139-
{unix, linux} -> inotifywait;
140-
{unix, freebsd} -> inotifywait;
141-
{unix, openbsd} -> inotifywait;
142-
{unix, sunos} -> undefined;
143-
{unix, _} -> kqueue;
144-
{win32, nt} -> inotifywait_win32;
145-
_ -> undefined
154+
{unix, darwin} ->
155+
fsevents;
156+
{unix, linux} ->
157+
inotifywait;
158+
{unix, freebsd} ->
159+
inotifywait;
160+
{unix, openbsd} ->
161+
inotifywait;
162+
{unix, sunos} ->
163+
undefined;
164+
{unix, _} ->
165+
kqueue;
166+
{win32, nt} ->
167+
inotifywait_win32;
168+
_ ->
169+
undefined
146170
end,
147171

148172
case Watcher of
149-
undefined -> {error, no_file_watcher_supported_for_os};
173+
undefined ->
174+
{error, no_file_watcher_supported_for_os};
150175
_ ->
151176
case Watcher:find_executable() of
152-
false -> {error, {no_file_watcher_installed, Watcher}};
153-
_ -> {ok, nil}
177+
false ->
178+
{error, {no_file_watcher_installed, Watcher}};
179+
_ ->
180+
{ok, nil}
154181
end
155182
end.

0 commit comments

Comments
 (0)