12

Is there any way to get E4X(ECMAScript) to work with NodeJS?

It would really help to output slick html/xml without hassle/noise.

It works fine using SpiderMonkey since it is natively implemented, but it doesn't seem to work with NodeJS.

using node

$node
> var name = "World";
> var p = <p>Hello {name}</p>;
...

using spidermonkey

$js
js> var name = "World";
js> var p = <p>Hello {name}</p>;
Hello World
js>

thanks in advance

zanona
  • 11,379
  • 24
  • 78
  • 137
  • 1
    Not really an answer to your question, but I like to write NodeJS scripts in [CoffeeScript](http://jashkenas.github.com/coffee-script/), because you can use heredocs, or something like `p = """Hello #{name}"""`. 3 quotation marks also allows you to have newlines inside strings. – Thai Feb 20 '11 at 14:11
  • @Thai thanks for this it was good to know that there's something helpul like CoffeScript and actually is very clever, I like it a lot. – zanona Feb 20 '11 at 17:57
  • You can use React & JSX in NodeJS. But it's not exactly what you want. – user1742529 Dec 08 '17 at 09:25

2 Answers2

10

Node uses V8, which does not implement E4X as of now.

There's a 2 year old issue, but still active issue on the topic. But it has no real "status" nor was it assigned to anyone.

So in short: The answer is no.

Ivo Wetzel
  • 44,463
  • 14
  • 89
  • 109
  • yes, it is really a shame because at a first instance it is seems really weird not having this feature implemented...and it also seems there's lots of people waiting for it too :( – zanona Feb 20 '11 at 17:58
  • There are a lot of template engines that could be used to do what e4x does. The only downside is you'll need to run an additional call to put the variables into the template string after you've defined it. – Blacktiger Feb 21 '11 at 20:50
0

I have developed a babel plugin that adds E4X basic support to NodeJS.

https://www.npmjs.com/package/babel-plugin-transform-simple-e4x

You can also use the npm simple4x library to parse xml strings to a XML like object.

https://www.npmjs.com/package/simple4x

The plugin transpiles the following E4X:

var fooId = 'foo-id';
var barText = 'bar text';
var xml =
    <xml>
        <foo id={fooId}>
          {barText}
        </foo>
    </xml>;

To the following JavaScript:

var XML = new require("simple4x");

var fooId = 'foo-id';
var barText = 'bar text';
var xml = new XML("<xml><foo id=\"" + fooId + "\">" + barText + "</foo></xml>");