【问题标题】:Webpack is not failing on TypeScript errorWebpack 没有因 TypeScript 错误而失败
【发布时间】:2021-02-11 23:38:44
【问题描述】:

自从我升级到第 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...
  • 在下一步中,我添加了一个代码 sn-p,它会导致 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
  • 打字稿 4.0.5

【问题讨论】:

    标签: typescript webpack ts-loader webpack-cli


    【解决方案1】:

    在您的tsconfig.json 中,您可以将noEmitOnError 设置为true,以确保遇到错误时进程中断:

    {
        "compilerOptions": {
            "noEmitOnError": true,
    

    (没有显式设置,默认为false)

    【讨论】:

    • 感谢您的提示,这会起作用,但如果发生错误,我只会看到“错误:TypeScript 未发出输出”,而不是告诉我问题出在哪里以及在哪里找到的实际错误消息失败的文件。
    【解决方案2】:

    没关系,这是 ts-loader 中的一个错误:https://github.com/TypeStrong/ts-loader/issues/1204

    【讨论】:

      猜你喜欢
      • 2020-10-07
      • 2018-06-29
      • 2017-03-15
      • 2020-08-28
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多