Phoenix with Elm ããã£ã¦ã¿ã Part1
Phoenix With Elm ããããã¦ã¿ã¾ããã®ã§ã¡ã¢æ¸ãã¨ãã¦æ®ãã¦ããã¾ãã
Phoenix With Elmå
¨ä½ãæ°ã«ãªãæ¹ã¯ä¸è¨ãããããã ããã
Phoenix with Elm
Phoenix with Elmのリポジトリ
#å®è¡ç°å¢
OSï¼OS X Yosemite
Erlang v19.0
Elixir v1.3.2
Phoenix v1.2.0
Elm v0.7.1
Node v6.2.2
Elmã¯JavaScriptã«ã³ã³ãã¤ã«ãããé¢æ°åè¨èªã§ãã
Elmèªä½ã¯æ¢ã«ã¤ã³ã¹ãã¼ã«ããã¦ãããã¨ãåæã¨ãã¦é²ãã¦ããã¾ãã
Elmlang.org ã«ã¦ã¤ã³ã¹ãã¼ã«æ¹æ³ã確èªãã¦ããã ããã°ãç¹ã«åé¡ãªãã¤ã³ã¹ãã¼ã«ã¯çµãããã¨æãã¾ãã
ã¾ãã¯ãPhoenixã®ã¢ããªã±ã¼ã·ã§ã³ãä½ãã¾ãããã
ããã¯ç¹ã«ãã«ãããã¨ã¯ãªãããã¤ãéãä½ã£ã¦ããã ãã¦åé¡ã¯ããã¾ããã
$ mix phoenix.new phoenix_with_elm $ cd phoenix_with_elm/ $ mix ecto.create
ç¶ãã¦ãElmã®ã³ã¼ããé
ç½®ããå ´æã®ä½æãè¡ãã¾ãã
webé
ä¸ã«elmãã£ã¬ã¯ããªãä½æãããã®ä¸ã«ã³ã¼ããä½æãã¦ããããã§ãã
$ mkdir web/elm
web/elm é
ä¸ã« SeatSaver.elm ã¨ããååã§Elmã®ã³ã¼ããè¨è¼ãã¾ãã
Elmã«ã¯ãHtmlã®ã³ã¢ã©ã¤ãã©ãªã¨ã㦠htmlã¨ããã©ã¤ãã©ãªãããã¾ãã
ãã¡ããã¤ã³ãã¼ããã¦ãHello from Elm ã¨ããæååã main ã«æ¸¡ãã¦ããããã§ãã
web/elm/SeatSaver.elm
module SeatSaver exposing (..) import Html main = Html.text "Hello from Elm"
ãã¦ãelmèªä½ã®ã³ã¼ãã®è¨è¼ã¯çµããã¾ããã®ã§ãjson ã®ç¨æããã¦ããã¾ãã
jsonã«ã¦elmããã±ã¼ã¸ã®ç®¡çãè¡ãããã®è¨è¼ãè¡ã£ã¦ããã¾ãããã
package.json
{ "repository": {}, "license": "MIT", "scripts": { "deploy": "brunch build --production", "watch": "brunch watch --stdin" }, "dependencies": { "babel-brunch": "~6.0.0", "brunch": "~2.1.3", "elm-brunch": "~0.7.0", "clean-css-brunch": "~1.8.0", "phoenix": "file:deps/phoenix", "phoenix_html": "file:deps/phoenix_html" }, "devDependencies": { "babel-brunch": "~6.0.0", "brunch": "2.7.4", "clean-css-brunch": "~2.0.0", "css-brunch": "~2.0.0", "elm": "^0.17.1", "javascript-brunch": "~2.0.0", "uglify-js-brunch": "~2.0.1" } }
brunch-config.json
... // Phoenix paths configuration paths: { // Dependencies and current project directories to watch watched: [ "web/static", "test/static", "web/elm/SeatSaver.elm" ], // Where to compile files to public: "priv/static" }, // Configure your plugins plugins: { elmBrunch: { elmFolder: "web/elm", mainModules: ["SeatSaver.elm"], outputFolder: "../static/vendor" }, babel: { // Do not use ES6 compiler in vendor code ignore: [/web\/static\/vendor/] } }, ...
ãã¦ãããã±ã¼ã¸ã®ç®¡çã®ããã®è¨è¼ãè¡ãã¾ããã®ã§ã次ã¯HTMLå´ã®æºåãè¡ã£ã¦ããã¾ãããã
Phoenixã®åºæ¬ãã¼ã¸ã§ãã index.html.eexã®å
容ãå¤æ´ãã¦ããã¾ãã
èªåçæãããå
容ãåé¤ããä¸è¨ãè¨è¼ãã¾ãã
web/templates/page/index.html.eex
<div id="elm-main"></div>
ãã¦ãç¶ãã¦app.jså
é¨ã«elmã®èªã¿è¾¼ã¿ã®è¨è¼ãè¡ãã¾ãã
elmã¯elmã§è¨è¼ãããã³ã¼ããJavaScriptã«å¤æ´ãã¾ãã®ã§ããã®å
容ãapp.jsã«ã¦èªã¿è¾¼ãã§å®è¡ãã¦ããã¾ãã
web/static/js/app.js
... const elmDiv = document.getElementById('elm-main') , elmApp = Elm.SeatSaver.embed(elmDiv)
æå¾ã«ãã³ãã¬ã¼ãã®è¨è¼ãè¡ã£ã¦å®äºã§ãã
web/templates/layout/app.html.eex
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content=""> <meta name="author" content=""> <title>Hello SeatSaver!</title> <link rel="stylesheet" href="<%= static_path(@conn, "/css/app.css") %>"> </head> <body> <div class="container"> <main role="main"> <%= render @view_module, @view_template, assigns %> </main> </div> <!-- /container --> <script src="<%= static_path(@conn, "/js/app.js") %>"></script> </body> </html>
ãã¦ãæå¾ã«å®è¡ããã°ãããâ¦ãã¨æãã¾ããããããã¯ããã¾ããã§ããã
ã©ãããbabel2015ã¨babel2016ã®å¯¾å¿ãããªããã°Elmã®ã³ã³ãã¤ã«ã«å¤±æãã¦ãã¾ããããªã®ã§ãnpm installã«ã¦å®è¡ãã¾ãããã
ç°å¢ãå¤ãã£ããããã¨å®éã«è©¦ãã¦ã¿ãªãã¨ããããªããã¨ã§ããã
compiled seatsaver.js ã¨è¡¨ç¤ºãããseatsaver.js ãã³ã³ãã¤ã«ãããã°ãåé¡ã¯ãªãã¯ãã§ãã
$ npm install babel-preset-es2015 $ npm install babel-preset-es2016 $ iex -S mix phoenix.server Erlang/OTP 19 [erts-8.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [info] Running PhoenixWithElm.Endpoint with Cowboy using http://localhost:4000 Interactive Elixir (1.3.2) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> Elm compile: SeatSaver.elm, in web/elm, to ../static/vendor/seatsaver.js 19 Sep 20:40:28 - info: compiled 8 files into 2 files, copied 3 in 1.6 sec 19 Sep 20:40:29 - info: compiled seatsaver.js and 5 cached files into app.js in 175ms
http://localhost:4000 ã«ã¢ã¯ã»ã¹ãã Hello from Elm ã¨è¡¨ç¤ºããã¦ããã°æåã§ãã
Elmèªä½ã®ã³ã¼ãã®æ¸ãæ¹ã¯ guide.elm-lang.org ãã¿ã¦ããã ããã°ãã©ã®ããã«è¨è¼ãã¦ããã®ããããããã¨æãã¾ãã
ããããå°ãã㤠Phoenix with Elmãç¶ãã¦åèè³æãããã¦ããããããã«é å¼µãã¾ãã