-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathes.ex
More file actions
56 lines (43 loc) · 1.55 KB
/
Copy pathes.ex
File metadata and controls
56 lines (43 loc) · 1.55 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
defmodule ElixirScript.ModuleSystems.ES do
@moduledoc false
alias ESTree.Tools.Builder, as: JS
alias ElixirScript.Translator
alias ElixirScript.Translator.State
alias ElixirScript.Translator.Utils
def build(imports, js_imports, body, exports) do
module_imports = Enum.map(imports, fn {module, path} -> import_module(module, path) end)
imports = js_imports
|> Enum.map(fn
{module, path} -> import_module(module, path)
{module, path, default: true} -> import_module(module, path)
{module, path, default: false} -> import_namespace_module(module, path)
end)
imports = Enum.uniq(imports ++ module_imports)
export = if is_nil(exports), do: [], else: [export_module(exports)]
imports ++ body ++ export
end
defp import_namespace_module(module_name, from) do
js_module_name = ElixirScript.Translator.Identifier.make_namespace_members(module_name)
import_specifier = JS.import_namespace_specifier(
js_module_name,
js_module_name
)
do_import_module([import_specifier], from)
end
defp import_module(import_name, from) do
js_module_name = ElixirScript.Translator.Identifier.make_namespace_members(import_name)
import_specifier = JS.import_default_specifier(
js_module_name
)
do_import_module([import_specifier], from)
end
defp do_import_module(import_specifiers, file_path) do
JS.import_declaration(
import_specifiers,
JS.literal(file_path)
)
end
defp export_module(exported_object) do
JS.export_default_declaration(exported_object)
end
end