我使用 Sublime Text 作为文本编辑器。
有一种用于格式化 javascript 文件的 jsFormat,但我找不到用于 JSX 的一种。
你们如何处理格式化 JSX?
我使用 Sublime Text 作为文本编辑器。
有一种用于格式化 javascript 文件的 jsFormat,但我找不到用于 JSX 的一种。
你们如何处理格式化 JSX?
更新 4
检查漂亮,不是配置为esformatter,但目前用于格式化一些大项目(等react本身)
更新 3
检查sublime jsfmt。如果您将esformatter-jsx添加到配置中并在 sublime-jsfmt 的 forlder 中安装该包。您将能够直接从 Sublime 格式化 JSX 文件。这是一个指南
更新 2
您还可以从命令行使用esbeautifier。它是esformatter的包装器,它接受要格式化的 globs 列表
# 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 代码。设置是:"e4x": true
在设置文件的“js”部分
首选项 > 包设置 > HTML\CSS\JS 美化 > 设置美化首选项
如果您有自关闭标签,这将不起作用,例如。以/> 结尾的标签
您可以为 Sublime 2 和 3 安装一个 JsPrettier 包。这是一个相当新的 JavaScript 格式化程序(在撰写本文时:2017 年 2 月)。它支持大多数最新开发,例如:ES2017、JSX 和 Flow。
快速开始
$ npm install -g prettier
{ "keys": ["super+b"], "command": "js_prettier" }
链接:
补充一下@Shobah 所说的:
HTML-CSS-JS Prettify 插件中有一个设置,允许您忽略 js/jsx 文件中的 xml 语法。这样它就不会弄乱 jsx 代码。设置为: "e4x": true 在设置文件的 "js" 部分
转到:首选项 > 包设置 > HTML\CSS\JS 美化 > 设置美化首选项
转到“js”部分:
将“jsx”添加到“allowed_file_extension”,然后将“e4x”更改为“true”