E4X with NodeJS
Asked Answered
R

2

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

Rhondarhondda answered 20/2, 2011 at 12:40 Comment(3)
Not really an answer to your question, but I like to write NodeJS scripts in CoffeeScript, because you can use heredocs, or something like p = """Hello #{name}""". 3 quotation marks also allows you to have newlines inside strings.Ophthalmoscope
@Ophthalmoscope 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.Rhondarhondda
You can use React & JSX in NodeJS. But it's not exactly what you want.Polyhedron
P
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.

Pollard answered 20/2, 2011 at 13:55 Comment(2)
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 :(Rhondarhondda
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.Salto
D
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>");
Dynamism answered 1/1, 2020 at 5:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.