npx eslint –fix 在将插件添加到 vite.config.ts 时出错。

huangapple go评论62阅读模式
英文:

npx eslint --fix error when adding plugins to vite.config.ts

问题

Here's the translated content:

"出现以下错误,当将插件添加到 vite.config.ts 文件时:

1:1 错误 解析错误:'import' 和 'export' 只能在 'sourceType: module' 中出现

vite.config.ts:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import eslint from 'vite-plugin-eslint';

export default defineConfig({
    plugins: [react(), eslint()],
});

如果我只保留 react 插件,它可以正常工作,但无论我添加哪个额外组件都会导致此问题,这是我的 eslintrc 文件:

.eslintrc.json:

{
    "extends": ["eslint:recommended", "plugin:node/recommended", "prettier"],
    "plugins": ["node", "prettier"],
    "rules": {
        "prettier/prettier": "error",
        "block-scoped-var": "error",
        "eqeqeq": "error",
        "no-var": "error",
        "prefer-const": "error",
        "eol-last": "error",
        "prefer-arrow-callback": "error",
        "no-trailing-spaces": "error",
        "quotes": ["warn", "single", {"avoidEscape": true}],
        "no-restricted-properties": [
            "error",
            {
                "object": "describe",
                "property": "only"
            },
            {
                "object": "it",
                "property": "only"
            }
        ]
    },
    "overrides": [
        {
            "files": ["src/**/*.ts", "src/**/*.tsx"],
            "parser": "@typescript-eslint/parser",
            "extends": ["plugin:@typescript-eslint/recommended"],
            "rules": {
                "@typescript-eslint/no-non-null-assertion": "off",
                "@typescript-eslint/no-use-before-define": "off",
                "@typescript-eslint/no-warning-comments": "off",
                "@typescript-eslint/no-empty-function": "off",
                "@typescript-eslint/no-var-requires": "off",
                "@typescript-eslint/explicit-function-return-type": "off",
                "@typescript-eslint/explicit-module-boundary-types": "off",
                "@typescript-eslint/ban-types": "off",
                "@typescript-eslint/camelcase": "off",
                "node/no-missing-import": "off",
                "node/no-empty-function": "off",
                "node/no-unsupported-features/es-syntax": "off",
                "node/no-missing-require": "off",
                "node/shebang": "off",
                "no-dupe-class-members": "off",
                "require-atomic-updates": "off"
            },
            "parserOptions": {
                "ecmaVersion": 2018,
                "sourceType": "module"
            }
        }
    ]
}

我还使用 TypeScript 5.0.3,不知道是否相关。"

英文:

For some reason I'm getting this error when adding plugins to the vite.config.ts file :

1:1 error Parsing error: 'import' and 'export' may appear only with 'sourceType: module'

vite.config.ts :

import {defineConfig} from 'vite';
import react from '@vitejs/plugin-react';
import eslint from 'vite-plugin-eslint';
export default defineConfig({
plugins: [react(), eslint()],
});

If I leave only the react plugin it works fine, but whatever extra component I add will cause this issue, this is my eslintrc file :

.eslintrc.json :

{
"extends": ["eslint:recommended", "plugin:node/recommended", "prettier"],
"plugins": ["node", "prettier"],
"rules": {
"prettier/prettier": "error",
"block-scoped-var": "error",
"eqeqeq": "error",
"no-var": "error",
"prefer-const": "error",
"eol-last": "error",
"prefer-arrow-callback": "error",
"no-trailing-spaces": "error",
"quotes": ["warn", "single", {"avoidEscape": true}],
"no-restricted-properties": [
"error",
{
"object": "describe",
"property": "only"
},
{
"object": "it",
"property": "only"
}
]
},
"overrides": [
{
"files": ["src/**/*.ts", "src/**/*.tsx"],
"parser": "@typescript-eslint/parser",
"extends": ["plugin:@typescript-eslint/recommended"],
"rules": {
"@typescript-eslint/no-non-null-assertion": "off",
"@typescript-eslint/no-use-before-define": "off",
"@typescript-eslint/no-warning-comments": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/ban-types": "off",
"@typescript-eslint/camelcase": "off",
"node/no-missing-import": "off",
"node/no-empty-function": "off",
"node/no-unsupported-features/es-syntax": "off",
"node/no-missing-require": "off",
"node/shebang": "off",
"no-dupe-class-members": "off",
"require-atomic-updates": "off"
},
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
}
}
]
}

I'm also using typescript 5.0.3, don't know if it's relevant.

答案1

得分: 1

Ensure your tsconfig.json 文件包含 vitest.config.ts:

{
  "include": ["src/**/*.ts", "src/**/*.tsx", "vitest.config.ts"]
}

然后确保你的 .eslintrc.json 文件包含 vitest.config.ts:

"overrides": [
  {
    "files": ["src/**/*.ts", "src/**/*.tsx", "vitest.config.ts"],
  }
]

或者,编辑 .eslintrc.json 以配置默认解析器(而不是 overrides 解析器):

{
  "parserOptions": {
    "ecmaVersion": "2018",
    "project": "./tsconfig.json",
    "sourceType": "module"
  }
}

如果仍然不起作用,尝试添加以下内容:

{
  "ignorePatterns": ["!vitest.config.ts"]
}

你需要将它添加为配置的顶级键。

你没有说明错误是如何生成的。如果是命令行,则可以忽略下面的备注。但如果是在VSCode中直接生成的错误,请在更改后执行Developer: Reload Window以确保应用了更改。

英文:

Ensure your tsconfig.json file includes vitest.config.ts:

{
  "include": ["src/**/*.ts", "src/**/*.tsx", "vitest.config.ts"]
}

Then ensure your .eslintrc.json file includes vitest.config.ts:

"overrides": [
  {
    "files": ["src/**/*.ts", "src/**/*.tsx", "vitest.config.ts"],
  }
]

Alternatively, edit .eslintrc.json to configure the default parser (instead of the overrides parser):

{
  "parserOptions": {
    "ecmaVersion": "2018",
    "project": "./tsconfig.json",
    "sourceType": "module"
}

If that still doesn't work then try adding this:

{
  "ignorePatterns": ["!vitest.config.ts"]
}

You need to add it as a top-level key in the config.

You don't say how you're generating the error. If it's command line then you can ignore this next remark. But if it's directly within VSCode then do Developer: Reload Window after the change to ensure it's applied.

huangapple
  • 本文由 发表于 2023年4月13日 15:35:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76002790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定