gpt4 book ai didi

webpack - 将 eslint-webpack-plugin 添加到项目中以提供 Typescript linting

转载 作者:行者123 更新时间:2023-12-04 12:14:02 32 4
gpt4 key购买 nike

我很难在我的 Webpack 项目中插入 Typescript linter。
显然 eslint-loader 已弃用,他们提供 eslint-webpack-plugin 作为备选。此外,我正在使用 typescript-eslint/parser 而不是 tslint (已弃用)问题是我在 .eslintrc.js 中设置的规则似乎没有被计算在内。我在 .eslintrc 中添加了一个简单的规则使用分号时应该会抛出错误。

export default class A {
static yes = 'this is static'; // ";" <- should be invalidated by eslint
}
我的依赖项是:
"@typescript-eslint/eslint-plugin": "^4.5.0",
"@typescript-eslint/parser": "^4.5.0",
"eslint": "^7.11.0",
"eslint-webpack-plugin": "^2.1.0",
"ts-loader": "^8.0.6",
"typescript": "^4.0.3",
"webpack": "^4.41.3",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0"
如果我理解正确,它应该像这样工作:
                                                        # # # # # # # # # # # # # # 
@typescript-eslint/parser --> eslint-webpack-plugin --> # #
(uses eslint?) (replaces eslint-loader) # webpack v4 #
# w/ webpack-dev-server #
typescript --> ts-loader --> # #
# # # # # # # # # # # # # #
旁注:我正在使用 webpack-dev-server而不是 webpack-cli 的服务命令其中 doesn't seem to work quite yet with webpack v5 .
我运行的启动服务器的命令: webpack-dev-server --hot --inline enter image description here
  • 项目结构:

  • ./
    ├─── src
    │ └─── index.ts
    └─── dist
    ├─── bundle.js
    └─── index.html <- imports bundle.js
  • webpack.config.js :

  • const path = require('path')
    const ESLintPlugin = require('eslint-webpack-plugin')

    module.exports = {
    mode: 'development',
    entry: './src/index.ts',
    devtool: 'inline-source-map',
    devServer: {
    contentBase: path.resolve(__dirname, 'dist')
    // publicPath: "/assets", // path of the served resources (js, img, fonts...)
    },
    module: {
    rules: [
    {
    test: /\.tsx?$/,
    use: 'ts-loader',
    exclude: /node_modules/,
    },
    ],
    },
    plugins: [
    new ESLintPlugin({
    files: 'src/**/*.ts',
    })
    ],
    resolve: {
    extensions: [ '.tsx', '.ts', '.js' ],
    },
    output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
    },
    }
  • .eslintrc.js :

  • module.exports = {
    root: true,
    env: {
    node: true,
    },
    parser: '@typescript-eslint/parser',
    extends: [
    'plugin:@typescript-eslint/recommended'
    ],
    parserOptions: {
    ecmaVersion: 2020,
    sourceType: 'module',
    },
    rules: {
    'semi': ['error', 'never'],
    },
    ignorePatterns: [ "dist/*" ],
    }
  • tsconfig.json :

  • {
    "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "paths": {
    "@/*": [
    "src/*"
    ]
    },
    "lib": [
    "dom",
    "dom.iterable",
    ]
    },
    "include": [
    "src/**/*.ts",
    "tests/**/*.ts",
    ],
    "exclude": [
    "node_modules"
    ]
    }

    最佳答案

    我在尝试使用 eslint-webpack-plugin 时遇到了类似的问题。与 .vue文件。
    我的解决方案是使用 extensions设置而不是 files .尝试这个:

    new ESLintPlugin({
    extensions: ['ts']
    })

    关于webpack - 将 eslint-webpack-plugin 添加到项目中以提供 Typescript linting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64461635/

    32 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com