首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在项目中添加eslint-webpack-plugin,以提供打印文本。

在项目中添加eslint-webpack-plugin,以提供打印文本。
EN

Stack Overflow用户
提问于 2020-10-21 10:29:26
回答 2查看 14.1K关注 0票数 12

我很难在Webpack的项目中插上打字稿林特。

显然,eslint-loader是不受欢迎的,他们给eslint-webpack-plugin作为替代方案。此外,我使用的是typescript-eslint/parser,而不是tslint (不推荐的)--问题是,我在.eslintrc.js中设置的规则似乎没有得到考虑。我在.eslintrc中添加了一个简单的规则,在使用分号时应该会抛出一个错误。

代码语言:javascript
复制
export default class A {
  static yes = 'this is static'; // ";" <- should be invalidated by eslint
}

我的依赖关系是:

代码语言:javascript
复制
"@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"

如果我正确理解,它应该是这样工作的:

代码语言:javascript
复制
                                                        # # # # # # # # # # # # # # 
@typescript-eslint/parser --> eslint-webpack-plugin --> #                         # 
      (uses eslint?)        (replaces eslint-loader)    #       webpack v4        #
                                                        #  w/ webpack-dev-server  #
       typescript         -->       ts-loader       --> #                         #
                                                        # # # # # # # # # # # # # #

附带注意:我使用的是webpack-dev-server,而不是的 服务命令。

启动服务器的命令:webpack-dev-server --hot --inline

  • 项目结构:

代码语言:javascript
复制
./
├─── src
│    └─── index.ts
└─── dist
     ├─── bundle.js
     └─── index.html <- imports bundle.js

  • webpack.config.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
{
  "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"
  ]
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-11-24 05:53:03

在尝试将eslint-webpack-plugin.vue文件一起使用时,我遇到了类似的问题。

我的解决方案是使用extensions设置而不是files。试试这个:

代码语言:javascript
复制
new ESLintPlugin({
  extensions: ['ts']
})
票数 20
EN

Stack Overflow用户

发布于 2022-08-23 23:58:57

我修复了这个问题,我当前的ESLintPlugin options对象如下:

代码语言:javascript
复制
const lintJSOptions = {
  context: path.join(paths.context, paths.js),
  extensions: ['js', 'ts'],
  emitError: true,
  emitWarning: true,
  failOnWarning: false,
  failOnError: false,
  // Toggle autofix
  fix: false,
  cache: false,

  formatter: require('eslint-friendly-formatter'),
};

希望这能有所帮助。

PD:context属性是*.js或*.ts文件相对于package.json文件的根位置,您可以使用path.resolve(__dirname, './src/scripts');或任何存储脚本的地方。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64461635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档