eslint_d
Makes eslint the fastest linter on the planet.
"But eslint is pretty fast already, right?"
Yes, it's actually super fast. But the node.js startup time and loading all the
required modules slows down linting times for a single file to ~700
milliseconds. eslint_d
reduces this overhead by running a server in the
background. It brings the linting time down to ~160 milliseconds. If you want
to lint from within your editor whenever you save a file, eslint_d
is for
you.
Install
This will install the eslint_d
command globally:
$ npm install -g eslint_d
Usage
To start the server and lint a file, just run:
$ eslint_d file.js
On the initial call, the eslint_d
server is launched and then the given file
is linted. Subsequent invocations are super fast.
How does this work?
The first time you use eslint_d
, a little server is started in the background
and bound to a random port. The port number is stored along with a
token in ~/.eslint_d
. You can then run eslint_d
commands the
same way you would use eslint
and it will delegate to the background server.
It will load a separate instance of eslint for each working
directory to make sure settings are kept local. If eslint is found in the
current working directories node_modules
folder, then this version of eslint
is going to be used. Otherwise, the version of eslint that ships with
eslint_d
is used as a fallback.
However, the performance gain comes at a small price: Changes in the eslint
settings are only picked up after a server restart, so you will have to
remember to run eslint_d restart
after tweaking this rule or installing that
plugin. Also, when you have a lot of projects that use eslint, it might use
quite a bit of ram for cached instances. All memory can be freed up by running
eslint_d stop
or eslint_d restart
.
Commands
Control the server like this:
$ eslint_d <command>
Available commands:
start
: start the serverstop
: stop the serverstatus
: print out whether the server is currently runningrestart
: restart the server[options] file.js [file.js] [dir]
: invoke eslint
with the given options.
The eslint
engine will be created in the current directory. If the server
is not yet running, it is started.
Type eslint_d --help
to see the supported eslint
options.
eslint_d
will select a free port automatically and store the port number
along with an access token in ~/.eslint_d
.
Editor integration
Linting
-
Sublime: Check out SublimeLinter-contrib-eslint_d.
-
Vim: Install the syntastic plugin, then make sure this is in your
.vimrc
:
let g:syntastic_javascript_checkers = ['eslint']
let g:syntastic_javascript_eslint_exec = 'eslint_d'
-
WebStorm: Configure your IDE to point to the eslint_d
package instead
of eslint
. In the ESLint configuration dialog, under 'ESLint package',
select your eslint_d
package.
-
Atom: You will not gain any performance from this module as it already
avoids starting a new node instance and uses the API directly (see this
AtomLinter issue).
-
Emacs: Use flycheck with the javascript-eslint
checker:
(setq flycheck-javascript-eslint-executable "eslint_d")
If you're using eslint_d
in any other editor, please tell me!
Automatic Fixing
eslint_d
has an additional flag that eslint
does not have, --fix-to-stdout
which prints the fixed file to stdout. This allows editors to add before save
hooks to automatically fix a file prior to saving. It must be used with
--stdin
.
-
Emacs: Add this to your init.el
, be sure to add hooks for the actual
major modes you use. If you're using along with flycheck, you can replace
"eslint_d"
below with flycheck-javascript-eslint-executable
:
(defun eslint-fix ()
(interactive)
(let ((current-point (point))
(line (count-screen-lines (window-start) (point)))
(command (concat
"eslint_d"
" --stdin"
" --fix-to-stdout"
" --stdin-filename " buffer-file-name))
(buffer (current-buffer))
(text (buffer-substring-no-properties (point-min) (point-max))))
(with-temp-buffer
(insert text)
(when (eq 0
(shell-command-on-region
;; Region
(point-min)
(point-max)
;; Command
command
;; Output to current buffer
t
;; Replace buffer
t
;; Error buffer name
"*eslint-fix error*"))
(let ((fixed-text (buffer-substring-no-properties (point-min) (point-max))))
(with-current-buffer buffer
(delete-region (point-min) (point-max))
(insert fixed-text)
;; Restore point and scroll position
(goto-char current-point)
(recenter (- line 1))))))))
(add-hook 'js-mode-hook
(lambda () (add-hook 'before-save-hook #'eslint-fix nil t)))
Moar speed
If you're really into performance and want the lowest possible latency, talk to
the eslint_d
server with netcat. This will also eliminate the node.js startup
time.
$ PORT=`cat ~/.eslint_d | cut -d" " -f1`
$ TOKEN=`cat ~/.eslint_d | cut -d" " -f2`
$ echo "$TOKEN $PWD file.js" | nc localhost $PORT
This runs eslint
in under 50ms
!
Tip For additional speed, did you know that you can lint only files that
have changed? This is a feature of normal eslint
, but it also works from
eslint_d
. Run:
$ eslint_d . --cache
Compatibility
4.0.0
: eslint 3.0+3.0.0
: eslint 2.2+1.0.0
, 2.0.0
: eslint 1.4+
License
MIT