Last active
October 10, 2018 21:17
-
-
Save myobie/2dd66e9252704c14d96e34d480962047 to your computer and use it in GitHub Desktop.
Automatically build and digest assets for a phoenix application when building an elixir release with distillery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rel/config.exs | |
# ... | |
environment :prod do | |
# ... | |
plugin(Example.Phoenix) | |
end | |
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rel/plugins/phoenix.exs | |
defmodule Example.Phoenix do | |
use Mix.Releases.Plugin | |
def before_assembly(_release, _opts) do | |
info("Deploying assets") | |
with {_output, 0} <- npm_install(), | |
{_output, 0} <- npm_deploy(), | |
_ = phx_digest() do | |
nil | |
else | |
{_output, error_code} -> | |
throw({:error, :npm_deploy_failed, error_code}) | |
end | |
end | |
def after_assembly(_release, _opts), do: nil | |
def before_package(_release, _opts), do: nil | |
def after_package(_release, _opts), do: nil | |
def after_cleanup(_release, _opts), do: nil | |
defp npm_install do | |
System.cmd( | |
"npm", | |
["install"], | |
cd: "assets", | |
into: IO.stream(:stdio, :line) | |
) | |
end | |
defp npm_deploy do | |
System.cmd( | |
"npm", | |
["run", "deploy"], | |
cd: "assets", | |
into: IO.stream(:stdio, :line) | |
) | |
end | |
defp phx_digest, do: Mix.Task.run("phx.digest") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment