Assuming you put gatsby in a folder called client in the root of your elixir app, something like the below may get you started.
in mix.exs (got the compile assets idea from https://github.com/bitwalker/distillery-aws-example/blob/master/mix.exs):
- add dep: {:plug_static_index_html, â~> 1.0â},
- then under aliases:
defp aliases do
[
"ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
test: ["ecto.create --quiet", "ecto.migrate", "test"],
compile: ["compile", &compile_assets/1],
clean: ["clean", &clean_assets/1]
]
end
defp compile_assets(_args) do
unless File.exists?(Path.join(System.cwd(), "priv/static")) do
prod =
if Mix.env() == :prod do
"true"
else
"false"
end
IO.puts("compiling client app with yarn")
System.cmd("yarn", ["run", "export"], cd: "client", env: [{"PROD", prod}])
end
end
defp clean_assets(_args) do
static_dir = Path.join(System.cwd(), "priv/static")
IO.puts("removing static dir")
IO.puts(static_dir)
File.rm_rf!(static_dir)
end
in endpoint.ex
plug Plug.Static.IndexHtml,
at: "/"
plug(
Plug.Static,
at: "/",
from: {:yourappname, "priv/static"},
gzip: true
)
/client/package.json:
"scripts": {
"export": "yarn run build && gatsby export -o ../priv/static"
}
The above script for package.json is probably not accurate for gatsby. But you will want a script called âexportâ or something similar to compile gatsby, and tell it the directory to compile to.
then you could run:
mix clean
mix compile
mix phx.digest
mix phx.server
You will not have hot code reloading (youâd need to run gatsby in dev mode for that).
But gatsbyâs index.html file should be served from localhost:4000/
Not 100% if the above will work as is, but it may start you off. Iâm doing something similar for next.js