Skip to content

Commit ba22c80

Browse files
committed
Add root option. Keep extension for imports
Root option allows for specifying the first part of imports used when importing FFI modules. The extension change is also for FFI module imports. Both are added from testing inside of Chrome when using ES modules directly.
1 parent 93dca72 commit ba22c80

File tree

9 files changed

+17
-22
lines changed

9 files changed

+17
-22
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
- ElixirScript now has a Foreign Function Interface (FFI) for interoperability with JavaScript. For more details, see documentation at `ElixirScript.FFI`
1111
- `ElixirScript.JS.mutate/3`
1212
- `ElixirScript.JS.map_to_object/1`
13+
- `root` option for specifying the root import path for FFI JavaScript modules. Defaults to `"."`
14+
- For imports, now keeping the `.js` extension
1315

1416
### Changed
1517
- Compiler has been completely rewritten. ElixirScript now requires Erlang 20+ and Elixir 1.5+

GettingStarted.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ Available options are:
3737
If path ends in `.js` then that will be the name of the file. If a directory is given,
3838
file will be named `Elixir.App.js`
3939

40+
* `root`: Optional root for imports of FFI JavaScript modules. Defaults to `.`. If using output directly in a browser, you may want to make it something like `/js` or some uri.
41+
4042

4143
Now run `mix compile` and you should see a JavaScript file named `Elixir.App.js` in the `priv/elixir_script/build/` directory. ElixirScript outputs JavaScript in the ES Module format. If your browser supports it, you can include the output in a script tag with the type "module"
4244

4345
```html
4446
<script type="module">
45-
import Elixir from './Elixir.App.js'
47+
import Elixir from '/js/Elixir.App.js'
4648
const myInitialArgs = []
4749
4850
Elixir.start(Elixir.MyEntryModule, myInitialArgs)

JavascriptInterop.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export default {
4444
}
4545
```
4646

47+
For more information and options. Check the documentation for `ElixirScript.FFI`
48+
4749
## JavaScript Calling ElixirScript
4850

4951
In order to start an ElixirScript application, you must first import it using whichever JavaScript module system you are using and then call `Elixir.start`
@@ -60,8 +62,7 @@ export default {
6062
```
6163

6264
```javascript
63-
//ES module example
64-
import Elixir from './Elixir.App'
65+
import Elixir from './Elixir.App.js'
6566
Elixir.start(Elixir.Main, [1, 2, 3])
6667
```
6768

lib/elixir_script/cli.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ defmodule ElixirScript.CLI do
77
output: :string,
88
help: :boolean,
99
version: :boolean,
10-
watch: :boolean
10+
watch: :boolean,
11+
root: :string
1112
]
1213

1314
@aliases [
@@ -44,6 +45,7 @@ defmodule ElixirScript.CLI do
4445
Can be a directory or filename.
4546
-v --version the current version number
4647
-h --help this message
48+
--root The root import path for FFI imports
4749
"""
4850
end
4951

@@ -67,7 +69,8 @@ defmodule ElixirScript.CLI do
6769
{watch, options} = Keyword.pop(options, :watch, false)
6870

6971
compile_opts = [
70-
output: Keyword.get(options, :output, :stdout)
72+
output: Keyword.get(options, :output, :stdout),
73+
root: Keyword.get(options, :root, ".")
7174
]
7275

7376
input = handle_input(input)

lib/elixir_script/compiler.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ defmodule ElixirScript.Compiler do
3636
|> Map.put(:output, Keyword.get(opts, :output))
3737
|> Map.put(:format, :es)
3838
|> Map.put(:entry_modules, entry_modules)
39+
|> Map.put(:root, Keyword.get(opts, :root, "."))
3940

4041
options = default_options
4142
Map.put(options, :module_formatter, ElixirScript.ModuleSystems.ES)

lib/elixir_script/passes/find_used_modules.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ defmodule ElixirScript.FindUsedModules do
3232
{name, path}
3333
else
3434
name = Enum.join(Module.split(module), "_")
35+
path = path <> ".js"
3536
{name, path}
3637
end
3738

lib/elixir_script/passes/output.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule ElixirScript.Output do
2626
end)
2727
|> Enum.map(fn
2828
{module, name, path} ->
29-
{module, name, Path.join(".", path)}
29+
{module, name, Path.join(opts.root, path)}
3030
end)
3131

3232
bundle(modules, opts, js_modules)

lib/elixir_script/passes/translate/module.ex

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,6 @@ defmodule ElixirScript.Translate.Module do
1313
ElixirScript.Translate.Protocol.compile(module, info, pid)
1414
end
1515

16-
def compile(module, %{attributes: [__foreign_info__: %{path: path, name: name, global: global}]}, pid) do
17-
{name, path} = if global do
18-
name = if name, do: name, else: module
19-
path = nil
20-
{name, path}
21-
else
22-
name = Enum.join(Module.split(module), "_")
23-
{name, path}
24-
end
25-
26-
ModuleState.put_javascript_module(pid, module, name, path)
27-
28-
nil
29-
end
30-
3116
def compile(module, info, pid) do
3217
%{
3318
attributes: attrs,

lib/elixir_script/state.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ defmodule ElixirScript.State do
105105
state.modules
106106
end)
107107
end
108-
end
108+
end

0 commit comments

Comments
 (0)