Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update eslint config in React templates #13550

Merged
merged 3 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/create-vite/template-preact-ts/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import { render } from 'preact'
import { App } from './app.tsx'
import './index.css'

render(<App />, document.getElementById('app') as HTMLElement)
render(<App />, document.getElementById('app')!)
15 changes: 13 additions & 2 deletions packages/create-vite/template-react-ts/.eslintrc.cjs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would the new ESLint config and existing TS config still work in monorepos?

Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'plugin:react-hooks/recommended',
],
parser: '@typescript-eslint/parser',
parserOptions: { ecmaVersion: 'latest', sourceType: 'module' },
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: true,
tsconfigRootDir: __dirname,
},
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': 'warn',
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs mention that Vite supports this option, but what if inside this Vite project are files run by another HMR implementation that doesn't support it?

Copy link
Member Author

@ArnaudBarre ArnaudBarre Jun 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this complex setup is out of scope of a Vite template. I don't consider this config to be perfect for every use cases, just a good starting for people using Vite in place of CRA.
(Idem for monorepo stuff, we people have already their linting configure they can delete the file.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not saying this template should set up other tools, I'm saying it should avoid breaking other tools when we can't make assumptions about what they are. I think this is actually more important with a minimal template, as we don't include common tools like doc generators or test frameworks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nickmccurdy do you have a common example of vite + a tool that will break after adding this config? I agree with the idea you're pushing here, but I don't know if it would apply to this particular option.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not aware of any specific counterexamples, sorry. Hopefully the docs for allowConstantExport help:

Don't warn when a constant (string, number, boolean, templateLiteral) is exported aside one or more components.

This should be enabled if the fast refresh implementation correctly handles this case (HMR when the constant doesn't change, propagate update to importers when the constant changes.). Vite supports it, PR welcome if you notice other integrations works well.

],
'@typescript-eslint/no-non-null-assertion': 'off',
},
}
2 changes: 1 addition & 1 deletion packages/create-vite/template-react-ts/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import './index.css'

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<App />
</React.StrictMode>,
Expand Down
5 changes: 4 additions & 1 deletion packages/create-vite/template-react/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ module.exports = {
settings: { react: { version: '18.2' } },
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': 'warn',
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
}