Skip to content

Commit 0fe7abe

Browse files
committed
Support FFI js files with dots in them
1 parent 83dcd0e commit 0fe7abe

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

lib/elixir_script/module_systems/es.ex

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ defmodule ElixirScript.ModuleSystems.ES do
22
@moduledoc false
33
alias ESTree.Tools.Builder, as: JS
44

5-
def build(imports, js_imports, body, exports) do
6-
module_imports = Enum.map(imports, fn {module, path} -> import_module(module, path) end)
7-
5+
def build(js_imports, body, exports) do
86
imports = js_imports
97
|> Enum.map(fn
10-
{_module, name, path} -> import_module(name, path)
8+
{_module, name, _path, import_path} -> import_module(name, import_path)
119
end)
1210

13-
imports = Enum.uniq(imports ++ module_imports)
14-
1511
export = if is_nil(exports), do: [], else: [export_module(exports)]
1612
imports ++ body ++ export
1713
end

lib/elixir_script/passes/output.ex

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ defmodule ElixirScript.Output do
2626
end)
2727
|> Enum.map(fn
2828
{module, name, path} ->
29-
{module, name, Path.join(opts.root, path)}
29+
import_path = Path.join(opts.root, path)
30+
{module, name, path, import_path}
3031
end)
3132

3233
bundle(modules, opts, js_modules)
@@ -82,7 +83,7 @@ defmodule ElixirScript.Output do
8283

8384
apps = get_app_names()
8485
output_dir = Path.dirname(file_name)
85-
Enum.each(js_modules, fn({_, _, path}) ->
86+
Enum.each(js_modules, fn({_, _, path, _}) ->
8687
copy_javascript_module(apps, output_dir, path)
8788
end)
8889

@@ -108,16 +109,31 @@ defmodule ElixirScript.Output do
108109

109110
defp copy_javascript_module(apps, output_dir, js_module_path) do
110111
Enum.each(apps, fn(app) ->
111-
full_path = Path.join([:code.priv_dir(app), "elixir_script", js_module_path]) <> ".js"
112+
base_path = Path.join([:code.priv_dir(app), "elixir_script"])
113+
114+
js_input_path = cond do
115+
File.exists?(Path.join([base_path, js_module_path])) ->
116+
Path.join([base_path, js_module_path])
117+
File.exists?(Path.join([base_path, slashes_to_dots(js_module_path)])) ->
118+
Path.join([base_path, slashes_to_dots(js_module_path)])
119+
true ->
120+
nil
121+
end
112122

113-
if File.exists?(full_path) do
114-
js_output_path = Path.join(output_dir, js_module_path) <> ".js"
115-
if !File.exists?(Path.dirname(js_output_path)) do
116-
File.mkdir_p!(Path.dirname(js_output_path))
123+
if js_input_path != nil do
124+
js_output_path = Path.join(output_dir, js_module_path)
125+
js_output_dir = Path.dirname(js_output_path)
126+
if !File.exists?(js_output_dir) do
127+
File.mkdir_p!(js_output_dir)
117128
end
118129

119-
File.cp(full_path, js_output_path)
130+
File.cp(js_input_path, js_output_path)
120131
end
121132
end)
122133
end
134+
135+
defp slashes_to_dots(js_module_path) do
136+
js_module_path
137+
|> String.replace("/", ".")
138+
end
123139
end

lib/elixir_script/passes/output/js_module.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ defmodule ElixirScript.Output.JSModule do
1212
elixir = J.variable_declaration([declarator], :const)
1313

1414
ast = opts.module_formatter.build(
15-
[],
1615
js_modules,
1716
[elixir, create_atom_table(), start(), load()] ++ body,
1817
J.identifier("Elixir")
@@ -94,4 +93,4 @@ defmodule ElixirScript.Output.JSModule do
9493
)
9594
end
9695

97-
end
96+
end

0 commit comments

Comments
 (0)