Introduction
My latest projects at work have evolved from being PHP / Laravel applications with sprinklings of Vue JS on the front end to being almost entirely Vue JS components driving the frontend with Laravel backends.
Whilst laravel ships with Laravel-Mix to make webpack / asset compilation easier it doesn’t ship with anyway to lint your javascript.
Luckily adding eslint and defining rules for your .js and .vue files to follow is pretty straight forward.
Getting Started
Whilst you can install eslint globally on your system I’d recommend adding it on a per-project basis so that you can include running eslint as a build step within your CI/CD process.
Add eslint to your projects package.json file.
1 | # Yarn |
Create a base configuration
1 | ./node_modules/.bin/eslint --init |
The above command will ask you a number of questions for my laravel projects I use the following:
1 | Are you using ECMAScript 6 features? Yes |
You’ll end up with a new file called .eslintr.json within your project root that has the following content generated as a result of the responses to the questions above.
1 | { |
At this point eslint is now set up and ready to be used in your project.
1 | ./node_modules/.bin/eslint ./resources/assets/js/ |
Running the above should give you some sort output like the following (unless you have JS that adheres to all of the standards of course!):
1 | /resources/assets/js/app.js |
Running the same command with the --fix argument should get eslint to attempt to fix as many of the above errors as it can.
Linting .vue files
Right now we can lint .js files but if we’re using .vue single file components then trying to run eslint on them will give us an error like this:
1 | ./node_modules/.bin/eslint ./resources/assets/js/components/*.vue --fix |
We need to tell eslint how to parse .vue files and what rules to apply to them.
1 | # Yarn |
Next register the eslint-vue-plugin in your .eslint.json file by adding it to the extends block:
1 | ... |
There are actually a number of different default config values for the eslint vue plugin that add increasingly stricter rule sets to be applied to the linter. They are as follows:
1 | plugin:vue/base |
Now running eslint against .vue files will result in expected output:
1 | /resources/assets/js/components/Example.vue |
Adding a new build step
Finally we can add eslint to our build process. For my work projects I use gitlab ci/cd.
Start by adding a new script to our package.json file called eslint which we can call from gitlab:
1 | { |
This script tells eslint will look for all files with the .js and .vue extension recursively within the resources/assets/js/ and test folders of my project.
Next lets update our .gitlab-ci.yml file to add the new build step.
If you don’t have a stage for syntax defined first do that:
1 | # Define pipeline stages |
Next add the job for eslint:
1 | eslint: |
And that’s it! Commit some code and watch your CI process check the code for errors!
Finally
I found that I had a few rules defined by default that bugged me so i turned them off. My current .eslint.json file looks like this:
1 | { |