robinloeffel / esbuild-plugin-eslint

Lint your esbuild bundles with eslint. 🧐
https://npmjs.com/esbuild-plugin-eslint
MIT License
7 stars 7 forks source link

Report errors and warnings back to esbuild #4

Closed jgray-indigobio closed 1 year ago

jgray-indigobio commented 1 year ago

If ESLint warnings or errors are encountered, they aren't reported back to esbuild's build result. That means from esbuild's perspective, the build still succeeded.

As a workaround, we manually grep through the esbuild output for ESLint errors, because we want the build to fail if there are any ESLint errors. It's possible that we'd want this behavior configurable--e.g. fail in production builds, but succeed in dev builds. However, since the plugin uses onEnd it's unclear if the bundles would still be written even if there are errors (desired behavior for dev builds, to be able to quickly write some noncompliant debugging code and try it out).

The esbuild docs make this sound relatively straightforward, but I'd consider submitting a pull request for this behavior.

martinpitt commented 1 year ago

You can do it like this:

            if (output.length > 0) {    
                console.log(output); // eslint-disable no-console    
                return {    
                    errors: [{ pluginName: 'eslint', text: 'ESLint errors found' }]    
                };    
            }    
            return null;    

If you care, you can of course also make this configurable, to raise warnings: instead.

jgray-indigobio commented 1 year ago

@martinpitt Yep, that's what I've done with a custom local plugin, which sounds like the route you all have taken too. Thanks!

martinpitt commented 1 year ago

Yes, we use this local plugin now. But I think it'd be nicer to just establish a well-working standard plugin on npmjs.com. Thanks!

robinloeffel commented 1 year ago

in the latest release, i've added options for throwOnWarning and throwOnError. please check it out and report back if those work for you!

KKoukiou commented 1 year ago

The new options do not work as expected. In order for the plugin to throw an exception on warnings, you need to return the warnings in a 'error' object to esbuild results.

Additionally you want 'warningCount' here, https://github.com/robinloeffel/esbuild-plugin-eslint/blob/main/source/index.js#L48 instead of the undefined 'warning' variable.

Lastly last release has a massive problem https://github.com/robinloeffel/esbuild-plugin-eslint/issues/6 which makes it unusable. If you would like I can send a PR to fix all these.