Is there any way of using JSX in a native CommonJS environment?

别等时光非礼了梦想. 提交于 2019-12-05 12:28:45

[Update]

Now that the JSX transformer is being deprecated in favor of Babel, you can use Babel's require hook to support JSX in CommonJS-like environments:

Usage

require("babel/register");

All subsequent files required by node with the extensions .es6, .es, .jsx and .js will be transformed by Babel. The polyfill is also automatically required.

NOTE: By default all requires to node_modules will be ignored. You can override this by passing an ignore regex via:

require("babel/register")({
  // This will override `node_modules` ignoring - you can alternatively pass
  // an array of strings to be explicitly matched or a regex / glob
  ignore: false
});

[Old Answer]

If it's a Node.js environment (like atom-shell is) you can use node-jsx:

require('node-jsx').install()
require("./myComponent.js");

You can also use the .jsx extension if you want:

require('node-jsx').install({extension: '.jsx'})
require("./myComponent.jsx");

You can simply copy over the source directory:

cp -R src dist

And then use the jsx tool from react-tools

jsx -x jsx dist dist

And remove the jsx files

find dist -iname '*.jsx' | xargs rm

There are perhaps cleaner ways to do this, but this should be good enough for now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!