|
| 1 | + <!DOCTYPE html> |
| 2 | +<html lang="en-us"> |
| 3 | +<meta charset="UTF-8"> |
| 4 | +<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 5 | +<meta content="" name="keywords"> |
| 6 | +<meta content="using with phoenix - Elixirscript" property="og:title"> |
| 7 | +<title>using with phoenix | Elixirscript</title> |
| 8 | +<link rel="stylesheet" href="https://elixirscript.github.io/css/style.css"> |
| 9 | +<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> |
| 10 | + |
| 11 | +<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/styles/github.min.css"> |
| 12 | + |
| 13 | + <section class="section"> |
| 14 | + <div class="container"> |
| 15 | + <nav class="nav"> |
| 16 | + <div class="nav-left"> |
| 17 | + <a class="nav-item" href="https://elixirscript.github.io"> |
| 18 | + <h1 class="title is-4">Elixirscript</h1> |
| 19 | + </a> |
| 20 | + </div> |
| 21 | + <div class="nav-right"> |
| 22 | + <nav class="nav-item level is-mobile"> |
| 23 | + <a class="level-item" href="/guide">Guide</a> |
| 24 | + <a class="level-item" href="https://github.com/elixirscript"> |
| 25 | + <span class="icon"> |
| 26 | + <i class="fa fa-github"></i> |
| 27 | + </span> |
| 28 | + </a> |
| 29 | + |
| 30 | + <a class="level-item" href="/index.xml"> |
| 31 | + <span class="icon"> |
| 32 | + <i class="fa fa-rss"></i> |
| 33 | + </span> |
| 34 | + </a> |
| 35 | + |
| 36 | + </nav> |
| 37 | + </div> |
| 38 | + </nav> |
| 39 | + </div> |
| 40 | +</section> |
| 41 | +<section class="section"> |
| 42 | + <div class="container"> |
| 43 | + <h1 class="title">using with phoenix</h1> |
| 44 | + |
| 45 | + <div class="content"> |
| 46 | + <p>This guide will walk through setting up a Phoenix project with Elixirscript. This guide assumes you have already created a Phoenix project</p> |
| 47 | + |
| 48 | +<p><strong>NOTE</strong>: This guide covers Phoenix 1.2. It will be updated when Phoenix 1.3 is released</p> |
| 49 | + |
| 50 | +<p>Update your mix.exs file to add the current version of elixirscript to your dependencies:</p> |
| 51 | + |
| 52 | +<pre><code class="language-elixir"> defp deps do |
| 53 | + [ |
| 54 | + #other deps |
| 55 | + {:elixir_script, "~> x.x.x"} |
| 56 | + ] |
| 57 | +</code></pre> |
| 58 | + |
| 59 | +<p>Run <code>mix deps.get</code>:</p> |
| 60 | + |
| 61 | +<pre><code class="language-bash">mix deps.get |
| 62 | +</code></pre> |
| 63 | + |
| 64 | +<p>Next, Add the <code>elixir_script</code> compiler to your list of mix compilers. Also add in the <code>elixir_script</code> configuration:</p> |
| 65 | + |
| 66 | +<pre><code class="language-elixir">def project do |
| 67 | + [app: :my_app, |
| 68 | + version: "0.0.1", |
| 69 | + elixir: "~> 1.2", |
| 70 | + elixirc_paths: elixirc_paths(Mix.env), |
| 71 | + compilers: [:phoenix, :gettext, :elixir_script] ++ Mix.compilers, |
| 72 | + build_embedded: Mix.env == :prod, |
| 73 | + start_permanent: Mix.env == :prod, |
| 74 | + deps: deps(), |
| 75 | + elixir_script: [ |
| 76 | + input: "web/static/elixirscript", |
| 77 | + output: "web/static/js/build" |
| 78 | + ] |
| 79 | + ] |
| 80 | +end |
| 81 | +</code></pre> |
| 82 | + |
| 83 | +<p>Elixirscript by default will looks for input in the <code>lib/elixirscript</code> directory. It will also by default output to <code>priv/elixirscript</code>. Update the input directory to <code>web/static/elixirscript</code>. Update the output directory to <code>web/static/js/build</code>. This lets it tie into Brunch’s pipeline.</p> |
| 84 | + |
| 85 | +<p>Next, update the watcher configuration to use the Elixirscript watcher:</p> |
| 86 | + |
| 87 | +<pre><code class="language-elixir"> watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin", |
| 88 | + cd: Path.expand("../", __DIR__)], mix: ["elixirscript.watch"]] |
| 89 | +</code></pre> |
| 90 | + |
| 91 | +<p>Whenever your Elixirscript code changes, the elixirscript compiler will recompile it.</p> |
| 92 | + |
| 93 | +<p>Create <code>app.ex</code> in the <code>web/static/elixirscript</code> directory</p> |
| 94 | + |
| 95 | +<pre><code class="language-bash">touch web/static/elixirscript/app.ex |
| 96 | +</code></pre> |
| 97 | + |
| 98 | +<p>For this example, write a simple module that will write <code>Hello, world</code> to the console on start:</p> |
| 99 | + |
| 100 | +<pre><code class="language-elixir">defmodule App do |
| 101 | + |
| 102 | + def start(_type, _args) do |
| 103 | + :console.log("Hello, world") |
| 104 | + end |
| 105 | + |
| 106 | +end |
| 107 | +</code></pre> |
| 108 | + |
| 109 | +<p>Finally, update <code>web/static/js/app.js</code> to start your Elixirscript app:</p> |
| 110 | + |
| 111 | +<pre><code class="language-javascript">import Elixir from './build/Elixir.App'; |
| 112 | +Elixir.start(Elixir.App, []) |
| 113 | +</code></pre> |
| 114 | + |
| 115 | +<p>The empty array is list of initial arguments for your app.</p> |
| 116 | + |
| 117 | +<p>If you run <code>mix compile</code>, you should see a JavaScript file, <code>Elixir.App.js</code> in your <code>web/static/js/build</code> directory.</p> |
| 118 | + |
| 119 | +<p>If you run <code>mix phoenix.server</code>, open your browser, and then open your console, you should see <code>Hello, world</code>. Any changes should cause a reload</p> |
| 120 | + |
| 121 | + </div> |
| 122 | + </div> |
| 123 | +</section> |
| 124 | +<section class="section"> |
| 125 | + <div class="container has-text-centered"> |
| 126 | + <p></p> |
| 127 | + </div> |
| 128 | +</section> |
| 129 | +<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/highlight.min.js" integrity="sha256-KbfTjB0WZ8vvXngdpJGY3Yp3xKk+tttbqClO11anCIU=" crossorigin="anonymous"></script> |
| 130 | + |
| 131 | +<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/elixir.min.js"></script> |
| 132 | + |
| 133 | +<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/javascript.min.js"></script> |
| 134 | + |
| 135 | +<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/languages/bash.min.js"></script> |
| 136 | + |
| 137 | +<script>hljs.initHighlightingOnLoad();</script> |
| 138 | + |
0 commit comments