0

我一直在按照指南学习 wepback 及其捆绑功能,并希望找到一种在对代码进行更改时自动更新浏览器的方法,并发现 --watch 是这样做的一种方法。

在 webpack.config.json 文件或 package.json 文件中没有任何监视,该站点已正确编译并显示“正在侦听端口 8080”

当我将 --watch 添加到 package.json 构建脚本并将 watch/watchOptions 添加到 webpack.config.json 文件时,我遇到了它只是挂起并且不打印“正在侦听端口 8080”的问题

这是我的代码:

webpack.config.json

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');

const outputDirectory = 'dist';

console.log(__dirname);
module.exports = {
  entry: {
    'bundle.js': [
      path.resolve(__dirname, 'src/client/index.js'),
    ]
  },
  output: {
    filename: '[name]',
    path: path.resolve(__dirname, 'dist'),
  },
  // entry: ['babel-polyfill', './src/client/index.js'],
  // output: {
  //   path: path.join(__dirname, outputDirectory),
  //   filename: 'bundle.js'
  // },

  module: {
    rules: [{
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader'
        }
      },
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']
      },
      {
        test: /\.(png|woff|woff2|eot|ttf|svg)$/,
        loader: 'url-loader?limit=100000'
      }
    ]
  },
  resolve: {
    extensions: ['*', '.js', '.jsx']
  },

  devServer: {
    port: 3000,
    open: true,
    inline: true,
    proxy: {
      '/api': 'http://localhost:8080'
    }
  },
  watch: true,
  watchOptions: {
    poll: true,
    ignored: /node_modules/
  },
  plugins: [
    new CleanWebpackPlugin([outputDirectory]),
    new HtmlWebpackPlugin({
      template: './public/index.html',
      favicon: './public/favicon.ico'
    })
  ]
};

包.json

{
  "name": "simple-react-full-stack",
  "version": "1.0.0",
  "description": "Boilerplate to build a full stack web application using React, Node.js, Express and Webpack.",
  "main": "src/server/index.js",
  "scripts": {
    "build": "webpack --mode production --watch --colors",
    "start": "npm run build && node src/server/index.js",
    "client": "webpack-dev-server --mode development --devtool inline-source-map --hot",
    "server": "nodemon src/server/index.js",
    "dev": "concurrently \"npm run server\" \"npm run client\""
  },
  "author": "Sandeep Raveesh",
  "license": "ISC",
  "dependencies": {
    "babel-polyfill": "^6.26.0",
    "express": "^4.16.4",
    "react": "^16.8.6",
    "react-dom": "^16.8.6"
  },
  "devDependencies": {
    "@babel/core": "^7.4.3",
    "@babel/plugin-proposal-class-properties": "^7.4.0",
    "@babel/preset-env": "^7.4.3",
    "@babel/preset-react": "^7.0.0",
    "babel-eslint": "^10.0.0",
    "babel-loader": "^8.0.5",
    "clean-webpack-plugin": "^1.0.1",
    "concurrently": "^4.1.0",
    "css-loader": "^2.1.1",
    "eslint": "^5.0.0",
    "eslint-config-airbnb": "^17.0.0",
    "eslint-plugin-import": "^2.11.0",
    "eslint-plugin-jsx-a11y": "^6.0.3",
    "eslint-plugin-react": "^7.7.0",
    "file-loader": "^3.0.0",
    "html-webpack-plugin": "^3.2.0",
    "nodemon": "^1.17.3",
    "style-loader": "^0.23.1",
    "url-loader": "^1.1.2",
    "webpack": "^4.30.0",
    "webpack-cli": "^3.3.1",
    "webpack-dev-server": "^3.1.3"
  }

这是执行 npm start git 命令提示符后命令行向我显示的图像

非常感谢帮助,这是我唯一坚持的事情,之后我在 Node.js 和 React.js 上一帆风顺:)

4

2 回答 2

0
"webpack-cli": "3.3.1",
"webpack-dev-server": "3.1.3"

尝试删除大写符号并提供 npm install

您需要使用 --hot 标志运行 webpack-dev-server:

webpack-dev-server --content-base ./ --port 9966 --hot

然后就可以访问热加载版本localhost:9966/webpack-dev-server/

您也不需要运行手表。

更新:你的 webpack 配置中的这个条目必须改变:

条目:['./js/main.js'],--> 条目:['webpack/hot/dev-server','./js/main.js']

更改您的 publicPath 条目:

publicPath: '/assets/'

于 2019-04-26T05:39:41.980 回答
0

使用 webpack-hot-middleware,链接在这里:https ://blog.cloudboost.io/live-reload-hot-module-replacement-with-webpack-middleware-d0a10a86fc80

https://www.npmjs.com/package/webpack-hot-middleware

于 2019-04-26T07:02:15.230 回答