Implement lint
This commit is contained in:
@@ -16,6 +16,7 @@ export default defineConfig(({ mode }) => {
|
||||
publicDir: false,
|
||||
plugins: [
|
||||
livetrailPublicAssets(),
|
||||
livetrailLint(isDev),
|
||||
vue()
|
||||
],
|
||||
build: {
|
||||
@@ -62,6 +63,37 @@ export default defineConfig(({ mode }) => {
|
||||
};
|
||||
});
|
||||
|
||||
function livetrailLint(isDev) {
|
||||
return {
|
||||
name: 'livetrail-lint',
|
||||
apply: 'build',
|
||||
//In `--watch` mode Vite re-runs the whole plugin pipeline (including
|
||||
//buildStart) on every rebuild it triggers from a file change, so this
|
||||
//alone gives re-linting on every save with no separate watchChange
|
||||
//wiring needed.
|
||||
async buildStart() {
|
||||
await runLint(isDev);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async function runLint(isDev) {
|
||||
const { ESLint } = await import('eslint');
|
||||
const eslint = new ESLint({ cwd: ROOT });
|
||||
const results = await eslint.lintFiles(['src']);
|
||||
|
||||
const formatter = await eslint.loadFormatter('stylish');
|
||||
const output = await formatter.format(results);
|
||||
if (output) process.stdout.write(output + '\n');
|
||||
|
||||
//Dev keeps watching regardless - the point is fast feedback, not a gate.
|
||||
//Prod aborts the build so bad code can't ship.
|
||||
const hasErrors = results.some((result) => result.errorCount > 0);
|
||||
if (hasErrors && !isDev) {
|
||||
throw new Error('ESLint found errors in src/ - aborting production build.');
|
||||
}
|
||||
}
|
||||
|
||||
function livetrailPublicAssets() {
|
||||
return {
|
||||
name: 'livetrail-public-assets',
|
||||
|
||||
Reference in New Issue
Block a user