|
| 1 | ++++ |
| 2 | +date = "2017-03-05T14:27:53-06:00" |
| 3 | +title = "using with phoenix" |
| 4 | ++++ |
| 5 | + |
| 6 | +This guide will walk through setting up a Phoenix project with Elixirscript. This guide assumes you have already created a Phoenix project |
| 7 | + |
| 8 | +**NOTE**: This guide covers Phoenix 1.2. It will be updated when Phoenix 1.3 is released |
| 9 | + |
| 10 | +Update your mix.exs file to add the current version of elixirscript to your dependencies: |
| 11 | + |
| 12 | +```elixir |
| 13 | + defp deps do |
| 14 | + [ |
| 15 | + #other deps |
| 16 | + {:elixir_script, "~> x.x.x"} |
| 17 | + ] |
| 18 | +``` |
| 19 | + |
| 20 | +Run `mix deps.get`: |
| 21 | + |
| 22 | +```bash |
| 23 | +mix deps.get |
| 24 | +``` |
| 25 | + |
| 26 | +Next, Add the `elixir_script` compiler to your list of mix compilers. Also add in the `elixir_script` configuration: |
| 27 | + |
| 28 | +```elixir |
| 29 | +def project do |
| 30 | + [app: :my_app, |
| 31 | + version: "0.0.1", |
| 32 | + elixir: "~> 1.2", |
| 33 | + elixirc_paths: elixirc_paths(Mix.env), |
| 34 | + compilers: [:phoenix, :gettext, :elixir_script] ++ Mix.compilers, |
| 35 | + build_embedded: Mix.env == :prod, |
| 36 | + start_permanent: Mix.env == :prod, |
| 37 | + deps: deps(), |
| 38 | + elixir_script: [ |
| 39 | + input: "web/static/elixirscript", |
| 40 | + output: "web/static/js/build" |
| 41 | + ] |
| 42 | + ] |
| 43 | +end |
| 44 | +``` |
| 45 | + |
| 46 | +Elixirscript by default will looks for input in the `lib/elixirscript` directory. It will also by default output to `priv/elixirscript`. Update the input directory to `web/static/elixirscript`. Update the output directory to `web/static/js/build`. This lets it tie into Brunch's pipeline. |
| 47 | + |
| 48 | +Next, update the watcher configuration to use the Elixirscript watcher: |
| 49 | + |
| 50 | +```elixir |
| 51 | + watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin", |
| 52 | + cd: Path.expand("../", __DIR__)], mix: ["elixirscript.watch"]] |
| 53 | +``` |
| 54 | + |
| 55 | +Whenever your Elixirscript code changes, the elixirscript compiler will recompile it. |
| 56 | + |
| 57 | +Create `app.ex` in the `web/static/elixirscript` directory |
| 58 | + |
| 59 | +```bash |
| 60 | +touch web/static/elixirscript/app.ex |
| 61 | +``` |
| 62 | + |
| 63 | +For this example, write a simple module that will write `Hello, world` to the console on start: |
| 64 | + |
| 65 | +```elixir |
| 66 | +defmodule App do |
| 67 | + |
| 68 | + def start(_type, _args) do |
| 69 | + :console.log("Hello, world") |
| 70 | + end |
| 71 | + |
| 72 | +end |
| 73 | +``` |
| 74 | + |
| 75 | +Finally, update `web/static/js/app.js` to start your Elixirscript app: |
| 76 | + |
| 77 | +```javascript |
| 78 | +import Elixir from './build/Elixir.App'; |
| 79 | +Elixir.start(Elixir.App, []) |
| 80 | +``` |
| 81 | + |
| 82 | +The empty array is list of initial arguments for your app. |
| 83 | + |
| 84 | + |
| 85 | +If you run `mix compile`, you should see a JavaScript file, `Elixir.App.js` in your `web/static/js/build` directory. |
| 86 | + |
| 87 | +If you run `mix phoenix.server`, open your browser, and then open your console, you should see `Hello, world`. Any changes should cause a reload |
| 88 | + |
0 commit comments