typescript npxeslint--修复向vite.config.ts添加插件时的错误

1l5u6lss  于 2023-04-22  发布在  TypeScript
关注(0)|答案(1)|浏览(241)

由于某些原因,我在向vite.config.ts文件添加插件时得到了这个错误:
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()],
});

如果我只留下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,不知道是否相关。

b91juud3

b91juud31#

确保您的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:更改后重新加载Window 以确保已应用。

相关问题