E4X with NodeJS

2.2k views Asked by At

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

2

There are 2 answers

2
Ivo Wetzel On BEST ANSWER

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.

0
Antonio Marin On

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>");