# Default Phoenix assets without Node.js

**URL:** <https://forum.elixirforum.com/t/default-phoenix-assets-without-node-js/31424>\
**Category:** Questions\
**Tags:** book-programming-phoenix, phoenix, troubleshooting, assets, nodejs, learning\
**Created:** [May 11, 2020, 11:09pm UTC](https://forum.elixirforum.com/t/default-phoenix-assets-without-node-js/31424 "2020-05-11T23:09:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Orzech](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/orzech/32/19906_2.png) [@Orzech](https://forum.elixirforum.com/u/Orzech)\
**Post date:** [May 11, 2020, 11:09pm UTC](https://forum.elixirforum.com/t/default-phoenix-assets-without-node-js/31424/1 "2020-05-11T23:09:22Z")

</div>

I’m learning Phoenix with “Programming Phoenix 1.4” book, but I want to avoid using Node.js altogether. I’d also like to track assets in my repo.

The `mix phx.new --no-webpack` task not only skips setting webpack up, but also skips default assets creation.

Shall I just download [phoenix.js](https://github.com/phoenixframework/phoenix/blob/master/assets/js/phoenix.js) and [phoenix\_html.js](https://github.com/phoenixframework/phoenix_html/blob/master/priv/static/phoenix_html.js) and concatenate them into `app.js` (and do similar thing with `css` files etc.), or maybe there’s an easier way?

---

<div class="post-metadata">

**Author:** ![Orzech](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/orzech/32/19906_2.png) [@Orzech](https://forum.elixirforum.com/u/Orzech)\
**Post date:** [May 12, 2020, 9:55pm UTC](https://forum.elixirforum.com/t/default-phoenix-assets-without-node-js/31424/2 "2020-05-12T21:55:53Z")

</div>

In case anyone stumbles upon the same problem:  
I’ve copied static and css assets from freshly generated Phoenix project and js files from phoenix deps and then added basic task to “compile” them. You can see it in [this commit](https://gitlab.com/0rzech/rumbl/-/commit/a43912d6e82160b2e684f19cbcc6d0d67928d841).

---

<div class="post-metadata">

**Author:** ![stefanchrobot](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/stefanchrobot/32/7657_2.png) [@stefanchrobot](https://forum.elixirforum.com/u/stefanchrobot)\
**Post date:** [May 13, 2020, 6:41am UTC](https://forum.elixirforum.com/t/default-phoenix-assets-without-node-js/31424/3 "2020-05-13T06:41:40Z")

</div>

I’m doing similar thing, although my goal is not to avoid Node (since that cannot be avoided for JS and CSS postprocessing), but to avoid Webpack and other bundlers. I’m using `make` instead of Mix tasks. Here’s my `assets/Makefile` so far:

```elixir
# Make targets for assets.

css:
		@echo 'Building CSS...'
		@mkdir -p ../priv/static/css
		@cat \
			vendor/nprogress-0.2.0/nprogress.css \
			css/app.css > ../priv/static/css/app.css

js:
		@echo 'Building JS...'
		@mkdir -p ../priv/static/js
		@cat \
			../deps/phoenix/priv/static/phoenix.js \
			../deps/phoenix_html/priv/static/phoenix_html.js \
			../deps/phoenix_live_view/priv/static/phoenix_live_view.js \
			vendor/nprogress-0.2.0/nprogress.js \
			js/app.js > ../priv/static/js/app.js

static:
		@echo 'Building static...'
		@cp -R static/* ../priv/static/

clean:
		@echo 'Cleaning assets...'
		@rm -rf ../priv/static/*

build: css js static

.PHONY: css js static

```

I’ll be adding CSS and JS minification later.  
Here’s my watchers config (using `watchexec`, dynamic endpoint config):

```elixir
defmodule MyApp.Endpoint do
  use Phoenix.Endpoint, otp_app: :my_app
  
  # ...

  # Dynamic endpoint configuration.
  # See: https://hexdocs.pm/phoenix/Phoenix.Endpoint.html#module-endpoint-configuration
  def init(:supervisor, config) do
    {:ok, endpoint_config(config)}
  end

  defp endpoint_config(config) do
    config
    |> Keyword.merge(
      # ...
    )
    |> Keyword.merge(
      case MyApp.app_env() do
        "development" ->
          [
            # The watchers configuration can be used to run external
            # watchers to your application. For example, we use it
            # with webpack to recompile .js and .css sources.
            watchers: [
              watcher(["watchexec", "-e", "css", "make", "css"]),
              watcher(["watchexec", "-e", "js", "make", "js"]),
              watcher(["watchexec", "-e", "png,ico,txt", "make", "static"])
            ],
            # ...
          ]
          
        # ...
      end
    )
  end

  # Returns a watcher that runs in the assets/ dir by running the specified command with args.
  # Uses the assets/wrapper script to work around commands that don't properly watch STDIN.
  defp watcher(args) do
    working_dir = Path.expand("../../assets", __DIR__ )
    # The executable is run via System.cmd which expects the executable
    # to be available in PATH or an absolute path.
    {Path.join(working_dir, "wrapper"), args ++ [cd: working_dir]}
  end
end

```

and the wrapper script:

```elixir
#!/usr/bin/env bash

# See https://hexdocs.pm/elixir/Port.html#module-zombie-operating-system-processes

# Start the program in the background
exec "$@" &
pid1=$!

# Silence warnings from here on
exec >/dev/null 2>&1

# Read from stdin in the background and
# kill running program when stdin closes
exec 0<&0 $(
  while read; do :; done
  kill -KILL $pid1
) &
pid2=$!

# Clean up
wait $pid1
ret=$?
kill -KILL $pid2
exit $ret

```

---

<div class="post-metadata">

**Author:** ![Orzech](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/orzech/32/19906_2.png) [@Orzech](https://forum.elixirforum.com/u/Orzech)\
**Post date:** [May 14, 2020, 12:13am UTC](https://forum.elixirforum.com/t/default-phoenix-assets-without-node-js/31424/4 "2020-05-14T00:13:55Z")

</div>

Thanks for sharing. I took the simplest way possible, though not the most convenient for sure.

AFAIU, to some extent, you can avoid Node.js even when postprocessing text assets, with eg.:

- [csscompressor](https://github.com/sprymix/csscompressor) (Python) - as name suggests.
- [esbuild](https://github.com/evanw/esbuild) (Go) - js/ts minifier and bundler.
- [jsmin](https://github.com/tikitu/jsmin/) (Python) - as name suggests.
- [rCSSmin](https://github.com/ndparker/rcssmin) (Python) - as name suggests.
- … _(I can’t put more than 5 links in a single post yet)_

---

<div class="post-metadata">

**Author:** ![Orzech](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/orzech/32/19906_2.png) [@Orzech](https://forum.elixirforum.com/u/Orzech)\
**Post date:** [May 14, 2020, 12:14am UTC](https://forum.elixirforum.com/t/default-phoenix-assets-without-node-js/31424/5 "2020-05-14T00:14:34Z")

</div>

- [rJSmin](https://github.com/ndparker/rjsmin) (Python) - as name suggests.
- [SlimIt](https://github.com/rspivak/slimit) (Python) - js minifier.
- [webassets](https://github.com/miracle2k/webassets) (Python) - js/css asset manager.
- [YUI Compressor](https://github.com/yui/yuicompressor) (Java) - js/css minifier.

YUI Compressor is the only one I have used, but it’s probably dead now. The newest kid on the block seems to be esbuild.
