简体   繁体   English

Webpack 未构建最新更改

[英]Webpack not building the most recent changes

I am working on a fairly simple project from https://medium.com/ethereum-developers/the-ultimate-end-to-end-tutorial-to-create-and-deploy-a-fully-descentralized-dapp-in-ethereum-18f0cf6d7e0e我正在做一个来自https://medium.com/ethereum-developers/the-ultimate-end-to-end-tutorial-to-create-and-deploy-a-fully-descentralized-dapp-in的相当简单的项目-ethereum-18f0cf6d7e0e

Since the tutorial doesn't focus on the frontend part(webpack and babel and other things), I picked up these steps from different places.由于本教程不关注前端部分(webpack 和 babel 等),我从不同的地方挑选了这些步骤。

Now I was trying to build the front using webpack and http-server, but I realized that it is not updating with the changes that I am making to the file.现在我尝试使用 webpack 和 http-server 构建前端,但我意识到它并没有随着我对文件所做的更改而更新。

webpack.config.js webpack.config.js

const path = require('path')
module.exports = {
   entry: path.join(__dirname, 'src/js', 'index.js'), // Our frontend will be inside the src folder
   output: {
      path: path.join(__dirname, 'dist'),
      filename: 'build.js' // The final file will be created in dist/build.js
   },
   module: {
      rules: [{
         test: /\.css$/, // To load the css in react
         use: ['style-loader', 'css-loader'],
         include: /src/
      }, {
         test: /\.jsx?$/, // To load the js and jsx files
         loader: 'babel-loader',
         exclude: /node_modules/,
         query: {
            presets: ['@babel/preset-env', '@babel/preset-react']
         }
      }]
   }
}

package.json package.json

{
  "name": "test-app",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.10.2",
    "@babel/preset-env": "^7.10.2",
    "@babel/preset-react": "^7.10.1",
    "babel-loader": "^8.1.0",
    "css-loader": "^3.5.3",
    "json-loader": "^0.5.7",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "style-loader": "^1.2.1",
    "web3": "^0.20.0",
    "webpack": "^4.43.0",
    "webpack-cli": "^3.3.11"
  },
  "directories": {
    "test": "test"
  },
  "dependencies": {},
  "description": ""
}

I build it using我使用它构建它

 npx webpack --config webpack.config.js  

and then serve it然后上桌

 http-server dist/

How do I fix this?我该如何解决? And is this even the right way to do it?这甚至是正确的方法吗? Thanks.谢谢。

U have already webpack-cli installed in your dependencies so u dont have to add config in command:你已经在你的依赖项中安装了 webpack-cli,所以你不必在命令中添加配置:

First Add Webpack Script in your Package.json:首先在您的 Package.json 中添加 Webpack 脚本:

  "scripts": {
    "watch": "webpack --watch",
  },

When u run npm run watch --watch webpack will continue to watch for changes in any of the resolved files.当你运行npm run watch --watch webpack 将继续观察任何已解析文件的变化。

And for Server I recommend you webpack-dev-server对于服务器,我推荐你webpack-dev-server

npm i webpack-dev-server

can be used to quickly develop an application可用于快速开发应用程序

module.exports = {
  //...
  devServer: {
    contentBase: path.join(__dirname, 'dist'),
    compress: true,
    port: 9000
  }
};

And add it to your npm script并将其添加到您的 npm 脚本中

 "scripts": {
        "watch": "webpack --watch",
        "start": "webpack-dev-server --hot --open",
      }

Now we can run npm start from the command line and we will see our browser automatically loading up our page.现在我们可以run npm start ,我们将看到我们的浏览器自动加载我们的页面。 If you now change any of the source files and save them, the web server will automatically reload after the code has been compiled.如果您现在更改任何源文件并保存它们,web 服务器将在代码编译后自动重新加载。

Advise : you must add html file in dist or plugins for webpack HtmlWebpackPlugin建议:您必须在 dist 中添加 html 文件或 webpack HtmlWebpackPlugin插件

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

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