Search code examples
purescript

How to translate Purescript into ES6


I am looking for a way of controlling translation format for Purescript code when target platform is JavaScript.

"spago bundle-app" generates JavaScript code for ES5 version.

spago/pulp/purs --help doesn't tell much.

googling by keywords like "Purescript codegen target ES6" is not helpful either.

Some discussions regarding ES6 and Purescript popped up among results but nothing practically useful.

I've found lebab tool translating ES5 to ES6, but I guess it is not right way to go.


Solution

  • The PureScript compiler creates mostly ES3 code. This is on purpose because Ecma Script is strictly backwards compatible and ES3 code runs in a ES5-, ES2015-, ES2016- (and so on) environment. This means that code created by the PureScript compiler runs even in older browsers.

    If you are coming from TypeScript or Babel, you might be used to being able to choose the target. This is because these compilers work with plugins that run after each other. But the PureScript compiler does not have such a feature (since it is not transforming JS to JS like these specific compilers).

    So what can be the benefit of targeting a newer version? Code size, performance and features. If you want to use new ES2015 features in your FFI code there is great news: You can make use of these features now in PureScript 0.13. There are also talks about making the compiler target newer JavaScript environments in the future for the benefits mentioned above. If someone would have to support older environments they could still add Babel to their toolchain. But PureScript is a small community project and neither performance nor code size are of a very high priority for the project (and if they were, there would probably be other optimisations that could yield much greater results).