webpack 找不到 src,但应该不是在找

webpack can't find src, but it shouldn't be looking for it

我正在尝试了解如何将 typescript 与 React 结合使用。我正在浏览本教程:https://blog.logrocket.com/how-why-a-guide-to-using-typescript-with-react-fffb76c61614

当我 运行 通过 npm 脚本 "pile" 的 webpack 命令在我的 package.json 下面定义时,我得到:ERROR in Entry module not found: Error: Can't resolve './src' in '/my/home/directory/projects/tsx-tutorial'

这是我的项目文件夹的目录结构:

tsx-tutorial
├── app.tsx {<---- I want this to be my entry point right?}
├── dist
├── index.html
├── index.ts
├── lookTree.txt
├── node_modules.zip
├── package-lock.json
├── package.json
├── src {<----- there it is??}
│   └── hello.ts
├── tsconfig.json
└── webpack.config.js

2 directories, 10 files

这是我的 webpack.config.js:

var path = require("path");
var config = {
  entry: "app.tsx", // I'm not doing anything with ./src at all here??
  output: {
    path: path.resolve(__dirname, "build"),
    filename: "bundle.js"
  },
  resolve: {
    extensions: [".ts", ".tsx", ".js"]
  },
  module: {
    loaders: [
      {
        test: /\.tsx?$/,
        loader: "ts-loader",
        exclude: /node_modules/
      }
    ]
  }
};

我不是要任何带有 'src' 的东西,句号 - 为什么它会出现在那里?我觉得我缺少一些非常基本的东西。

最后 package.json:

{
  "name": "tsx-tutorial",
  "version": "0.0.1",
  "description": "to help me learn tsx-fu",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "pile": "webpack"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "ts-loader": "^4.4.1",
    "webpack": "^4.12.1",
    "webpack-cli": "^3.0.8"
  },
  "dependencies": {
    "@types/react": "^16.4.1",
    "@types/react-dom": "^16.0.6",
    "react": "^16.4.1",
    "react-dom": "^16.4.1"
  }
}

非常感谢您的帮助!感谢您帮助新手学习。

虽然它看起来就像你给了它一个配置,文件需要在底部导出它以便 webpack 看到它。

module.exports = config

解释错误:从版本 4 开始,如果未指定配置,webpack 默认使用 ./src 作为入口点。由于模块解析的工作方式,./src./src/index.js 的行为相同,但是文件名和扩展名是可配置的。