Extract warnings from create-react-app without running

(Peter Bengtsson) #1

Suppose that I have an unused variable in my src/App.js. With create-react-app you get a nice warning in the terminal about this when you start (yarn start). But what if I don’t ever want to accept any warnings and would like to break pull requests if there are warnings. Is there any way to do that?

One possible way to do it is to use CI=true yarn build. Example:

▶ CI=true yarn build && echo $?
yarn run v1.3.2
$ react-scripts build
Creating an optimized production build...

Treating warnings as errors because process.env.CI = true.
Most CI servers set it automatically.

Failed to compile.

  Line 7:  'variable' is assigned a value but never used  no-unused-vars

error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

That’s not bad but it does require a full yarn build which will definitely add to the CI time.
Is there a way to get the warnings without having to do a build?

I.e. Ideally something like this:

yarn anywarnings

(Pr0da) #2


You can run eslint separately with the create-react-app rule set:

Add an .eslintrc file:

  "extends": "react-app"

Add a script to package.json:

"lint": "eslint '**/*.js'",

Run with yarn lint or npm run lint.

Ofc you can refine with git hook (lint-staged, husky) and prettier integration, but the base idea is that simple.

(Danny Burnett) #3

If I’m assuming correctly that this is caused by ESLint, you can suppress the logs / warnings by commenting // eslint-disable-line no-unused-vars.

(Peter Bengtsson) #4

Cool! I didn’t even know about that trick. But it sounds ideal. Haven’t tried it yet.