|
1 | 1 | +++ |
2 | | -title = "Elixirscript 0.30.0 Released" |
| 2 | +title = "ElixirScript 0.30.0 Released" |
3 | 3 | date = 2017-08-09T00:29:03-05:00 |
4 | 4 | draft = true |
5 | 5 | +++ |
| 6 | + |
| 7 | +ElixirScript 0.30 is a large release with lots of changes. It includes a rewrite of the compiler, a change in JavaScript interoperability and much more. These changes make writing ElixirScript projects feel like writing a normal Elixir project. These changes make this the best release of ElixirScript so far! This is a great point to give ElixirScript a try for yourself. This post will describe the major changes in this version. |
| 8 | + |
| 9 | +## Erlang 20 and Elixir 1.5 (compiled with Erlang 20) required |
| 10 | + |
| 11 | +This release requires the above because it reads debug information from beam files. This is the same information used for `Exception.blame/3` in Elixir 1.5. This means ElixirScript can compile any Elixir module. One benefit is ElixirScript can now compile the actual Elixir standard library. In earlier versions, it had to reimplement the standard library. Sections below describe the other benefits of this. |
| 12 | + |
| 13 | +## Can now use normal Elixir dependencies |
| 14 | + |
| 15 | +With the above, ElixirScript can also compile your project's dependencies to JavaScript. |
| 16 | + |
| 17 | +## ElixirScript-specific packages are now possible |
| 18 | + |
| 19 | +This is one of the most exciting changes. It is now possible to create packages that ElixirScript code can use. This is thanks to the changes described above and the new FFI described later. These are normal mix projects which may or may not have FFI modules and some JavaScript files. |
| 20 | + |
| 21 | +If creating a Hex package for ElixirScript, please prepend `elixir_script` to your package name. This is so that it is clear your package is to be use with ElixirScript. For example, a React package would be named `elixir_script_react`. [Here is an example](https://github.com/elixirscript/elixirscript_react). |
| 22 | + |
| 23 | +## Foreign Function Interface (FFI) |
| 24 | + |
| 25 | +JavaScript interoperability has changed in this version. ElixirScript now interacts with JavaScript using an FFI. This allows for a stricter barrier between Elixir code and JavaScript. It also allows for the ability to make packages. Another benefit is that there are far fewer warnings during compilation time. As an example let's create an FFI module for some HTTP specific functions we want to use from JavaScript. An FFI module has 2 parts: the Elixir module and the corresponding JavaScript module. |
| 26 | + |
| 27 | +Our Elixir module, `MyApp.Http` |
| 28 | +```elixir |
| 29 | +defmodule MyApp.Http do |
| 30 | + use ElixirScript.FFI |
| 31 | + |
| 32 | + defexternal get(url) |
| 33 | + defexternal post(url, body) |
| 34 | +end |
| 35 | +``` |
| 36 | + |
| 37 | +Our JavaScript module |
| 38 | +```javascript |
| 39 | +function get(url) { |
| 40 | + // Implementation goes here |
| 41 | +} |
| 42 | +function post(url, body) { |
| 43 | + // Implementation goes here |
| 44 | +} |
| 45 | + |
| 46 | +export default { |
| 47 | + get, |
| 48 | + post |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +ElixirScript depends on the JavaScript module being at a certain path. It will look for the JavaScript module either at `priv/elixir_script/my_app/http.js` or `priv/elixir_script/my_app.http.js`. If your code uses the FFI module, during compilation ElixirScript copies the js file along side the compilation output. The generated output imports the js file. |
| 53 | + |
| 54 | +With the above in place, you can call functions on `MyApp.Http` as you would any other Elixir module. |
| 55 | + |
| 56 | +For more information, check the documentation for `ElixirScript.FFI`. |
| 57 | + |
| 58 | +## Limitations |
| 59 | + |
| 60 | +`receive` is still not supported. When ElixirScript encounters a `receive` call during compilation, the compiler will show a warning, but will continue. During runtime these calls will throw an error. With `receive` not yet supported, this also means most of OTP is not supported as well. |
| 61 | + |
| 62 | +## A Call to Arms |
| 63 | + |
| 64 | +Calls to Erlang functions that the Elixir standard library make have to be reimplemented in JavaScript. There are many that are implemented, but there are more that need to be. This effects the availability of some Elixir standard library calls. A major contribution to ElixirScript would be to help with an implementation of an Erlang function that is blocking the use of an Elixir function you want to use! |
| 65 | + |
| 66 | +## Breaking Changes |
| 67 | + |
| 68 | +ElixirScript now only works as a mix compiler. This means there will no longer be an escript for download and plugins for Brunch and Webpack will no longer work with this and future versions as well. |
| 69 | + |
| 70 | +ElixirScript now only supports output of ES modules. This is mainly so that there is no confusion about how to implement the JavaScript portion of FFI modules. |
| 71 | + |
| 72 | +## Conclusion |
| 73 | + |
| 74 | +For more information regarding changes, please check the [changelog](https://github.com/elixirscript/elixirscript/blob/master/CHANGELOG.md). For more information about how to get started, the FFI, and other things, check out the [docs](https://hexdocs.pm/elixir_script) |
| 75 | + |
| 76 | +Finally, I want to give a great big thanks to the Elixir Core Team! Thanks for making the updates in Erlang and Elixir that allowed ElixirScript's progress to continue. And thank you for being available to answer questions and to give great advice! |
0 commit comments