如何 运行 babel.transform 与 Nashorn 反应?
How to run babel.transform for react with Nashorn?
我正在尝试使用 babel.transform
而不是 JSXTranformer
进行反应。
...
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');");
...
Babel 和 babel-core 作为全局节点模块安装,但出现错误:
Testsuite: com.my.app.BabelTransformerTest
Cannot find module ./lib/api/node.js
Cannot load module babel-core LOAD_ERROR
Cannot load module babel LOAD_ERROR
Cannot load module babel-core LOAD_ERROR
Cannot load module babel LOAD_ERROR
Cannot find module ./lib/api/node.js
Cannot load module babel-core LOAD_ERROR
Cannot load module babel LOAD_ERROR
./lib/api/node.js
在 C:\Users\***\AppData\Roaming\npm\node_modules
中
听说可以从 Nashorn 运行 babel.transform
?
也许有办法只加载 babel 的某些模块作为 JavaScript 文件?
您应该直接加载 babel.js,然后计算 babel.transform("...")
。应该不需要从 npm 或 node 加载任何东西。
但是,唉,您必须等待 JDK bug 8135190 的修复程序发布,因为加载 babel.js 失败并出现 "method code too large" 异常。
我已经在 jdk1.8 中使用 Babel Standalone。0_45 使用以下脚本:
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);
哪个returns:
React.createElement(Component, null);
es2015 预设也适用。
我正在尝试使用 babel.transform
而不是 JSXTranformer
进行反应。
...
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');");
...
Babel 和 babel-core 作为全局节点模块安装,但出现错误:
Testsuite: com.my.app.BabelTransformerTest
Cannot find module ./lib/api/node.js
Cannot load module babel-core LOAD_ERROR
Cannot load module babel LOAD_ERROR
Cannot load module babel-core LOAD_ERROR
Cannot load module babel LOAD_ERROR
Cannot find module ./lib/api/node.js
Cannot load module babel-core LOAD_ERROR
Cannot load module babel LOAD_ERROR
./lib/api/node.js
在 C:\Users\***\AppData\Roaming\npm\node_modules
听说可以从 Nashorn 运行 babel.transform
?
也许有办法只加载 babel 的某些模块作为 JavaScript 文件?
您应该直接加载 babel.js,然后计算 babel.transform("...")
。应该不需要从 npm 或 node 加载任何东西。
但是,唉,您必须等待 JDK bug 8135190 的修复程序发布,因为加载 babel.js 失败并出现 "method code too large" 异常。
我已经在 jdk1.8 中使用 Babel Standalone。0_45 使用以下脚本:
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);
哪个returns:
React.createElement(Component, null);
es2015 预设也适用。