Skip to content

Commit 579f992

Browse files
committed
Allow TAILWIND_COMPILER_WASM to specify install path
1 parent 087db4f commit 579f992

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ A precompiled WebAssembly binary is also available with each release. This lets
3535
To install the WASM binary alongside the NIF during `mix compile`:
3636

3737
```bash
38+
# Download to the default cache location
3839
TAILWIND_COMPILER_WASM=true mix compile
39-
```
4040

41-
Or set it in your environment permanently so it's always included:
41+
# Download and copy to a specific directory
42+
TAILWIND_COMPILER_WASM=priv/static/assets mix compile
4243

43-
```elixir
44-
# config/config.exs
45-
System.put_env("TAILWIND_COMPILER_WASM", "true")
44+
# Download and copy to a specific file path
45+
TAILWIND_COMPILER_WASM=priv/static/assets/tw.wasm mix compile
4646
```
4747

4848
You can also download it on demand at runtime:

lib/tailwind_compiler/native.ex

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ defmodule TailwindCompiler.Native do
3333
System.get_env("TAILWIND_COMPILER_WASM") != nil
3434
end
3535

36+
@doc """
37+
Returns the custom WASM install path from `TAILWIND_COMPILER_WASM`, or `nil`
38+
if the env var is unset or set to `"true"` (meaning use the default location).
39+
"""
40+
def wasm_install_path do
41+
case System.get_env("TAILWIND_COMPILER_WASM") do
42+
nil -> nil
43+
"true" -> nil
44+
"1" -> nil
45+
path -> path
46+
end
47+
end
48+
3649
@doc """
3750
Returns the NIF path (without extension) for `:erlang.load_nif/2`.
3851
Uses `:code.priv_dir/1` so it works at runtime (including in releases).
@@ -95,13 +108,28 @@ defmodule TailwindCompiler.Native do
95108
end
96109

97110
defp ensure_wasm do
98-
path = wasm_file_path()
111+
# Always download to the default cache location first
112+
case wasm_path() do
113+
{:ok, cached_path} ->
114+
# If a custom path was given, copy there
115+
case wasm_install_path() do
116+
nil ->
117+
:ok
118+
119+
custom ->
120+
dest = if Path.extname(custom) == ".wasm" do
121+
custom
122+
else
123+
Path.join(custom, "tailwind_compiler.wasm")
124+
end
125+
126+
File.mkdir_p!(Path.dirname(dest))
127+
File.cp!(cached_path, dest)
128+
log("Installed WASM binary to #{dest}")
129+
end
99130

100-
unless File.exists?(path) do
101-
case download_wasm() do
102-
:ok -> :ok
103-
{:error, reason} -> log("Warning: could not download WASM binary: #{inspect(reason)}")
104-
end
131+
{:error, reason} ->
132+
log("Warning: could not download WASM binary: #{inspect(reason)}")
105133
end
106134
end
107135

0 commit comments

Comments
 (0)