javascript - 关于Webpack的热加载更新无效的问题
大家讲道理
大家讲道理 2017-04-11 13:02:47
[JavaScript讨论组]

项目结构与Webpack配置

//webpack.config.js

"use strict";

const path = require("path");
const webpack = require("webpack");
const htmlWebpackPlugin = require("html-webpack-plugin");

const srcPath = path.resolve(__dirname, "src/render");
const outPath = path.resolve(__dirname, "dist/render");

//判断是否生产模式
const isProduction = process.env.NODE_ENV === "production";


//awesome-typescript-loader config
const awesomeTSLoaderOptions = {
    loader: "awesome-typescript-loader",
    options: {
        useBabel: true,
        babelOptions: {
            presets: ["es2015"]
        }
    }
};

module.exports = {
    context: srcPath,
    entry: {
        render: "./app.tsx",
        vendor: [
            'react',
            'react-dom',
            // 'react-redux',
            // 'react-router',
            // 'react-router-redux',
            // 'redux'
        ]
    },
    output: {
        filename: "[name].js",
        path: outPath,
        publicPath: "",
    },
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                // loader:"ts-loader",
                use: isProduction
                    ? awesomeTSLoaderOptions
                    : [
                        "react-hot-loader",
                        awesomeTSLoaderOptions
                    ],
                exclude: /node_modules/
            }

        ]
    },
    target: 'web',
    resolve: {
        //引入模块时,会去查找这3个类型文件
        extensions: ['.tsx', '.ts', '.js']
    },
    devServer: {
        contentBase: outPath,
        publicPath:""
    },
    //sourceMap 类型
    devtool: "eval-source-map",
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.optimize.CommonsChunkPlugin({
            name: 'vendor'
        }),
        new htmlWebpackPlugin({
            template: "./index.html",
            filename: "index.html"
        })
    ]
};

//生产模式
if (isProduction) {
    module.exports.devtool = "source-map";
    module.exports.plugins = (module.exports.plugins || []).concat([
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: JSON.stringify('production')
            }
        }),
        //uglifyJs压缩
        new webpack.optimize.UglifyJsPlugin({
            sourceMap: true
        })
    ]);
}

目录结构

|--src
    |--render
        |--index.html
        |--app.tsx
|--dist
    |--render
        |--index.html
        |--render.js
        |--vendor.js
|--node_modules
|--webpack.config.js
|--package.json

问题描述

执行cross-env NODE_ENV=development webpack-dev-server -d --history-api-fallback --hot --inline --progress --colors --port 1235 --open"后,webpack.dev.server打开站点后,修改文件,有在浏览器的Console中看到其获取接收文件变更信息,如
[WDS] App updated. Recompiling...
[WDS] App hot update...
但页面内容并没有进行热加载,请问是我的webpack设置项编写的有问题吗?


备注

cross-env模块为设定process.NODE_ENV为development,只是为了简化在不同平台下的设定操作。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(3)
PHPz

cross-env NODE_ENV=development webpack-dev-server -d --history-api-fallback --hot --inline --progress --colors --port 1235 --open中的--hot去除后可以刷新页面,但未进行热更新加载

PHPz

ide的safe write勾上了?

ringa_lee

提醒一下,webpack-dev-serverhot 参数的时候,要去掉config里面的 HotModuleReplacementPlugin
不然会内存溢出。

另外,你这个问题。看配置,是没问题的。如果不更新页面,建议你在app.jsx 里面加上如下代码:

if(module.hot){
  module.hot.accept()
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号