【问题标题】:webpack dev middleware, how to autoreload when HMR failswebpack 开发中间件,如何在 HMR 失败时自动重新加载
【发布时间】:2016-07-21 18:22:01
【问题描述】:

当我更改我的 javascript 源时,我在浏览器的控制台中收到以下消息:

[HMR] 以下模块无法热更新:(完全重新加载 需要)这通常是因为模块已经改变(和 他们的父母)不知道如何热装自己。看 http://webpack.github.io/docs/hot-module-replacement-with-webpack.html 了解更多详情。

我的问题是如何告诉 webpack 在发生这种情况时自动重新加载页面?

这是我的服务器设置:

app.use(morgan('dev'));

        // Disable views cache
        app.set('view cache', false);

        var webpack = require('webpack');

        var webpackConfig = require('../client/webpack.config');
        var compiler = webpack(webpackConfig);

        app.use(require("webpack-dev-middleware")(compiler, {
            noInfo: true, publicPath: webpackConfig.output.publicPath
        }));
        app.use(require("webpack-hot-middleware")(compiler));

还有我的 webpack.config:

var path = require('path');
var AureliaWebpackPlugin = require('../node_modules/aurelia-webpack-plugin');
var webpack = require('../node_modules/webpack');

module.exports = {
    entry: {
        main: [
            'webpack-hot-middleware/client',
            './client/src/main'
        ]
    },
    resolve: {
        alias: {
            breeze: 'breeze-client/build/breeze.debug',
            resources: path.resolve( __dirname, 'src', 'resources'),
            utils: path.resolve( __dirname, 'src', 'resources', 'utils', 'utils'),
            tradestudyUtils: path.resolve( __dirname, 'src', 'resources', 'tradestudy-utils', 'tradestudy-utils')
        }
    },
    output: {
        path: path.join(__dirname, 'client'),
        filename: 'bundle.js',
        publicPath: '/'
    },
    devtool: 'eval',
    plugins: [
        new webpack.optimize.OccurenceOrderPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.NoErrorsPlugin(),
        new AureliaWebpackPlugin()
    ],
    module: {
        //preLoaders: [
        //    {test: /\.js$/, exclude: /node_modules/, loader: 'eslint-loader'}
        //],
        loaders: [
            { test: /\.scss$/, loaders: ['style', 'css', 'sass'] },
            { test: /\.js$/, loader: 'babel', exclude: /node_modules/, query: { presets: ['es2015-loose', 'stage-1'], plugins: ['transform-decorators-legacy'] } },
            { test: /\.css?$/, loader: 'style!css' },
            { test: /\.html$/, loader: 'raw' },
            { test: /\.(png|gif|jpg)$/, loader: 'url-loader?limit=8192' },
            { test: /\.woff2(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url-loader?limit=10000&minetype=application/font-woff2' },
            { test: /\.woff(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'url-loader?limit=10000&minetype=application/font-woff' },
            { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: 'file-loader' }
        ]
    }
};

提前致谢?

【问题讨论】:

    标签: javascript webpack


    【解决方案1】:

    您可以将parameter reload 传递给webpack-hot-middleware/client

        entry: {
            main: [
                'webpack-hot-middleware/client?reload=true',
                './client/src/main'
            ]
        },
    

    【讨论】:

    • 这是刷新整个页面还是确实是“热模块更换”?
    • 我的设置略有不同,但我在 aurelia-bootstrapper 之前添加了 'webpack-hot-middleware/client?reload=true',这是热模块替换
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 2017-05-03
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 2018-06-30
    相关资源
    最近更新 更多