当前位置: 首页 > 知识库问答 >
问题:

webpack热中间件不与webpack dev中间件一起工作

百里泓
2023-03-14

我正在尝试使用webpack热中间件来热加载我的应用程序的模块。我也在使用webpack开发中间件。

这是我的设置。

包裹json

{
  "name": "dev-front-end",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "webpack --config webpack.config.production.js",
    "clean-dist": "rimraf dist",
    "lint": "esw buildScripts data/*.js src *.js --color",
    "lint:watch": "npm run lint -- --watch",
    "prebuild": "npm run clean-dist",
    "prestart": "babel-node buildScripts/startMessage.js",
    "security-check": "nsp check",
    "serve-dist": "babel-node buildScripts/distServer.js",
    "serve-src": "babel-node buildScripts/srcServer.js",
    "share": "ngrok http 3000",
    "start": "npm run serve-src",
    "test": "babel-node buildScripts/testSetup.js",
    "test:watch": "npm run test -- --watch"
  },
  "dependencies": {
    "angular": "1.5.11",
    "angular-animate": "<1.6.0",
    "angular-aria": "<1.6.0",
    "angular-audio": "^1.7.3",
    "angular-cookie": "^4.1.0",
    "angular-cookies": "<1.6.0",
    "angular-google-maps": "^2.4.1",
    "angular-ismobile": "^1.0.0",
    "angular-loading-bar": "^0.9.0",
    "angular-material": "^1.1.3",
    "angular-messages": "<1.6.0",
    "angular-scroll": "^1.0.0",
    "angular-socialshare": "^2.3.7",
    "angular-socket-io": "^0.7.0",
    "angular-timeago": "^0.4.4",
    "angular-ui-router": "^1.0.0-rc.1",
    "angulartics": "^1.4.0",
    "angulartics-google-analytics": "^0.4.0",
    "bootstrap": "v4.0.0-alpha.6",
    "bootstrap-social": "^5.1.1",
    "cropperjs": "^0.8.1",
    "font-awesome": "^4.7.0",
    "lodash": "^4.17.4",
    "ng-file-upload": "^12.2.13",
    "ng-infinite-scroll": "^1.3.0",
    "ng-token-auth": "^0.0.30",
    "ngSmoothScroll": "^2.0.0",
    "ngclipboard": "^1.1.1",
    "restangular": "^1.6.1",
    "socket.io-client": "^2.0.3"
  },
  "author": "Medhavin Goyal",
  "license": "ISC",
  "devDependencies": {
    "babel-cli": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-latest": "^6.24.1",
    "compression": "^1.6.2",
    "connect-history-api-fallback": "^1.3.0",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.4",
    "eslint": "^4.0.0",
    "eslint-config-angular": "^0.5.0",
    "eslint-loader": "^1.8.0",
    "eslint-plugin-angular": "^2.5.0",
    "eslint-watch": "^3.1.2",
    "express": "^4.15.3",
    "extract-text-webpack-plugin": "^2.1.2",
    "file-loader": "^0.11.2",
    "html-webpack-plugin": "^2.28.0",
    "jest": "^20.0.4",
    "ng-annotate-loader": "^0.6.1",
    "ngrok": "^2.2.11",
    "npm-run-all": "^4.0.2",
    "raw-loader": "^0.5.1",
    "rimraf": "^2.6.1",
    "strip-loader": "^0.1.2",
    "style-loader": "^0.18.2",
    "url-loader": "^0.5.9",
    "webpack": "^2.6.1",
    "webpack-dev-middleware": "^1.10.2",
    "webpack-hot-middleware": "^2.18.0"
  }
}

网页包。配置。js

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

var VENDOR_LIBS = [
    'webpack-hot-middleware/client?timeout=20000&reload=true',
    'angular',
    'angular-animate',
    'angular-aria',
    'angular-audio',
    'angular-cookie',
    'angular-cookies',
    'angular-google-maps',
    'angular-ismobile',
    'angular-loading-bar',
    'angular-material',
    'angular-messages',
    'angular-scroll',
    'angular-socialshare',
    'angular-socket-io',
    'angular-timeago',
    'angular-ui-router',
    'angulartics',
    'angulartics-google-analytics',
    'bootstrap',
    'cropperjs',
    'lodash',
    'ng-file-upload',
    'ng-infinite-scroll',
    'ng-token-auth',
    'ngSmoothScroll',
    'ngclipboard',
    'restangular',
    'socket.io-client'
];

