fxbois / web-mode

web template editing mode for emacs
https://web-mode.org
GNU General Public License v3.0
1.63k stars 262 forks source link

JavaScript file variation #1289

Closed jcubic closed 7 months ago

jcubic commented 7 months ago

with NodeJS there are new types of file extensions: mjs for ES Modules having this extension NodeJS will run JavaScript as a module, and there is also cjs for forcing common JS type of module.

Both file extensions don't work with Web-mode.

Is there a way to force web-mode to think that the file is actually JavaScript? I only tested manually triggering the web-mode with M-x.

yugaego commented 7 months ago

Did you try adding these extensions to auto-mode-alist? See also web-mode-content-types-alist.

jcubic commented 7 months ago

auto-mode-alist will not help since this only triggers the major mode of different file types. This is the same as triggering the mode manually. The mode is enabled, only everything is white (in dark mode).

Not sure how to use the second one, there is only a default value that I don't understand:

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

I've found this variable web-mode-engines-alist and used this code:

(add-to-list 'auto-mode-alist '("\\.mjs\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.cjs\\'" . web-mode))

(dolist (spec '(("js" . "\\.mjs\\'")
                ("js" . "\\.cjs\\'")))
  (add-to-list 'web-mode-engines-alist spec))

But this doesn't work. Also tried:

(dolist (spec '(("javascript"      . "\\.mjs\\'")
                ("javascript"      . "\\.cjs\\'")))
  (add-to-list 'web-mode-engines-alist spec))

That also doesn't work.

jcubic commented 7 months ago

This seems to work:

(add-to-list 'auto-mode-alist '("\\.mjs\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.cjs\\'" . web-mode))

(dolist (spec '(("javascript" . "\\.mjs\\'")
                ("javascript" . "\\.cjs\\'")))
  (add-to-list 'web-mode-content-types-alist spec))