Skip to content

Instantly share code, notes, and snippets.

@hawkeye64
Last active January 11, 2021 17:04
Show Gist options
  • Save hawkeye64/d839d9bf9b4769eb45b452e0bfc524cb to your computer and use it in GitHub Desktop.
Save hawkeye64/d839d9bf9b4769eb45b452e0bfc524cb to your computer and use it in GitHub Desktop.
// -----------------------------------------------
// eslint-plugin.js - put into plugins folder
// see below for usage
// -----------------------------------------------
const path = require('path')
const fs = require('fs').promises
const ESLint = require('eslint').ESLint
const cwd = process.cwd()
const eslint = new ESLint({ cwd })
let formatter = ESLint.Formatter
module.exports = (
options = { extNames: [ '.js', '.jsx', '.ts', '.tsx', '.vue' ] }
) => {
return {
name: 'eslint-plugin',
transforms: [
{
test ({ path: filepath }) {
return (
filepath.startsWith(cwd)
&& filepath.indexOf('node_modules') < 0
&& options.extNames.includes(path.extname(filepath))
)
},
async transform ({
code,
path: filepath
}) {
if (!formatter) {
formatter = await eslint.loadFormatter('stylish')
}
const lintResultList = await eslint.lintText(
await fs.readFile(filepath, { encoding: 'utf8' }),
{
filePath: filepath,
warnIgnored: false
}
)
if (lintResultList && lintResultList.length) {
const text = formatter.format(lintResultList)
if (text && text.trim().length) {
console.log(text)
}
}
return code
}
}
]
}
}
// -----------------------------------------------
// In vite.config.js add the following:
const eslintPlugin = require('./plugins/eslint-plugin.js')
module.exports = {
plugins: [
eslintPlugin()
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment