-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathcompiler.ex
More file actions
59 lines (46 loc) · 1.62 KB
/
compiler.ex
File metadata and controls
59 lines (46 loc) · 1.62 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
58
59
defmodule ElixirScript.Compiler do
@moduledoc """
Compiles the given modules to 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.
"""
@spec compile([atom], []) :: nil
def compile(entry_modules, opts \\ []) do
opts = build_compiler_options(opts, entry_modules)
{:ok, pid} = ElixirScript.State.start_link(opts)
entry_modules = List.wrap(entry_modules)
IO.puts "Finding used modules"
ElixirScript.FindUsedModules.execute(entry_modules, pid)
IO.puts "Finding used functions"
ElixirScript.FindUsedFunctions.execute(entry_modules, pid)
IO.puts "Compiling"
modules = ElixirScript.State.list_modules(pid)
ElixirScript.Translate.execute(modules, pid)
IO.puts "Building Output"
modules = ElixirScript.State.list_modules(pid)
result = ElixirScript.Output.execute(modules, pid)
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, Keyword.get(opts, :format, :es))
|> Map.put(:entry_modules, entry_modules)
options = default_options
Map.put(options, :module_formatter, get_module_formatter(options[:format]))
end
defp get_module_formatter(:umd) do
ElixirScript.ModuleSystems.UMD
end
defp get_module_formatter(:common) do
ElixirScript.ModuleSystems.Common
end
defp get_module_formatter(_) do
ElixirScript.ModuleSystems.ES
end
end