简体   繁体   English

Webpack 未编译

[英]Webpack is not compiling

With my React-Django project I'm running into an issue with Webpack.在我的 React-Django 项目中,我遇到了 Webpack 的问题。 It's not generating the compiled file (main.js) like I expected and I'm not sure what the problem is.它没有像我预期的那样生成编译文件(main.js),我不确定问题是什么。 The file tree looks as follows:文件树如下所示:

|-- __init__.py
|-- admin.py
|-- apps.py
|-- migrations
|   -- __init__.py
|-- models.py
|-- package-lock.json
|-- package.json
|-- src
|   |-- components
|   |   -- App.js
|   -- index.js
|-- static
|   -- frontend
|-- templates
|   -- frontend
|       -- index.html
|-- tests.py
|-- urls.py
|-- views.py
|-- webpack.config.js

I configured webpack like this:我这样配置 webpack :

const path = require('path');

module.exports = {
  entry: [
    "./src/index.js"
  ],
  output: {
    filename: "main.js",
    path: path.resolve(__dirname, 'static', 'frontend')
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  }
};

I'm running my react project with npm run dev , where the dev script is "dev": "webpack serve --mode development . This package.json looks as follows:我正在使用npm run dev运行我的反应项目,其中开发脚本是"dev": "webpack serve --mode development 。这个 package.json 如下所示:

{
  "name": "frontend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "webpack serve --mode development",
    "build": "webpack --mode production"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.13.1",
    "@babel/preset-env": "^7.13.5",
    "@babel/preset-react": "^7.12.13",
    "babel-loader": "^8.2.2",
    "html-webpack-plugin": "^5.2.0",
    "path": "^0.12.7",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "webpack": "^5.24.2",
    "webpack-cli": "^4.5.0",
    "webpack-dev-server": "^3.11.2"
  }
}

I've been looking for the mistake for a couple of hours now so I hope someone can help me out here:)我一直在寻找错误几个小时,所以我希望有人可以在这里帮助我:)

When you start webpack with webpack dev server (which you do with webpack serve ), it compiles everything into memory and serve from there.当您使用 webpack 开发服务器(您使用webpack serve )启动 webpack 时,它会将所有内容编译成 ZCD691B4957F096CDE98 中的所有内容。 If you want to emit files you, probably don't need dev server, so just startgin webpack watch would be sufficient.如果你想自己发出文件,可能不需要开发服务器,所以只需 startgin webpack watch就足够了。 If you need dev server and want to emit files, you can set it with this writeToDisk option如果您需要开发服务器并想要发出文件,可以使用此writeToDisk选项进行设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM