Javascript 如何使用webpack 5只编译一个typescript文件,而排除所有其他文件?

Javascript 如何使用webpack 5只编译一个typescript文件,而排除所有其他文件?,javascript,node.js,reactjs,typescript,webpack,Javascript,Node.js,Reactjs,Typescript,Webpack,我有一个Node.js运行时,为react应用程序提供服务 ,以下是文件夹结构: 我在react应用程序中运行npm运行build,以编译react应用程序本身 然后我想从项目的根运行npm run build,只将index.ts文件编译到/dist/index.js,并忽略react app文件夹中的内容 这是我的webpack.config.ts: const webpack = require("webpack"); const path2 = require(&q

我有一个Node.js运行时,为react应用程序提供服务

,以下是文件夹结构:

我在react应用程序中运行
npm运行build
,以编译react应用程序本身

然后我想从项目的根运行
npm run build
,只将index.ts文件编译到/dist/index.js,并忽略react app文件夹中的内容

这是我的webpack.config.ts:

const webpack = require("webpack");
const path2 = require("path");
const nodeExternals = require("webpack-node-externals");
const copyFiles = require('copy-webpack-plugin');

module.exports = {
    entry: './index.ts',
    target: "node",
    mode: "production",
    externals: [nodeExternals()],
    resolve: {
        extensions: ['.ts']
    },
    module: {
        rules: [
            // all files with a `.ts` extension will be handled by `ts-loader`
            { test: /index.ts$/, loader: 'ts-loader', exclude: [ path2.resolve(__dirname, 'react-app')]}
        ]
    },
    plugins: [new webpack.HotModuleReplacementPlugin(), new copyFiles({ patterns: [{ from: 'react-app/build', to: 'build'}]})],
    output: {
        path: path2.resolve(__dirname, 'dist'),
        filename: 'index.js'
    },
};
我尝试过只包含index.ts文件,并以百万种方式排除react应用程序文件夹,但webpack一直在尝试编译react应用程序文件夹中的ts文件。

这是我的package.json:

{
  "name": "frontend-service",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node dist/index.js",
    "build": "webpack",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"
  },
  "devDependencies": {
    "@types/express": "^4.17.11",
    "@types/node": "^14.14.31",
    "copy-webpack-plugin": "^7.0.0",
    "ts-loader": "^8.0.17",
    "ts-node": "^9.1.1",
    "typescript": "^4.2.2",
    "webpack": "^5.24.2",
    "webpack-cli": "^4.5.0",
    "webpack-node-externals": "^2.5.2"
  }
}

请帮助。

我最初认为评论是错误的,因为我在我的tsconfig的“compileOptions”中添加了“include”:[“index.ts”]并得到:

error TS5023: Unknown compiler option 'include'.
但显然我不得不把它放在编译之外,比如:

{
  "compilerOptions": {
    // Your options.....
  },
  "include": ["./index.ts"]
}
这就行了

但是,react项目将不再生成,因为它发现:

a different version of webpack-dev-server was detected higher up in the tree
我建议不要将react typescript项目放在节点typescript项目中。最好创建一个根文件夹,并将react项目放在节点项目旁边的文件夹中。

tsconfig.json