Webpack在TypeScript错误上并未失败。

3

自从我升级到5版本后,webpack不会在TypeScript出现错误时失败。

这是我的情况:

index.ts(入口点):

const message: string = "Hello World";
console.log(message);
  • 我运行./node_modules/.bin/webpack --config webpack.config.js --watch
  • 一切正常。这是webpack的输出:
[webpack-cli] Compilation starting...
[webpack-cli] Compilation finished
asset index.js 50 bytes [compared for emit] [minimized] (name: main)
./src/index.ts 65 bytes [built] [code generated]
webpack 5.0.0 compiled successfully in 1251 ms
[webpack-cli] watching files for updates...
[webpack-cli] Compilation starting...
[webpack-cli] Compilation finished
asset index.js 50 bytes [emitted] [minimized] (name: main)
./src/index.ts 65 bytes [built] [code generated]
webpack 5.0.0 compiled successfully in 152 ms
[webpack-cli] watching files for updates...

在下一步中,我将添加一个代码片段,这应该会导致TypeScript错误:
const message: number = "Hello World"; // <= Type 'string' is not assignable to type 'number'.ts(2322)
console.log(message);
  • 我希望webpack的监视过程会失败或显示一些错误,但它并没有失败。以下是webpack输出的内容:
[webpack-cli] Compilation starting...
[webpack-cli] Compilation finished
asset index.js 50 bytes [emitted] [minimized] (name: main)
./src/index.ts 65 bytes [built] [code generated]
webpack 5.0.0 compiled successfully in 244 ms
[webpack-cli] watching files for updates...

有趣的是,只有在我启动webpack的监视进程后更改index.ts时才会出现这种情况。如果我不使用--watch选项运行webpack,则会得到此输出,并且不会创建任何捆绑文件(如预期所述):
[webpack-cli] Compilation finished
assets by status 50 bytes [cached] 1 asset
./src/index.ts 65 bytes [built] [code generated] [1 error]

ERROR in /Users/oliverschwendener/projects/test/src/index.ts
./src/index.ts
[tsl] ERROR in /Users/oliverschwendener/projects/test/src/index.ts(1,7)
      TS2322: Type 'string' is not assignable to type 'number'.

webpack.config.js:

const path = require('path');

const config = {
    entry: './src/index.ts',
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                use: 'ts-loader',
                exclude: /node_modules/,
            },
        ],
    },
    resolve: {
        extensions: [ '.tsx', '.ts', '.js' ],
    },
    output: {
        filename: 'index.js',
        path: path.resolve(__dirname, 'bundle'),
    },
};

module.exports = config;

*编辑:这是我的tsconfig.json文件。

{
  "compilerOptions": {
    "target": "es2015",
    "module": "commonjs",
    "lib": ["dom", "es2017"],
    "sourceMap": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "noUnusedLocals": true,
    "moduleResolution": "node"
  }
}

我无法想象这是webpack、webpack-cli或ts-loader的错误。所以我猜测我的配置有问题。有人能告诉我这里出了什么问题吗?谢谢!

  • webpack 5.3.1(使用webpack版本4.44.2一切正常)
  • webpack-cli 4.1.0
  • ts-loader 8.0.7
  • typescript 4.0.5
3个回答

1
在我的情况下,webpack v5.74.0由于设置缺少一个包而未能输出有关错误的 TypeScript 语法:fork-ts-checker-webpack-plugin(npm link)
安装并设置后,我终于可以在 webpack 编译过程中看到 TypeScript 错误了。

0

0
在你的tsconfig.json文件中,你可以将noEmitOnError设置为true,以确保如果出现错误,则进程会停止:
{
    "compilerOptions": {
        "noEmitOnError": true,

(如果没有明确设置,它默认为false

谢谢你的提示,这个方法可以解决问题,但如果出现错误,我只会看到"错误:TypeScript未生成任何输出",而不是实际的错误消息,告诉我问题所在以及找到失败文件的位置。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接