2014-12-19 29 views
78

我使用Sublime Text作为文本编辑器。是否有一个用于崇高文本的JSX格式器?

有一个用于格式化javascript文件的js格式,但是我找不到一个用于JSX的文件。

你们如何处理格式化JSX?

+0

是,寻找jsx textmate捆绑包,几乎可以与任何编辑器一起使用。 – FakeRainBrigand 2014-12-19 02:01:36

+0

它看起来只是一个语法hightlighter,我正在寻找格式化程序,确定身份和排序的东西 – fin 2014-12-19 02:59:42

+2

我不知道一个。反应谷歌组将是一个更好的地方问。 – FakeRainBrigand 2014-12-19 05:16:05

回答

45

更新4

检查prettier,而不是配置为esformatter,但目前用于格式化一些大项目(like React itself

更新3

检查sublime jsfmt。如果您将esformatter-jsx添加到配置中,并将该软件包安装在forlder中以获得sublime-jsfmt。您将能够直接从Sublime格式化JSX文件。 Here is a guide for that

更新2

在命令行中,你也可以使用esbeautifier。它是围绕esformatter的包装接受水珠列表格式

# install the dependencies globally 
npm i -g esbeautifier 

# beautify the files under src/ and specs/ both .js and .jsx 
esbeautifier src/**/*.js* specs/**/*.js* 

更新

所以最后我做一个插件esformatter启用的JSX文件格式:

https://www.npmjs.com/package/esformatter-jsx

这里是一个live demo on requirebin

从Sublime传递esformatter作为参数传递当前文件应该是可行的。在任何情况下使用该命令行,你可以按照下列指示:

在命令行中就可以这样使用:

# install the dependencies globally 
npm i -g esformatter esformatter-jsx 

# call it (this will print to stdout) 
esformatter --plugins=esformatter-jsx ./path/to/your/file 

# to actually modify the file 
esformatter --plugins=esformatter-jsx ./path/to/your/file > ./path/to/your/file 

# to specify a config file (where you can also specify the plugins) 
# check esformatter for more info about the configuration options 
esformatter -c ./path/to/.esformatter ./path/to/your/file > ./path/to/your/file 

====老回答以下===

所以,如果你正在寻找的只是让你的jsx文件格式化,同时允许jsx语法(基本上美化所有的javascript语法,并忽略jsx标签,意味着离开它们),这就是我' m使用esformatter

// needed for grunt.file.expand 
var grunt = require('grunt'); 

// use it with care, I haven't check if there 
// isn't any side effect from using proxyquire to 
// inject esprima-fb into the esformatter 
// but this type of dependency replacement 
// seems to be very fragile... if rocambole deps change 
// this will certainly break, same is true for esformatter 
// use it with care 
var proxyquire = require('proxyquire'); 
var rocambole = proxyquire('rocambole', { 
    'esprima': require('esprima-fb') 
}); 

var esformatter = proxyquire('esformatter', { 
    rocambole: rocambole 
}); 

// path to your esformatter configuration 
var cfg = grunt.file.readJSON('./esformatter.json'); 

// expand the files from the glob 
var files = grunt.file.expand('./js/**/*.jsx'); 

// do the actual formatting 
files.forEach(function (fIn) { 
    console.log('formatting', fIn); 
    var output = esformatter.format(grunt.file.read(fIn), cfg); 
    grunt.file.write(fIn, output); 
}); 

我实际上喜欢esformatter使用rocambole版本,使用esprima-fb而不是esprima来避免proxyquire。

+1

这只适用于esformatter-jsx-ignore,但这已经足够了。谢谢! – Jasper 2015-06-27 16:08:20

+0

我正在使用esformatter-jsx,它按照我的预期工作。不过,我还必须在Sublime 3的“首选项>包装设置> Sublime JSFMT>设置 - 默认值”中将“esformatter-jsx”添加到“选项”中:{“plugins”:{“esformatter-jsx”}}作品 – Kuma 2015-10-01 06:57:00

+0

这是一个很好的答案,我现在已经使用了这个工具一段时间,但是我们已经改用es6/es7和babel,并且它不再工作了。 – Mintberry 2015-10-05 13:25:08

57

HTML-CSS-JS Prettify插件中有一个设置,允许您忽略js/jsx文件中的xml语法。这样它就不会搞乱jsx代码。 设置为:"e4x": true的设置中的“JS”一节中的文件

首选项>套餐设置> HTML \ CSS \ JS美化>设置美化首

,如果你有自我结束标记这个工作不正常例如。以/>结尾的标签

+0

这是一个窍门!谢谢! – 2015-02-26 17:35:41

+1

这仍然适用于babel-sublime和HTML-CSS-JS的最新版本。只要确保你添加了“jsx”或你使用的任何文件扩展名为“allowed_file_extensions”。 – damd 2016-04-01 13:39:22

+0

这个答案过时了吗?我的代码中没有'/>',但它仍然不起作用 – 2017-04-11 03:36:53

18

要添加什么@Shoobah说:

有一个在HTML,CSS,JS美化插件的设置,允许你 忽略xs语法在js/jsx文件中。这样它就不会搞糟 的jsx代码。该设置是: “E4X”:真正的 设置的 “JS” 一节中的文件

转到:首选项>套件设置> HTML \ CSS \ JS美化>设置 美化首

围棋以“JS”部分:

添加“JSX”到“allowed_file_extension”,然后更改“EX4”为“true”

15

在总告诉你设置“E4X”到互联网的答案真, 但有时,我们要设置“format_on_save_extensions”的选项,然后添加“JSX”在阵列

修改jsFormat.sublime的设置

{ 
    "e4x": true, 
    "format_on_save": true, 
    "format_on_save_extensions": ["js", "json", "jsx"] 
} 
+0

我按照你的说法做了,但它不适合我。 :( – 2016-04-27 05:58:17

+0

这个为我工作的魅力 – kachar 2016-05-14 17:52:50

+0

在哪里是_“jsFormat.sublime-settings”_ found,这样我就可以修改'“format_on_save_extensions”:[“js”,“json”,“jsx”]'。最近我发现关于文件扩展名是:''js“:{ ”allowed_file_extensions“:[”js“,”json“,...]' – Chris22 2016-10-18 23:31:15

3

用崇高的软件包安装程序,安装Babel。然后:

  1. 打开一个.jsx文件。
  2. 从菜单中选择查看,
  3. 然后语法 - >打开所有当前的扩展名为... - > Babel - > JavaScript(Babel)。
15

你可以安装一个JsPrettier包崇高2 & 3.这是一个相当新的JavaScript格式化(在写这篇的时间:二月-2017)。它支持大多数最新的开发,如:ES2017,JSX和Flow。

快速入门

  1. 安装漂亮全球使用终端:$ npm install -g prettier
  2. 在崇高去工具 - >命令面板 - >程序包控制:安装包,键入单词JsPrettier,然后选择完成安装。
  3. 格式文件使用上下文菜单编辑器内或将其绑定到一个键盘快捷键:{ "keys": ["super+b"], "command": "js_prettier" }

链接:

+1

这是迄今为止最好的答案!漂亮是大爆炸! – majorBummer 2017-04-19 01:50:10

+1

是的,确实,这对我来说也很合适。最重要的是要注意的是“全球”部分。 – Ionut 2018-01-13 15:02:29