|
1 | 1 | defmodule ElixirScript do |
2 | | - @moduledoc false |
| 2 | + @moduledoc """ |
| 3 | +
|
| 4 | + ElixirScript acts as a mix compiler. This means that when you execute `mix compile`, |
| 5 | + ElixirScript's compiler will run as well. Make sure to add ElixirScript to your |
| 6 | + list of compilers in mix.exs. |
| 7 | +
|
| 8 | + ElixirScript must be told which modules to use as the entry to your ElixirScript application. |
| 9 | + This is done by adding an `elixir_script` key to your project configuration whose value is a keyword list of options. |
| 10 | + Add an `input` key and make the value either the name of a module or a list of modules |
| 11 | + that are the entry modules you wish to compile to JavaScript. ElixirScript will use |
| 12 | + those modules to find what other modules and functions it needs to convert to JavaScript. |
| 13 | + ElixirScript by default places output in `priv/elixir_script/build`. If you wish to change this, |
| 14 | + add an `output` key to your ElixirScript configuration. |
| 15 | +
|
| 16 | + An example configuration for a project is shown below |
| 17 | +
|
| 18 | + ``` elixir |
| 19 | + def project do |
| 20 | + [ |
| 21 | + app: :my_app, |
| 22 | + version: "0.1.0", |
| 23 | + elixir: "~> 1.0", |
| 24 | + deps: deps, |
| 25 | + # Add elixir_script as a compilter |
| 26 | + compilers: Mix.compilers ++ [:elixir_script], |
| 27 | + # Our elixir_script configuration |
| 28 | + elixir_script: [ |
| 29 | + # Entry module. Can also be a list of modules |
| 30 | + input: MyEntryModule, |
| 31 | + # Output path. Either a path to a js file or a directory |
| 32 | + output: "priv/elixir_script/build/Elixir.App.js" |
| 33 | + ] |
| 34 | + ] |
| 35 | + end |
| 36 | + ``` |
| 37 | +
|
| 38 | + Available options are: |
| 39 | +
|
| 40 | + * `input` (required): The entry module(s) for your application or library |
| 41 | +
|
| 42 | + * `output`: The path of the generated JavaScript file. (defaults to `priv/elixir_script/build`) |
| 43 | +
|
| 44 | + If path ends in `.js` then that will be the name of the file. If a directory is given, |
| 45 | + file will be named `Elixir.App.js` |
| 46 | +
|
| 47 | + * `root`: Optional root for imports of FFI JavaScript modules. Defaults to `.`. If using output directly in a browser, you may want to make it something like `/js` or some uri. |
| 48 | +
|
| 49 | +
|
| 50 | + Now run `mix compile` and you should see a JavaScript file named `Elixir.App.js` in the `priv/elixir_script/build/` directory. ElixirScript outputs JavaScript in the ES Module format. If your browser supports it, you can include the output in a script tag with the type "module" |
| 51 | +
|
| 52 | + ```html |
| 53 | + <script type="module"> |
| 54 | + import Elixir from '/js/Elixir.App.js' |
| 55 | + const myInitialArgs = [] |
| 56 | +
|
| 57 | + Elixir.start(Elixir.MyEntryModule, myInitialArgs) |
| 58 | + </script> |
| 59 | + ``` |
| 60 | +
|
| 61 | + If your browser does not yet support ES modules directly, use a tool such as [webpack](https://webpack.js.org/) or [brunch](http://brunch.io/) to convert it into something that can be used in the browser |
| 62 | +
|
| 63 | + ### JavaScript Interop |
| 64 | +
|
| 65 | + Check out the [JavaScript Interoperability](JavascriptInterop.html) documentation |
| 66 | +
|
| 67 | + ### Limitations |
| 68 | +
|
| 69 | + ElixirScript does not support `receive` or any of OTP at this time. |
| 70 | + """ |
3 | 71 | end |
0 commit comments