我正在使用Sublime Text作为文本编辑器。
有JSFormat用于格式化JavaScript文件,但我找不到JSX的格式化工具。
你们如何处理JSX的格式化?
我正在使用Sublime Text作为文本编辑器。
有JSFormat用于格式化JavaScript文件,但我找不到JSX的格式化工具。
你们如何处理JSX的格式化?
更新 4
查看 prettier,虽然可配置性不如 esformatter,但目前被一些大型项目(例如 React 本身)用来格式化代码。
更新 3
查看 sublime jsfmt。如果您将 esformatter-jsx 添加到配置文件中并在 sublime-jsfmt 文件夹内安装该包,则可以直接从 Sublime 格式化 JSX 文件。这里是使用指南。
更新 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
这里是一个在 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
标签,也就是保留它们不变),那么我使用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使用一个使用esprima-fb而不是esprima的rocambole版本,以避免使用proxyquire。
HTML-CSS-JS Prettify插件中有一个设置可以让你忽略js/jsx文件中的xml语法。这样就不会破坏jsx代码。
设置是:在设置文件的"js"部分中添加"e4x": true
首选项 > 包设置 > HTML\CSS\JS Prettify > 设置Prettify首选项
如果您有自闭合标签(例如以/>结尾的标签),则该设置可能效果不佳。
/>
,但它仍然无法工作。 - Andy Darwin$ npm install -g prettier
{ "keys": ["super+b"], "command": "js_prettier" }
补充@Shoobah所说的:
在HTML-CSS-JS Prettify插件中有一个设置,可以忽略js/jsx文件中的xml语法。这样就不会破坏jsx代码。该设置是:settings文件中“js”部分中的“e4x”: true
前往: Preferences > Package Settings > HTML\CSS\JS Prettify > Set Prettify Preferences
前往“js”部分:
添加“jsx”到“allowed_file_extension”,然后将“e4x”改为“true”
互联网上的答案通常告诉你将'e4x'设置为true,但有时我们必须设置'format_on_save_extensions'选项,然后在数组中添加'jsx'
修改jsFormat.sublime-settings文件
{
"e4x": true,
"format_on_save": true,
"format_on_save_extensions": ["js", "json", "jsx"]
}
“js”:“allowed_file_extensions”:[“js”,“json”,...]
- Chris22虽然不是专为Sublime Text设计的,但JavaScript中有一个用于美化React JSX的工具。
http://prettydiff.com/?m=beautify声称支持JSX,请参考: http://prettydiff.com/guide/react_jsx.xhtml