Skip to content

Commit be2c7b6

Browse files
committed
Update tests, readme, and simplify install
1 parent f7e3037 commit be2c7b6

File tree

6 files changed

+49
-178
lines changed

6 files changed

+49
-178
lines changed

README.md

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,17 @@ def deps do
2424
end
2525
```
2626

27-
However, if your assets are precompiled during development,
28-
then it only needs to be a dev dependency:
29-
30-
```elixir
31-
def deps do
32-
[
33-
{:tailwind, "~> 0.3", only: :dev}
34-
]
35-
end
36-
```
37-
3827
Once installed, change your `config/config.exs` to pick your
3928
Tailwind version of choice:
4029

4130
```elixir
42-
config :tailwind, version: "4.0.9"
31+
config :tailwind, version: "4.1.12"
4332
```
4433

45-
Note that `:tailwind` 0.3+ assumes Tailwind v4+ by default. It still supports Tailwind v3, but some configuration options when setting up a new
46-
project might be different. If you use Tailwind v3, also have a look at [the README in the 0.2 branch](https://github.com/phoenixframework/tailwind/blob/v0.2/README.md).
34+
Note that `:tailwind` 0.3+ assumes Tailwind v4+ by default.
35+
It still supports Tailwind v3, but some configuration options
36+
when setting up a new project might be different. If you use
37+
Tailwind v3, also have a look at [the README in the 0.2 branch](https://github.com/phoenixframework/tailwind/blob/v0.2/README.md).
4738

4839
Now you can install Tailwind by running:
4940

@@ -73,11 +64,11 @@ Where `TARGET` is your system target architecture.
7364
The first argument to `tailwind` is the execution profile.
7465
You can define multiple execution profiles with the current
7566
directory, the OS environment, and default arguments to the
76-
`tailwind` task:
67+
`tailwind` task in your `config/config.exs`:
7768

7869
```elixir
7970
config :tailwind,
80-
version: "4.0.9",
71+
version: "4.1.12",
8172
default: [
8273
args: ~w(
8374
--input=assets/css/app.css
@@ -88,22 +79,7 @@ config :tailwind,
8879
```
8980

9081
When `mix tailwind default` is invoked, the task arguments will be appended
91-
to the ones configured above. Note profiles must be configured in your
92-
`config/config.exs`, as `tailwind` runs without starting your application
93-
(and therefore it won't pick settings in `config/runtime.exs`).
94-
95-
We also strongly recommend setting up the `@source` paths in your in `app.css`
96-
file, for example:
97-
98-
```css
99-
@source "../css";
100-
@source "../js";
101-
@source "../../lib/YOUR_APP_web";
102-
```
103-
104-
This will make Tailwind watch `assets/css`, `assets/js` and `lib/YOUR_APP_web`.
105-
Without those, too many files (including build artifacts) may be watched,
106-
leading to frequent recompilations.
82+
to the ones configured above.
10783

10884
## Adding to Phoenix
10985

@@ -117,7 +93,7 @@ as a dependency in your `mix.exs`:
11793
```elixir
11894
def deps do
11995
[
120-
{:phoenix, "~> 1.7"},
96+
{:phoenix, "~> 1.8"},
12197
{:tailwind, "~> 0.3", runtime: Mix.env() == :dev}
12298
]
12399
end
@@ -136,7 +112,7 @@ We'll also give it our `assets/css/app.css` as our css entry point:
136112

137113
```elixir
138114
config :tailwind,
139-
version: "4.0.9",
115+
version: "4.1.12",
140116
default: [
141117
args: ~w(
142118
--input=assets/css/app.css
@@ -147,14 +123,14 @@ config :tailwind,
147123
```
148124

149125
> Make sure the "assets" directory from priv/static is listed in the
150-
> :only option for Plug.Static in your lib/my_app_web/endpoint.ex
126+
> `:only` option for Plug.Static in your `lib/my_app_web/endpoint.ex`
151127
152128
If your Phoenix application is using an umbrella structure, you should specify
153129
the web application's asset directory in the configuration:
154130

155131
```elixir
156132
config :tailwind,
157-
version: "4.0.9",
133+
version: "4.1.12",
158134
default: [
159135
args: ...,
160136
cd: Path.expand("../apps/<folder_ending_with_web>", __DIR__)
@@ -170,15 +146,23 @@ configuration in your `config/dev.exs` and add:
170146

171147
Note we are enabling the file system watcher.
172148

173-
Finally, run the command:
149+
Finally, create the relevant `assets/css/app.css` file:
174150

175-
```bash
176-
$ mix tailwind.install
151+
```css
152+
@import "tailwindcss" source(none);
153+
@source "../css";
154+
@source "../js";
155+
@source "../../lib/YOUR_APP_web";
156+
157+
@custom-variant phx-click-loading (.phx-click-loading&, .phx-click-loading &);
158+
@custom-variant phx-submit-loading (.phx-submit-loading&, .phx-submit-loading &);
159+
@custom-variant phx-change-loading (.phx-change-loading&, .phx-change-loading &);
177160
```
178161

179-
This command installs Tailwind and updates your `assets/css/app.css`
180-
and `assets/js/app.js` with the necessary changes to start using Tailwind
181-
right away. See `mix help tailwind.install` to learn more.
162+
We also strongly recommend setting up the `@source` paths in your in `app.css`
163+
file to watch `assets/css`, `assets/js` and `lib/YOUR_APP_web`, as above.
164+
Without those, too many files (including build artifacts) may be watched,
165+
leading to frequent recompilations.
182166

183167
## Updating from Tailwind v3 to v4
184168

@@ -200,7 +184,7 @@ For a typical Phoenix application, updating from Tailwind v3 to v4 requires the
200184
```diff
201185
config :tailwind,
202186
- version: "3.4.13",
203-
+ version: "4.0.9",
187+
+ version: "4.1.12",
204188
default: [
205189
args: ~w(
206190
- --config=tailwind.config.js

config/config.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Config
22

33
config :tailwind,
4-
version: "4.0.9",
4+
version: "4.1.12",
55
another: [
66
args: ["--help"]
77
]

lib/mix/tasks/tailwind.install.ex

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,22 @@ defmodule Mix.Tasks.Tailwind.Install do
2121
2222
## Options
2323
24-
* `--runtime-config` - load the runtime configuration
25-
before executing command
24+
* `--runtime-config` - load the runtime configuration
25+
before executing command
2626
27-
* `--if-missing` - install only if the given version
28-
does not exist
27+
* `--if-missing` - install only if the given version
28+
does not exist
2929
30-
* `--no-assets` - does not install Tailwind assets
31-
32-
## Assets
33-
34-
Whenever Tailwind is installed, a default tailwind configuration
35-
will be placed in a new `assets/tailwind.config.js` file. See
36-
the [tailwind documentation](https://tailwindcss.com/docs/configuration)
37-
on configuration options.
38-
39-
The default tailwind configuration includes Tailwind variants for Phoenix
40-
LiveView specific lifecycle classes:
41-
42-
* phx-click-loading - applied when an event is sent to the server on click
43-
while the client awaits the server response
44-
* phx-submit-loading - applied when a form is submitted while the client awaits the server response
45-
* phx-change-loading - applied when a form input is changed while the client awaits the server response
46-
47-
Therefore, you may apply a variant, such as `phx-click-loading:animate-pulse`
48-
to customize tailwind classes when Phoenix LiveView classes are applied.
4930
"""
5031

51-
@shortdoc "Installs Tailwind executable and assets"
32+
@shortdoc "Installs Tailwind executable"
5233
@compile {:no_warn_undefined, Mix}
5334

5435
use Mix.Task
5536

5637
@impl true
5738
def run(args) do
58-
valid_options = [runtime_config: :boolean, if_missing: :boolean, assets: :boolean]
39+
valid_options = [runtime_config: :boolean, if_missing: :boolean]
5940

6041
{opts, base_url} =
6142
case OptionParser.parse_head!(args, strict: valid_options) do
@@ -81,13 +62,6 @@ defmodule Mix.Tasks.Tailwind.Install do
8162
if opts[:if_missing] && latest_version?() do
8263
:ok
8364
else
84-
if Keyword.get(opts, :assets, true) do
85-
File.mkdir_p!("assets/css")
86-
87-
prepare_app_css()
88-
prepare_app_js()
89-
end
90-
9165
if function_exported?(Mix, :ensure_application!, 1) do
9266
Mix.ensure_application!(:inets)
9367
Mix.ensure_application!(:ssl)
@@ -102,34 +76,4 @@ defmodule Mix.Tasks.Tailwind.Install do
10276
version = Tailwind.configured_version()
10377
match?({:ok, ^version}, Tailwind.bin_version())
10478
end
105-
106-
defp prepare_app_css do
107-
app_css =
108-
case File.read("assets/css/app.css") do
109-
{:ok, str} -> str
110-
{:error, _} -> ""
111-
end
112-
113-
unless app_css =~ "tailwind" do
114-
File.write!("assets/css/app.css", """
115-
@import "tailwindcss";
116-
@plugin "@tailwindcss/forms";
117-
@variant phx-click-loading ([".phx-click-loading&", ".phx-click-loading &"]);
118-
@variant phx-submit-loading ([".phx-submit-loading&", ".phx-submit-loading &"]);
119-
@variant phx-change-loading ([".phx-change-loading&", ".phx-change-loading &"]);
120-
121-
#{String.replace(app_css, ~s|@import "./phoenix.css";\n|, "")}\
122-
""")
123-
end
124-
end
125-
126-
defp prepare_app_js do
127-
case File.read("assets/js/app.js") do
128-
{:ok, app_js} ->
129-
File.write!("assets/js/app.js", String.replace(app_js, ~s|import "../css/app.css"\n|, ""))
130-
131-
{:error, _} ->
132-
:ok
133-
end
134-
end
13579
end

lib/tailwind.ex

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
defmodule Tailwind do
22
# https://github.com/tailwindlabs/tailwindcss/releases
3-
@latest_version "4.1.11"
3+
@latest_version "4.1.12"
44

55
@moduledoc """
66
Tailwind is an installer and runner for [tailwind](https://tailwindcss.com/).
@@ -15,10 +15,10 @@ defmodule Tailwind do
1515
version: "#{@latest_version}",
1616
default: [
1717
args: ~w(
18-
--input=css/app.css
19-
--output=../priv/static/assets/app.css
18+
--input=assets/css/app.css
19+
--output=priv/static/assets/app.css
2020
),
21-
cd: Path.expand("../assets", __DIR__),
21+
cd: Path.expand("..", __DIR__),
2222
]
2323
2424
## Tailwind configuration
@@ -57,10 +57,10 @@ defmodule Tailwind do
5757
version: "#{@latest_version}",
5858
default: [
5959
args: ~w(
60-
--input=css/app.css
61-
--output=../priv/static/assets/app.css
60+
--input=assets/css/app.css
61+
--output=priv/static/assets/app.css
6262
),
63-
cd: Path.expand("../assets", __DIR__),
63+
cd: Path.expand("..", __DIR__),
6464
],
6565
# skip executable check/download
6666
version_check: false,

test/tailwind_test.exs

Lines changed: 7 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ defmodule TailwindTest do
55

66
setup do
77
Application.put_env(:tailwind, :version, @version)
8-
File.mkdir_p!("assets/js")
9-
File.mkdir_p!("assets/css")
10-
File.rm("assets/css/app.css")
118
:ok
129
end
1310

14-
test "run on default" do
11+
test "--help" do
1512
assert ExUnit.CaptureIO.capture_io(fn ->
1613
assert Tailwind.run(:default, ["--help"]) == 0
1714
end) =~ @version
@@ -23,75 +20,21 @@ defmodule TailwindTest do
2320
end) =~ @version
2421
end
2522

26-
test "run with pre-existing app.css" do
23+
test "run on default" do
2724
assert ExUnit.CaptureIO.capture_io(fn ->
2825
assert Tailwind.run(:default, []) == 0
2926
end) =~ @version
3027
end
3128

32-
test "updates on install" do
29+
test "installs and updates with custom config and path" do
3330
Application.put_env(:tailwind, :version, "3.4.17")
34-
Mix.Task.rerun("tailwind.install", ["--if-missing"])
35-
36-
assert ExUnit.CaptureIO.capture_io(fn ->
37-
assert Tailwind.run(:default, ["--help"]) == 0
38-
end) =~ "3.4.17"
3931

40-
Application.delete_env(:tailwind, :version)
41-
42-
Mix.Task.rerun("tailwind.install", ["--if-missing"])
43-
assert File.read!("assets/css/app.css") =~ "tailwind"
32+
Mix.Task.rerun("tailwind.install", [
33+
"https://github.com/tailwindlabs/tailwindcss/releases/download/v$version/tailwindcss-$target"
34+
])
4435

4536
assert ExUnit.CaptureIO.capture_io(fn ->
4637
assert Tailwind.run(:default, ["--help"]) == 0
47-
end) =~ @version
48-
end
49-
50-
test "install on existing app.css and app.js" do
51-
File.write!("assets/css/app.css", """
52-
@import "./phoenix.css";
53-
body {
54-
}
55-
""")
56-
57-
File.write!("assets/js/app.js", """
58-
import "../css/app.css"
59-
60-
let Hooks = {}
61-
""")
62-
63-
Mix.Task.rerun("tailwind.install")
64-
65-
expected_css =
66-
String.trim("""
67-
@import "tailwindcss";
68-
@plugin "@tailwindcss/forms";
69-
@variant phx-click-loading ([".phx-click-loading&", ".phx-click-loading &"]);
70-
@variant phx-submit-loading ([".phx-submit-loading&", ".phx-submit-loading &"]);
71-
@variant phx-change-loading ([".phx-change-loading&", ".phx-change-loading &"]);
72-
73-
body {
74-
}
75-
""")
76-
77-
expected_js =
78-
String.trim("""
79-
80-
let Hooks = {}
81-
""")
82-
83-
assert String.trim(File.read!("assets/css/app.css")) == expected_css
84-
assert String.trim(File.read!("assets/js/app.js")) == expected_js
85-
86-
Mix.Task.rerun("tailwind.install")
87-
88-
assert String.trim(File.read!("assets/js/app.js")) == expected_js
89-
end
90-
91-
test "installs with custom URL" do
92-
assert :ok =
93-
Mix.Task.rerun("tailwind.install", [
94-
"https://github.com/tailwindlabs/tailwindcss/releases/download/v$version/tailwindcss-$target"
95-
])
38+
end) =~ "3.4.17"
9639
end
9740
end

test/test_helper.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ExUnit.start()
1+
ExUnit.start(timeout: 180_000)

0 commit comments

Comments
 (0)