Skip to content

Commit c09ad9d

Browse files
committed
Use TAILWIND_WASM_PATH for WASM install with required path
1 parent 579f992 commit c09ad9d

3 files changed

Lines changed: 44 additions & 136 deletions

File tree

README.md

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,19 @@ Precompiled NIF binaries are available for `x86_64-linux`, `aarch64-linux`, `aar
3232

3333
A precompiled WebAssembly binary is also available with each release. This lets you run the full Tailwind compiler in any WASM runtime — browsers, Deno, Cloudflare Workers, etc.
3434

35-
To install the WASM binary alongside the NIF during `mix compile`:
35+
To install the WASM binary during `mix compile`, set `TAILWIND_WASM_PATH` to the
36+
directory or file path where it should be saved:
3637

3738
```bash
38-
# Download to the default cache location
39-
TAILWIND_COMPILER_WASM=true mix compile
39+
# Install to a directory (saved as tailwind_compiler.wasm)
40+
TAILWIND_WASM_PATH=priv/static/assets mix compile
4041

41-
# Download and copy to a specific directory
42-
TAILWIND_COMPILER_WASM=priv/static/assets mix compile
43-
44-
# Download and copy to a specific file path
45-
TAILWIND_COMPILER_WASM=priv/static/assets/tw.wasm mix compile
42+
# Install to a specific file path
43+
TAILWIND_WASM_PATH=priv/static/assets/tw.wasm mix compile
4644
```
4745

48-
You can also download it on demand at runtime:
49-
50-
```elixir
51-
{:ok, path} = TailwindCompiler.wasm_path()
52-
File.cp!(path, "priv/static/assets/tailwind_compiler.wasm")
53-
```
46+
The directory must already exist or compilation will fail. When unset, no WASM
47+
binary is downloaded.
5448

5549
The WASM binary exports three functions: `alloc`, `free`, and `compile`. See the [WASM Playground](https://beaconcms.github.io/tailwind_compiler/) source for a complete browser integration example.
5650

lib/tailwind_compiler.ex

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,6 @@ defmodule TailwindCompiler do
8585
end
8686
end
8787

88-
@doc """
89-
Returns the path to the precompiled WASM binary, downloading it if necessary.
90-
91-
The WASM binary is a standalone WebAssembly module (~350KB) that can run the
92-
full Tailwind CSS compiler in any WASM runtime (browser, Deno, Cloudflare Workers, etc.).
93-
94-
## Examples
95-
96-
{:ok, path} = TailwindCompiler.wasm_path()
97-
File.read!(path)
98-
#=> <<0, 97, 115, 109, ...>>
99-
100-
"""
101-
@spec wasm_path() :: {:ok, String.t()} | {:error, term()}
102-
defdelegate wasm_path, to: TailwindCompiler.Native
103-
104-
@doc """
105-
Same as `wasm_path/0` but raises on error.
106-
"""
107-
@spec wasm_path!() :: String.t()
108-
defdelegate wasm_path!, to: TailwindCompiler.Native
109-
11088
@doc """
11189
Validate a list of token strings, returning only those recognized as valid
11290
Tailwind CSS utilities.

lib/tailwind_compiler/native.ex

Lines changed: 36 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ defmodule TailwindCompiler.Native do
1111
Returns `false` if `TAILWIND_COMPILER_PATH` is set, or if no precompiled
1212
NIF could be found, in which case Zigler compilation is used as fallback.
1313
14-
When `TAILWIND_COMPILER_WASM` is set, also downloads the WASM binary
15-
during compilation.
14+
When `TAILWIND_WASM_PATH` is set, also downloads and installs the WASM
15+
binary to the specified path during compilation.
1616
"""
1717
def use_precompiled? do
18-
if install_wasm?(), do: ensure_wasm()
18+
if wasm_path = System.get_env("TAILWIND_WASM_PATH") do
19+
install_wasm!(wasm_path)
20+
end
21+
1922
not force_build?() and ensure_precompiled()
2023
end
2124

@@ -26,26 +29,6 @@ defmodule TailwindCompiler.Native do
2629
System.get_env("TAILWIND_COMPILER_PATH") != nil
2730
end
2831

29-
@doc """
30-
Whether to install the WASM binary during compilation.
31-
"""
32-
def install_wasm? do
33-
System.get_env("TAILWIND_COMPILER_WASM") != nil
34-
end
35-
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-
4932
@doc """
5033
Returns the NIF path (without extension) for `:erlang.load_nif/2`.
5134
Uses `:code.priv_dir/1` so it works at runtime (including in releases).
@@ -67,34 +50,6 @@ defmodule TailwindCompiler.Native do
6750
"#{arch()}-#{os()}"
6851
end
6952

70-
@doc """
71-
Returns the path to the WASM binary, downloading it if necessary.
72-
73-
Returns `{:ok, path}` if the binary is available, `{:error, reason}` otherwise.
74-
"""
75-
def wasm_path do
76-
path = wasm_file_path()
77-
78-
if File.exists?(path) do
79-
{:ok, path}
80-
else
81-
case download_wasm() do
82-
:ok -> {:ok, path}
83-
{:error, reason} -> {:error, reason}
84-
end
85-
end
86-
end
87-
88-
@doc """
89-
Same as `wasm_path/0` but raises on error.
90-
"""
91-
def wasm_path! do
92-
case wasm_path() do
93-
{:ok, path} -> path
94-
{:error, reason} -> raise "Could not get WASM binary: #{inspect(reason)}"
95-
end
96-
end
97-
9853
# --- Private ---
9954

10055
defp ensure_precompiled do
@@ -107,29 +62,39 @@ defmodule TailwindCompiler.Native do
10762
end
10863
end
10964

110-
defp ensure_wasm do
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}")
65+
defp install_wasm!(wasm_path) do
66+
dest = if Path.extname(wasm_path) == ".wasm" do
67+
wasm_path
68+
else
69+
Path.join(wasm_path, "tailwind_compiler.wasm")
70+
end
71+
72+
dir = Path.dirname(dest)
73+
74+
unless File.dir?(dir) do
75+
Mix.raise("TAILWIND_WASM_PATH directory does not exist: #{dir}")
76+
end
77+
78+
# Download to a temp location, then copy to the target
79+
tmp_dir = Path.join(System.tmp_dir!(), "tailwind_compiler_wasm")
80+
File.mkdir_p!(tmp_dir)
81+
82+
case download_and_extract(wasm_download_url(), tmp_dir) do
83+
:ok ->
84+
tmp_file = Path.join(tmp_dir, "tailwind_compiler.wasm")
85+
86+
if File.exists?(tmp_file) do
87+
File.cp!(tmp_file, dest)
88+
File.rm_rf!(tmp_dir)
89+
log("Installed WASM binary to #{dest}")
90+
else
91+
File.rm_rf!(tmp_dir)
92+
Mix.raise("WASM archive did not contain tailwind_compiler.wasm")
12993
end
13094

13195
{:error, reason} ->
132-
log("Warning: could not download WASM binary: #{inspect(reason)}")
96+
File.rm_rf!(tmp_dir)
97+
Mix.raise("Could not download WASM binary: #{inspect(reason)}")
13398
end
13499
end
135100

@@ -148,14 +113,6 @@ defmodule TailwindCompiler.Native do
148113
Path.join(build_priv_dir(), "native")
149114
end
150115

151-
defp wasm_dir do
152-
Path.join(build_priv_dir(), "wasm")
153-
end
154-
155-
defp wasm_file_path do
156-
Path.join(wasm_dir(), "tailwind_compiler.wasm")
157-
end
158-
159116
defp build_priv_dir do
160117
Path.join([Mix.Project.build_path(), "lib", "tailwind_compiler", "priv"])
161118
end
@@ -191,27 +148,6 @@ defmodule TailwindCompiler.Native do
191148
end
192149
end
193150

194-
defp download_wasm do
195-
url = wasm_download_url()
196-
log("Downloading WASM binary...")
197-
198-
dest = wasm_dir()
199-
File.mkdir_p!(dest)
200-
201-
case download_and_extract(url, dest) do
202-
:ok ->
203-
if File.exists?(wasm_file_path()) do
204-
log("Successfully installed WASM binary")
205-
:ok
206-
else
207-
{:error, "Archive did not contain tailwind_compiler.wasm"}
208-
end
209-
210-
{:error, reason} ->
211-
{:error, reason}
212-
end
213-
end
214-
215151
defp nif_download_url(target_triple) do
216152
"https://github.com/#{@github_repo}/releases/download/v#{@version}/tailwind_compiler-nif-#{target_triple}.tar.gz"
217153
end

0 commit comments

Comments
 (0)