Skip to content

Commit 4626e8d

Browse files
authored
Update README (#36)
* Do not use the default namespace * Configure assets.setup and assets.build * Automatically run bun install on assets.setup * Add bun install command
1 parent 2e2eb96 commit 4626e8d

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

README.md

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,23 @@ directory, the OS environment, and default arguments to the
7272
```elixir
7373
config :bun,
7474
version: "1.1.22",
75-
default: [
75+
assets: [
76+
args: [],
77+
cd: Path.expand("../assets", __DIR__)
78+
],
79+
js: [
7680
args: ~w(build js/app.js),
7781
cd: Path.expand("../assets", __DIR__)
7882
]
7983
```
8084

81-
When `mix bun default` is invoked, the task arguments will be appended
82-
to the ones configured above.
85+
When `mix bun js` is invoked, it will invoke `bun build js/app.js`
86+
appending any argument given to the task. You can also use
87+
`mix bun assets` to run any `bun` command in the `assets` directory.
8388

8489
## Adding to Phoenix
8590

86-
To add `bun` to an application using Phoenix, you need only four steps. Installation requires that Phoenix watchers can accept module-function-args tuples which is not built into Phoenix 1.5.9.
91+
To add `bun` to an application using Phoenix, you need only four steps. Installation requires that Phoenix v1.6+:
8792

8893
First add it as a dependency in your `mix.exs`:
8994

@@ -96,16 +101,17 @@ def deps do
96101
end
97102
```
98103

99-
Now let's change `config/config.exs` to configure `bun` to use
100-
`assets/js/app.js` as an entry point and write to `priv/static/assets`:
104+
Now let's change `config/config.exs` to configure `bun` to add two commands,
105+
one to install dependencies and another to build `assets/js/app.js` as an
106+
entry point and write to `priv/static/assets`:
101107

102108
```elixir
103109
config :bun,
104110
version: "1.1.22",
105-
default: [
111+
assets: [args: [], cd: Path.expand("../assets", __DIR__)],
112+
js: [
106113
args: ~w(build js/app.js --outdir=../priv/static/assets --external /fonts/* --external /images/*),
107-
cd: Path.expand("../assets", __DIR__),
108-
env: %{}
114+
cd: Path.expand("../assets", __DIR__)
109115
]
110116
```
111117

@@ -116,16 +122,18 @@ For development, we want to enable watch mode. So find the `watchers`
116122
configuration in your `config/dev.exs` and add:
117123

118124
```elixir
119-
bun: {Bun, :install_and_run, [:default, ~w(--sourcemap=inline --watch)]}
125+
bun_js: {Bun, :install_and_run, [:js, ~w(--sourcemap=inline --watch)]}
120126
```
121127

122128
Note we are inlining source maps and enabling the file system watcher.
123129

124-
Finally, back in your `mix.exs`, make sure you have a `assets.deploy`
125-
alias for deployments, which will also use the `--minify` option:
130+
Finally, back in your `mix.exs`, let's configure Phoenix `assets` tasks
131+
to use `bun` instead:
126132

127133
```elixir
128-
"assets.deploy": ["bun default --minify", "phx.digest"]
134+
"assets.setup": ["bun.install --if-missing"],
135+
"assets.build": ["bun js"],
136+
"assets.deploy": ["bun js --minify", "phx.digest"],
129137
```
130138

131139
### Phoenix JS libraries
@@ -146,11 +154,15 @@ To tell bun about those libraries you will need to add the following to the `ass
146154
}
147155
}
148156
```
149-
and run:
157+
158+
and then configure `mix assets.setup` to install them:
159+
150160
```
151-
_build/bun install
161+
"assets.setup": ["bun.install --if-missing", "bun assets install"],
152162
```
153163

164+
Now run `mix assets.setup` and you are good to go!
165+
154166
### Replace esbuild with bun
155167

156168
You can use `bun` to build CSS with TailwindCSS, replacing both `esbuild` and the `tailwindcss` library in Elixir.
@@ -162,19 +174,19 @@ First, update `assets/package.json`:
162174
"phoenix": "workspace:*",
163175
"phoenix_html": "workspace:*",
164176
"phoenix_live_view": "workspace:*",
165-
"tailwindcss": "^3.4.9",
177+
"tailwindcss": "^4.1.0",
178+
"@tailwindcss/cli": "^4.1.0",
166179
"topbar": "^3.0.0"
167180
}
168181
```
169182

170-
Update your `config/config.exs`:
183+
Update your `:tailwind` configuration in `config/config.exs` to use `bun` instead:
171184

172185
```elixir
173186
config :bun,
174187
css: [
175188
args: ~w(run tailwindcss --input=css/app.css --output=../priv/static/assets/app.css),
176-
cd: Path.expand("../assets", __DIR__),
177-
env: %{}
189+
cd: Path.expand("../assets", __DIR__)
178190
]
179191
```
180192

@@ -189,9 +201,9 @@ bun_css: {Bun, :install_and_run, [:css, ~w(--watch)]}
189201
Update `mix.exs` aliases:
190202

191203
```elixir
192-
"assets.setup": ["bun.install"],
193-
"assets.build": ["bun default", "bun css"],
194-
"assets.deploy": ["bun default --minify", "bun css --minify", "phx.digest"]
204+
"assets.setup": ["bun.install --if-missing", "bun assets install"],
205+
"assets.build": ["bun js", "bun css"],
206+
"assets.deploy": ["bun css --minify", "bun js --minify", "phx.digest"],
195207
```
196208

197209
Remove the `tailwind` and `esbuild` dependencies from your `mix.exs`.
@@ -207,7 +219,7 @@ to add them to your application:
207219

208220
import topbar from "../vendor/topbar"
209221

210-
2. Call `_build/bun add topbar` inside your assets
222+
2. Call `mix bun assets add topbar` inside your assets
211223
directory and `bun` will be able to automatically
212224
pick them up:
213225

0 commit comments

Comments
 (0)