-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcompiler.ex
More file actions
57 lines (43 loc) · 1.83 KB
/
compiler.ex
File metadata and controls
57 lines (43 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
defmodule ElixirScript.Compiler do
@moduledoc """
The entry point for the ElixirScript compilation process.
Takes the given module(s) and compiles them and all modules
and functions they use into JavaScript
"""
@doc """
Takes either a module name or a list of module names as
the entry point(s) of an application/library. From there
it will determine which modules and functions are needed
to be compiled.
Available options are:
* `output`: The path of the generated JavaScript file.
If output is `nil`, then generated code is sent to standard out
If output is a path, the generated code placed in that path.
If path ends in `.js` then that will be the name of the file.
If a directory is given, file will be named `elixirscript.build.js`
* `root`: Optional root for imports of FFI JavaScript modules. Defaults to `.`.
"""
@spec compile(atom | [atom], []) :: nil
def compile(entry_modules, opts \\ []) do
opts = build_compiler_options(opts, entry_modules)
{:ok, pid} = ElixirScript.State.start_link()
entry_modules = List.wrap(entry_modules)
ElixirScript.FindUsedModules.execute(entry_modules, pid)
ElixirScript.FindUsedFunctions.execute(entry_modules, pid)
modules = ElixirScript.State.list_modules(pid)
ElixirScript.Translate.execute(modules, pid)
modules = ElixirScript.State.list_modules(pid)
result = ElixirScript.Output.execute(modules, pid, opts)
ElixirScript.State.stop(pid)
result
end
defp build_compiler_options(opts, entry_modules) do
default_options = Map.new
|> Map.put(:output, Keyword.get(opts, :output))
|> Map.put(:format, :es)
|> Map.put(:entry_modules, entry_modules)
|> Map.put(:root, Keyword.get(opts, :root, "."))
options = default_options
Map.put(options, :module_formatter, ElixirScript.ModuleSystems.ES)
end
end