使用webpack-dev-server热更新失败

发布于 2022-09-04 18:44:28 字数 3438 浏览 9 评论 0

命令"start": "webpack-dev-server --config webpack.dev.config.js"

webpack/server 版本
`"webpack": "^1.13.1",
"webpack-dev-server": "^1.14.1"`

webpack配置文件

let webpack = require('webpack');
let path = require('path');
let OpenBrowserPlugin = require('open-browser-webpack-plugin');
let autoprefixer=require('autoprefixer');
module.exports = {
    devServer: {
        hot: true,
        inline: true,
        progress: true,
        port: 7070,
        host: '0.0.0.0'
    },
    entry: {
        index: [
            './app/index.js'
        ],
        vendor: [
            'react',
            'react-dom',
            'react-router',
        ]
    },
    output: {
        path: './dist',
        publicPath:'/dist/',
        filename:'[name].js',
    },
    module: {
        loaders: [{
            test: /\.css$/,
            loader: 'style!css!postcss'
        }, {
            test: /\.js[x]?$/,
            exclude: /node_modules/,
            loader: 'react-hot!babel'
        }, {
            test: /\.scss$/,
            loader: 'style!css!sass!postcss'
        }, {
            test: /\.less$/,
            loader: 'style!css!less'
        },
      {test: /\.woff2?$/, loader: 'url?limit=10000&minetype=application/font-woff'},
      {test: /\.ttf$/, loader: 'url?limit=10000&minetype=application/octet-stream'},
      {test: /\.eot$/, loader: 'file'},
      {test: /\.svg$/, loader: 'url?limit=10000&minetype=image/svg+xml'},
      {test: /\.(png|jpg|jpeg|gif)$/i, loader: 'url?limit=10000&name=[name].[ext]'},
      {test: /\.json$/, loader: 'json'}]
    },
    resolve: {
        extensions: ['', '.js', '.jsx'],
    },
    postcss: [
        autoprefixer({
            browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8']
        })
    ],
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js'),

        new webpack.DefinePlugin({
            'process.env': {
                'NODE_ENV': '"development"'
            }
        })
    ]
};

index.html

<!DOCTYPE html>
<head>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale =1, user-scalable=no"/>
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="format-detection" content="telephone=no">
    <meta name="format-detection" content="email=no">
    <title>C-IoT</title>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" href="/favicon.ico" type="image/x-icon">
    <script src="http://localhost:7070/webpack-dev-server.js"></script>
    <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
</head>
<body >
<div id="root"></div>
<script type="text/javascript" src="/dist/vendor.js"></script>
<script type="text/javascript" src="/dist/index.js"></script>
</body>
</html>

为什么这样配置还是无法热更新,总是报[WDS] Hot Module Replacement enabled.
请问一下该如何修改才能使程序热更新?
打包的代码放在https://github.com/yourenA/we... 如果可以帮我看一下,谢谢

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2022-09-11 18:44:28

热更新要往entry里插入语句的- -不然没用

var config = require('./webpack.dev.config.js');
var devPort=7070;

for (var i in config.entry) {
    config.entry[i].unshift('webpack-dev-server/client?http://0.0.0.0:' + devPort, 'webpack/hot/dev-server');
}

还有entry要改为数组形式:

   entry: {
        index1: ['./src/js/entry1.js'],
        index2: ['./src/js/entry2.js'],
        index3: ['./src/js/entry3.js']
        ...
    },

你给的文件测试效果:
图片描述

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文