How to run babel.transform to react with Nashorn?

I am trying to use babel.transforminstead JSXTranformerfor the reaction.

...
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine nashorn = mgr.getEngineByName("nashorn");
nashorn.eval("var process = {env:{}}"); // node-modules expect that
nashorn.eval(getScript("com/facebook/babel/jvm-npm.js"));
babel = (JSObject) nashorn.eval("require('babel');");
...

Babylon and babel-core are installed as global node modules, and I have an error:

Testsuite: com.my.app.BabelTransformerTest
Cannot find the module. / lib / api / node.js
Unable to load module babel-core LOAD_ERROR
Unable to load module babel LOAD_ERROR
Unable to load module babel-core LOAD_ERROR
Unable to load module babel LOAD_ERROR
Unable to find module. / lib / api / node.js
Unable to load babel-core module LOAD_ERROR
Unable to load babel-LOAD_ERROR module

./lib/api/node.js is in C:\Users\***\AppData\Roaming\npm\node_modules

I heard that you can run babel.transformfrom Nashorn?

, babel JavaScript?

+4
2

Babel Standalone jdk1.8.0_45 script:

FileReader babelScript = new FileReader("babel.js");
ScriptEngine engine = new ScriptEngineManager().getEngineByMimeType("text/javascript");

SimpleBindings bindings = new SimpleBindings();
engine.eval(babelScript, bindings);

bindings.put("input", "<Component />");
Object output = engine.eval("Babel.transform(input, { presets: ['react'] }).code", bindings);
System.out.println(output);

:

React.createElement(Component, null);

es2015.

+8

babel.js , babel.transform("..."). npm node.

, , JDK bug 8135190, babel.js " ".

+2

Source: https://habr.com/ru/post/1608024/


All Articles