Skip to content

Commit 3c8dcd1

Browse files
committed
Move manifest writing to the mix compiler
1 parent cda84c3 commit 3c8dcd1

5 files changed

Lines changed: 39 additions & 33 deletions

File tree

lib/elixir_script/compiler.ex

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ defmodule ElixirScript.Compiler do
3232
FindUsedModules,
3333
FindUsedFunctions,
3434
Output,
35-
Manifest,
3635
}
3736
alias ElixirScript.ModuleSystems.ES
3837
alias Kernel.ParallelCompiler
3938

40-
@spec compile(atom | [atom] | binary, []) :: [{atom, map}]
39+
@spec compile(atom | [atom] | binary, []) :: map
4140
def compile(path, opts \\ [])
4241

4342
def compile(path, opts) when is_binary(path) do
@@ -87,10 +86,7 @@ defmodule ElixirScript.Compiler do
8786

8887
State.stop(pid)
8988

90-
manifest_path = Path.join(Mix.Project.manifest_path(), ".compile.elixir_script")
91-
Manifest.write_manifest(manifest_path, modules, opts)
92-
93-
result
89+
transform_output(modules, result, opts)
9490
end
9591

9692
defp build_compiler_options(opts) do
@@ -107,4 +103,25 @@ defmodule ElixirScript.Compiler do
107103
defp on_module_compile(pid, _file, module, beam) do
108104
State.put_in_memory_module(pid, module, beam)
109105
end
106+
107+
defp transform_output(modules, compiled_js, opts) do
108+
output_path = if opts.output == nil or opts.output == :stdout do
109+
""
110+
else
111+
Path.dirname(opts.output)
112+
end
113+
114+
Enum.reduce(modules, %{}, fn {module, info}, current_data ->
115+
info = %{
116+
references: info.used_modules,
117+
last_modified: info.last_modified,
118+
beam_path: Map.get(info, :beam_path),
119+
source: Map.get(info, :file),
120+
js_path: Path.join(output_path, "#{module}.js"),
121+
js_code: Keyword.get(compiled_js, module)
122+
}
123+
124+
Map.put(current_data, module, info)
125+
end)
126+
end
110127
end

lib/elixir_script/manifest.ex

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,10 @@ defmodule ElixirScript.Manifest do
66

77
end
88

9-
@spec write_manifest(binary, [{atom, map}], map) :: :ok
10-
def write_manifest(manifest_path, modules, opts) do
11-
output_path = if opts.output == nil or opts.output == :stdout do
12-
""
13-
else
14-
Path.dirname(opts.output)
15-
end
16-
9+
@spec write_manifest(binary, map) :: :ok
10+
def write_manifest(manifest_path, modules) do
1711
data = Enum.reduce(modules, %{}, fn {module, info}, current_data ->
18-
info = %{
19-
references: info.used_modules,
20-
last_modified: info.last_modified,
21-
beam_path: Map.get(info, :beam_path),
22-
source: Map.get(info, :file),
23-
js_path: Path.join(output_path, "#{module}.js")
24-
}
25-
26-
Map.put(current_data, module, info)
12+
Map.put(current_data, module, Map.drop(info, :js_code))
2713
end)
2814

2915
data = :erlang.term_to_binary(data, [:compressed])

lib/elixir_script/passes/output.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule ElixirScript.Output do
77
@doc """
88
Takes outputs the JavaScript code in the specified output
99
"""
10-
@spec execute([atom], pid, map) :: any
10+
@spec execute([atom], pid, map) :: [{atom, binary}]
1111
def execute(modules, pid, opts) do
1212
prepared_modules = modules
1313
|> Enum.filter(fn {_, info} -> Map.has_key?(info, :js_ast) end)
@@ -71,12 +71,10 @@ defmodule ElixirScript.Output do
7171
|> String.replace(".", "$")
7272
end
7373

74-
defp output(code, _, nil, _) do
75-
code
76-
end
77-
78-
defp output(code, _, :stdout, _) do
74+
defp output(code, module, nil, _), do: {module, code}
75+
defp output(code, module, :stdout, _) do
7976
IO.puts(code)
77+
{module, code}
8078
end
8179

8280
defp output(code, module, path, js_modules) do
@@ -99,6 +97,8 @@ defmodule ElixirScript.Output do
9997

10098
copy_bootstrap_js(output_dir)
10199
File.write!(file_name, code)
100+
101+
{module, code}
102102
end
103103

104104
defp copy_bootstrap_js(directory) do

lib/mix/tasks/compile.elixir_script.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Mix.Tasks.Compile.ElixirScript do
22
use Mix.Task
3+
alias ElixirScript.Manifest
34

45
@recursive true
56
@manifest ".compile.elixir_script"
@@ -43,7 +44,9 @@ defmodule Mix.Tasks.Compile.ElixirScript do
4344

4445
defp do_compile() do
4546
{input, opts} = get_compiler_params()
46-
ElixirScript.Compiler.compile(input, opts)
47+
result = ElixirScript.Compiler.compile(input, opts)
48+
49+
Manifest.write_manifest(manifest(), result)
4750
end
4851

4952
def clean do

test/compiler_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ defmodule ElixirScript.Compiler.Test do
33

44
test "Can compile one entry module" do
55
result = ElixirScript.Compiler.compile(Version)
6-
assert is_binary(hd(result))
6+
assert result |> Map.to_list |> hd |> elem(1) |> Map.get(:js_code) |> is_binary
77
end
88

99
test "Can compile multiple entry modules" do
1010
result = ElixirScript.Compiler.compile([Atom, String, Agent])
11-
assert is_binary(hd(result))
11+
assert result |> Map.to_list |> hd |> elem(1) |> Map.get(:js_code) |> is_binary
1212
end
1313

1414
test "Output" do
1515
result = ElixirScript.Compiler.compile(Atom, [])
16-
assert hd(result) =~ "export default"
16+
assert result |> Map.to_list |> hd |> elem(1) |> Map.get(:js_code) =~ "export default"
1717
end
1818

1919
test "compile file" do

0 commit comments

Comments
 (0)