【发布时间】:2021-02-04 06:05:46
【问题描述】:
我很难在我的 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
- 项目结构:
./
├─── 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"
]
}
【问题讨论】:
标签: webpack eslint typescript-eslint