使用 TypeScript 输入脚本的 Webpack 没有发出输出错误

我从 https://www.npmjs.com/package/ts-loader得到了这个配置:

Webpack.config.js:

var path = require('path');
var webpack = require('webpack');


module.exports = {
mode: "development",
devtool: "inline-source-map",
entry: "./src/Api.ts",
output: {
filename: "bundle.js"
},
resolve: {
// Add `.ts` and `.tsx` as a resolvable extension.
extensions: [".ts", ".tsx", ".js"]
},
module: {
rules: [
// all files with a `.ts` or `.tsx` extension will be handled by `ts-loader`
{ test: /\.tsx?$/, loader: "ts-loader" }
]
}
};

./src/Api. ts:

export class Api {
...
}

但是当我运行 webpack 时,我得到:

Error: TypeScript emitted no output for Api.ts
50722 次浏览

检查 tsconfig.json文件中没有将 noEmit设置为 true

首先将 webpack 配置条目(如 index.js)更改为 index.tsx。 第二,确保为 tsx 文件添加了如下规则:

{
test: /\.(ts|js)x?$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-typescript",
],
},
},
},