Last time I talked about getting Parcel and some other tools set-up so that we can get started developing widgets with NodeCG. Iām not going to cover NodeCG widgets quite yet, but if you want to learn how you can keep your brand new development environment in a clean stateā¦ then Iāve got you covered today!
As I mentioned last time, you can see me apply these steps in order in this GitHub repository.
Alright, letās get started! š
Catching code and format smells with ESLint + Prettier
Our dev environment is setupāwhy go further? Well, sometimes we write code that is innocuous now but could be problematic in the future, or we might have other people working with us and itās useful to have a consistent style or format.
- ESLint can help us with part of this. We can use it to find and fix JavaScript (or in our case, TypeScript) problems
- Prettier is an opinionated code formatter so that we donāt have to think about how we format our code
Letās set those up!
ESLint
Installing ESLint is straightforward, but depends a lot on how you want to configure it. All you need to do is npm install --save-dev eslint
then npx eslint --init
to go through the wizard. I opted to pick the standard JS style but there are other styles available.
If you run eslint
now (npx eslint "src/**"
), it may warn you about some potential problems in the code weāve written (oh no!). Some of these problems are self-imposed, but others might feel unfixable, for example:
React must be in scope.
h is defined but never used.
How do we handle these?! š²
While our bundling process is aware of these variables, eslint
is not. To fix this, weāre going to npm install --save-dev eslint-config-standard-preact
1 and make a small changes to our .eslintrc
file.
"extends": [
"plugin:react/recommended",
"standard",
"standard-preact" // No need to add prefix; eslint is smart like that
],
ā¦And if you run eslint
again, the errors should hopefully go away. If they donāt, itās possible that we missed something. Hopefully thereās enough information to resolve the issues if so š¤
Prettier
That leaves Prettier. Honestly, if itās just one person working on the project, adding this might be a bit of overkill, but itās easy to add and leaves code in a consistent style so Iād still recommend it (and it works for multiple different file types).
Installation is pretty easy, and once youāve done that, you can run npx prettier --write .
to format all support files in your project!
ā¦But donāt do that just yet! Prettier and ESLint may not agree on formattingārunning Prettier could create style-based linting errors š±
So weāll need to fix that! Fortunately, Prettier has some documentation on a few different ways you can resolve this depending on your setup. In our case, we can go the recommended route and npm install --save-dev eslint-config-prettier
and add it to the end of our .eslintrc
āextendsā as before:
"extends": [
"plugin:react/recommended",
"standard",
"standard-preact",
"prettier" // Again, no prefix necessary
],
More details on the eslint-config-prettier
plugin are here. It will disable several rules but thatās worth it to make sure that the two programs donāt fight.
And voila, weāve made our environment a little bit cleaner! āØ
ā¦But can we do more? š¤š
Optional: Integrating with Atom
Itās great that we have these tools, but running them manually still kind of sucks. Manual tools are tools that are not run very often.
There are a few different ways to fix this.
- Git hooks: automatically run our tools before committing. Prettier even has some documentation on a few different ways of doing this including a tool called Husky which is intended to make git hooks easier.
- Automatically run tools in your IDE: As long as you arenāt running notepad, you can probably set this up
- Build toolkits (e.g. Gulp, Grunt, possibly even webpack or Parcel, CI): Have the tools run as part of the build process!
Heck, you can probably implement a bunch of these simultaneouslyāwhich also reduces the likelihood of it being missedābut weāre going to focus on setting it up in the IDE for now. š
I use Atom, so most of the following steps will be specific to it, but Iām sure similar plugins or settings are available in most IDEs.
A quick rundown of the packages I installed, and the settings I had to change:
linter
: This allows any sort of linting in the Atom UI. However, it needs specific linters installed to workā¦linter-eslint
: Allows us to useeslint
with linter! Atom will look for a version ofeslint
in the project, and if it canāt find one, will use a version that it is prepackeged with.- By default, it doesnāt know to lint
.ts
and.tsx
files. To fix that, youāll need to go into Atomās settings > Packages > linter-eslint, and under itās settings addsource.ts, source.tsx
to the list of scopes.
- By default, it doesnāt know to lint
prettier-atom
: Surprise! This will allow Atom to run Prettier.- Most settings you can leave as is, but Iād strongly suggest setting it to āFormat on saveā and also āOnly format if a Prettier config is foundā. This avoids the issue of formatting other files in Atom unexpectedly for projects not using Prettier.
atom-typescript
: This lets Atom know about Typescript files so it can syntax highlight correctly and provide other useful typing hints
Thereās also one last one which is super unnecessary for an individual project, but Iām including for completeness because I think itās a cool idea to avoid arguments in groups: EditorConfig. š¤
In a nutshell, EditorConfig is another way of maintaining consistent code styles, but instead of doing it after the fact, like Prettier, it does so while youāre coding. Itās mostly for things like indentation amounts and type (tabs vs spaces), character sets, and line endings. Itās a little less necessary since weāre using Prettier, but a useful tool to add to your toolkit (and is supported by many IDEs).
To get it to work in Atom, we just need to install editorconfig
and add a basic file to our project at the root. I picked these settings fairly arbitrarily based on my preferences:
[*]
end_of_line = lf
charset = utf-8
indent_style = space
indent_size = 2
max_line_length = 80
And thatās it! We now have a really tidy setup for our NodeCG development! I hope it wonāt be too long before I can talk more about how to actually develop widgets, but I wanted to show you all how you can set things up cleanly forā¦ well, most front end projects! š„³
eslint-config-standard-preact
is quite old at this point (~4 years since a new version was published), so there may be better fixes for this problemā¦ Iām just not aware of better alternatives at the moment.ā©