Skip to content

Commit

Permalink
esbuild-{es,style}lint-plugin.js: Fix error behaviour
Browse files Browse the repository at this point in the history
Properly report errors. Unfortunately errors cause eslint watch mode to
abort the watch loop, so add a hack to reduce them to warnings in watch
mode. Warnings don't fail a normal "npm run build" [1], so we have to
make this dynamic for now.

[1] eslint/eslint#16804
  • Loading branch information
martinpitt committed Feb 20, 2023
1 parent a2e8396 commit 1cf6b07
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 10 additions & 3 deletions esbuild-eslint-plugin.js
Expand Up @@ -4,8 +4,10 @@

import { ESLint } from 'eslint';

const NAME = 'eslintPlugin';

export const eslintPlugin = {
name: 'eslintPlugin',
name: NAME,
setup(build) {
const filesToLint = [];
const eslint = new ESLint();
Expand All @@ -20,9 +22,14 @@ export const eslintPlugin = {
const formatter = await eslint.loadFormatter('stylish');
const output = formatter.format(result);
if (output.length > 0) {
// eslint-disable-next-line no-console
console.log(output);
console.log(output); // eslint-disable no-console
// HACK: errors abort watch mode, but warnings don't fail build
const key = (process.env.ESBUILD_WATCH === "true") ? 'warnings' : 'errors';
return {
[key]: [{ pluginName: NAME, text: 'ESLint errors found' }]
};
}
return null;
});
},
};
12 changes: 10 additions & 2 deletions esbuild-stylelint-plugin.js
Expand Up @@ -3,11 +3,13 @@
import * as stylelint from 'stylelint';
import * as formatter from 'stylelint-formatter-pretty';

const NAME = 'stylelintPlugin';

export const stylelintPlugin = ({
filter = /\.(s?css)$/,
...stylelintOptions
} = {}) => ({
name: 'stylelintPlugin',
name: NAME,
setup(build) {
const targetFiles = [];
build.onLoad({ filter }, ({ path }) => {
Expand All @@ -24,8 +26,14 @@ export const stylelintPlugin = ({
});
const { output } = result;
if (output.length > 0) {
console.log(output);
console.log(output); // eslint-disable no-console
// HACK: errors abort watch mode, but warnings don't fail build
const key = (process.env.ESBUILD_WATCH === "true") ? 'warnings' : 'errors';
return {
[key]: [{ pluginName: NAME, text: 'stylelint errors found' }]
};
}
return null;
});
}
});

0 comments on commit 1cf6b07

Please sign in to comment.