ElixirScript DocumentationBuild

The goal is to convert a subset (or full set) of Elixir code to JavaScript, providing the ability to write JavaScript in Elixir. This is done by taking the Elixir AST and converting it into JavaScript AST and then to JavaScript code. This is done using the Elixir-ESTree library.

Requirements

Usage

ElixirScript can be used in the following ways:

Development

Clone the repo

git clone git@github.com:bryanjos/elixirscript.git

Get dependencies

mix deps.get
npm install

Compile

mix compile

Test

mix test

Usage

  usage: ex2js <input> [options]

  <input> path to elixir files or
          the elixir code string if the -ex flag is used

  options:
  -o  --output [path]   places output at the given path
  -ex --elixir          read input as elixir code string
  -r  --root [path]     root path for standard libs
  -h  --help            this message

A note on -r. The standard lib modules are included in the output as elixir.js. They are by default included by importing them like so

import {...} from &#39;elixir&#39;

Depending on your setup that may not work. With -r you can specify the root path that will be prepended to the default path.

Ex.

mix ex2js "my/elixir/dir/**/*.ex" -r "js" -o my/js/dir

Will make the standard lib imports look like so

import {...} from &#39;js/elixir&#39;

Examples

Macros

Macros can only be used when using ElixirScript as a library if the Macros are loaded into the current environment or if you give it a custom environment with the env option

#module with macro defined
defmodule Math do
  defmacro squared(x) do
    quote do
      unquote(x) * unquote(x)
    end
  end
end

#create an env with the module required if not already in the current enviroment
def make_custom_env do
  require Logger
  require Math
  __ENV__
end


#Now pass it to `ElixirScript.compile`
ElixirScript.compile("""
  Math.squared(1)
""", env: make_custom_env)

# returns ["1 * 1"]

You should be able to use use in modules now as well, but modules that have __using__ macros must also be require'd so that they can be expanded.

Using JavaScript libraries

You can use alias, import, and require as you would in Elixir (sans macros).

For JavaScript modules, use JS.import

JS.import A, "a" #translates to "import {default as A} from &#39;a&#39;"

JS.import [A, B, C], "a" #translates to "import {A, B, C} from &#39;a&#39;"

Limitations

Not all of the Kernel.SpecialForms module is defined

The following aren't defined (yet):

The following are defined but incomplete:

Most of the Standard Library isn't defined yet

A lot of functions in the Kernel module are implemented. The Enum, Atom, List, Tuple, Logger, and Range modules are either fully defined are not complete. The rest still need to be implemented. Some modules like System or File may not be useful or function in the browser and may end up being only useful when using ElixirScript outside of the browser.

Example projects

Using with Brunch

There is a plugin for using ElixirScript in your Brunch project here