const config =  {
    entry: {
        bundle: ['webpack-hot-middleware/client?timeout=20000&reload=true', './src/app.module.js'],
        vendor: VENDOR_LIBS
    },
    output: {
        path: path.resolve(__dirname, 'build'),
        filename: 'js/[name].[hash].js',
        publicPath: '/'
    },
    module: {
        rules: [
            {
                use: 'babel-loader',
                test: /\.js$/,
                exclude: /node_modules/
            },
            {
                use: 'url-loader',
                test: /\.woff2?(\?v=[0-9]\.[0-9]\.[0-9])?$/
            },
            {
                use: 'file-loader?name=fonts/[name].[hash].[ext]',
                test: /\.(ttf|eot|svg)(\?[\s\S]+)?$/
            },
            {
                use: ExtractTextPlugin.extract({
                    use: 'css-loader'
                }),
                test: /\.css$/
            },
            {
                use: 'raw-loader',
                test: /\.html$/,
                exclude: /node_modules/
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin('css/style.[hash].css'),
        new webpack.optimize.CommonsChunkPlugin({
            name: ['vendor','manifest']
        }),
        new HtmlWebpackPlugin({
            template: 'src/index.html'
        }),
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery',
            'window.jQuery': 'jquery',
            Tether: 'tether',
            Popper: ['popper.js', 'default'],
            _: 'lodash',
            io: 'socket.io-client'
        }),
        new CopyWebpackPlugin([{
            from: 'assets',
            to: 'assets'
        }]),
        new webpack.HotModuleReplacementPlugin(),
        // new webpack.NoEmitOnErrorsPlugin()
    ],
    devtool: 'inline-source-map',
    target: 'web'
};

module.exports = config;

srcServer。js

import express from 'express';
import path from 'path';
import webpack from 'webpack';
import webpackMiddleware from 'webpack-dev-middleware';
import webpackHotMiddleware from 'webpack-hot-middleware';
import historyApiFallback from 'connect-history-api-fallback';
import config from '../webpack.config';

const port = 3000;
const app = express();

app.use(webpackMiddleware(webpack(config), {
    noInfo: false,
    publicPath: config.output.publicPath
}));
app.use(webpackHotMiddleware(webpack(config), {
   'log': console.log,
   'heartbeat': 10 * 1000
}));
app.use(historyApiFallback({
    disableDotRule: true
}));
app.use(webpackMiddleware(webpack(config), {
    noInfo: false,
    publicPath: config.output.publicPath
}));

app.listen(port, function(err) {
    if (err) {
        console.log(err);
    } else {
        console.log("App listening on port " + port);
    }
});

安装程序不工作。当我更新代码时,编译工作正常,当我刷新浏览器时,新的应用程序代码被执行。但是,这些模块没有被更换。

浏览器控制台中的输出

网络流量

请帮我解决这个问题。谢谢

共有1个答案

狄承望
2023-03-14

如果您按照fine手册中的说明操作,它应该会起作用,在您的情况下,这将转化为以下内容:

let compiler = webpack(config);

app.use(webpackMiddleware(compiler, {
  noInfo     : false,
  publicPath : config.output.publicPath
}));

app.use(webpackHotMiddleware(compiler, {
  'log'       : console.log,
  'path'      : '/__webpack_hmr',
  'heartbeat' : 10 * 1000
}));

我不确定您传递给webpackHotMiddleware的各种选项(log已经默认为console.log,因此您可以省略它,您可能应该使用path的默认值,而不是自己设置)。

 类似资料:
  • 我目前试图取代我的旧的设置,使用webpack-开发服务器与一个更健壮的解决方案,基于快速webpack-中间件。所以我过去是这样运行它的:"webpack-dev-server-内容-基础公共/-历史-api-回退",但现在我想这样使用它:"节点devServer.js"。以下是我当前设置的详细信息。 网页包。配置。dev.js: devServer。js: 你能告诉我哪里出了问题吗?我的印象是

  • 在faygo中,操作与中间件使用同一个接口实现,因此实际上每次请求调用的是一条有序、步阶可控的操作链。 操作链在每次请求中匹配到路由后被调用执行。 操作与中间件的接口定义: Handler interface { Serve(ctx *Context) error } 通常我们使用function或struct来实现操作或中间件。 从业务层面区分操作与中间件: 每个URL对应的操作链[]Han

  • 我是JS的初学者。我正在尝试使用 jwt 令牌构建身份验证。 我在系统中有3个用户(角色:管理员,医生 我有一个用于身份验证的中间件,它是auth.js 我试图写另一个将限制路由的(路由/api/admin/me)访问只有管理员。同样是在adminguard.js.(我试图提取角色和检查条件) 请求有关第二个中间件功能的帮助或提出更好的解决方案。 routes/api/systemusers。js

  • Laravel auth中间件给我带来了两天的麻烦。 我已经了解了身份验证系统如何使用JWTtoken。在我的控制器中,一切正常,我获得了用户登录,可以使用Auth::guard进行访问- 但我有一个问题在我的中间件Authenticate.php: 我尝试了我在多个帖子或教程中找到的所有内容,但结果是相同的(在控制器中同时所有工作用户都登录): 授权控制器。php Kernel.php 我尝试在

  • rightReduce与中间件compose 然后,有心同学可能会发现,我们的makeTravel其实是一个对函数列表进行rightReduce的函数: <?php function compose(...$fns) { return array_right_reduce($fns, function($carry, $fn) { return function() us

  • JWT 中间件 JSON Web Token(JWT) 是一个非常轻巧的规范。这个规范允许我们使用JWT在用户和服务器之间传递安全可靠的信息。 对于有效的令牌,它将用户存储进上下文,并调用下一个处理程序。 对于无效的令牌,它发送”401 - Unauthorized”的响应。 对于空的或无效的Authorization头,它发送”400 - Bad Request”。 使用 e.Use(middl