如何使用minibuffer为react js文件设置web模式内容类型

How to set web-mode-content-type for react js files, using minibuffer

本文关键字:模式 web 类型 设置 文件 minibuffer 何使用 react js      更新时间:2023-09-26

我使用此配置为.jsx文件启用web-mode

(add-to-list 'auto-mode-alist '("''.jsx$" . web-mode))

但是,当我有react文件的.js扩展名时,这将失败。web-mode无法将内容类型关联为jsx

上面写着:

var web-mode-content-types-alist可用于将文件路径与内容类型相关联

但我不想指定文件路径。

我想使用迷你缓冲区或更好的自动检测来关联内容类型。

您可以使用

(setq web-mode-content-types-alist '(("jsx" . "/some/react/path/*''.js[x]?'''")))

或者,如果你不想为每个js文件检测jsx

(setq web-mode-content-types-alist '(("jsx" . "''.js[x]?'''")))

您可以使用eval表达式(通常绑定到M-:),然后扬克或键入以下表达式:

(setq web-mode-content-types-alist '(("jsx"  . "''.js[x]?'''")))

问题在最新版本中得到修复。这以交互方式设置内容类型:

(web-mode-set-content-type "jsx")

以下是我对支持web模式的react jsx的设置。

(require 'jsx-mode)
(add-to-list 'auto-mode-alist '("''.jsx'''" . jsx-mode))
(autoload 'jsx-mode "jsx-mode" "JSX mode" t)
(custom-set-variables
 '(jsx-indent-level 2)
 '(jsx-cmd-options '("--add-search-path" "/path/to/search-path")))
;; add web mode indent for react jsx
(add-to-list 'auto-mode-alist '("''.jsx'''" . web-mode))