简体   繁体   English

运行通过babel反应原生es6 javascript

[英]Run react native es6 javascript through babel

What's the easiest way to run a javascript module that exists in my react native source tree using babel? 使用babel运行我的react本地源代码树中存在的javascript模块的最简单方法是什么?

Let's say I have the following file 假设我有以下文件

// hi.js
import _ from 'lodash'    
console.log(_.upperCase('hi'));

And I want to just run this quickly from the command line to test it out or use it in a utility script or something like that. 我想从命令行快速运行它来测试它或在实用程序脚本中使用它或类似的东西。 I can't use node directly because of the import statement. 由于import语句,我不能直接使用node。 I know I can install and configure babel... but since babel is already bundled and configured with RN can I just use that somehow? 我知道我可以安装和配置babel ...但由于babel已经捆绑并配置了RN,我可以用它吗?

eg > workspace/node_modules/babe-core/bin/babel-node hi.js 例如> workspace / node_modules / babe-core / bin / babel-node hi.js

Can I just install babel-cli into the project? 我可以将babel-cli安装到项目中吗?

Thanks! 谢谢!

You can compile a file using the babel command line: 您可以使用babel命令行编译文件:

babel hi.js --out-file hi-compiled.js --plugins=es2015,react

This will allow you to then run the file with node: 这将允许您随后使用节点运行该文件:

node hi-compiled.js

Alternatively you could use babel-node which would do it in one command: 或者你可以使用babel-node,它可以在一个命令中执行:

babel-node hi.js --plugins es2015

All this can be achieved with npm install -g babel-cli 所有这些都可以通过npm install -g babel-cli来实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM