我用webpack构建前端多页面,热加载无效

清新锴哥
  • 8

热加载无效
1,目录结构如下

clipboard.png

2,package.json 配置如下

  "devDependencies": {
    "babel": "^6.3.13",
    "babel-core": "^6.3.21",
    "babel-loader": "^6.2.0",
    "babel-plugin-transform-runtime": "^6.3.13",
    "babel-preset-es2015": "^6.3.13",
    "babel-runtime": "^5.8.34",
    "clean-webpack-plugin": "^0.1.10",
    "copy-webpack-plugin": "^3.0.1",
    "css-loader": "^0.23.1",
    "extract-text-webpack-plugin": "^1.0.1",
    "file-loader": "^0.8.5",
    "glob": "^7.0.5",
    "html-loader": "^0.4.3",
    "html-webpack-plugin": "^2.9.0",
    "imports-loader": "^0.6.5",
    "jquery": "^1.12.4",
    "less": "^2.7.1",
    "less-loader": "^2.2.3",
    "style-loader": "^0.13.0",
    "url-loader": "^0.5.7",
    "webpack": "^1.15.0",
    "webpack-dev-server": "^1.14.1"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "webpack-dev-server --hot --inline",
    "build": "webpack -p"
  }

3,webpack.config.js 配置如下

var path = require('path');
var glob = require('glob');
var webpack = require('webpack');
var webPackDevServer = require('webpack-dev-server');
var ExtractTextPlugin = require('extract-text-webpack-plugin');//将你的行内样式提取到单独的css文件里,
var HtmlWebpackPlugin = require('html-webpack-plugin'); //html模板生成器
var CleanPlugin = require('clean-webpack-plugin'); // 文件夹清除工具
var CopyWebpackPlugin = require('copy-webpack-plugin'); // 文件拷贝
var config = {
    entry: { //配置入口文件,有几个写几个
        index: './src/js/index.js',
        list: './src/js/list.js',
        about: './src/js/about.js'
    },
    output: {
        path: path.join(__dirname, 'dist'), //打包后生成的目录
        publicPath: '',    //模板、样式、脚本、图片等资源对应的server上的路径
        filename: 'js/[name].js',    //根据对应入口名称,生成对应js名称
        chunkFilename: 'js/[id].chunk.js'   //chunk生成的配置
    },
    resolve: {
        root: [],
        //设置require或import的时候可以不需要带后缀
        extensions: ['', '.js', '.less', '.css']
    },
    module: {
        loaders: [
            {
                test: /\.css$/,

                loader: ExtractTextPlugin.extract('style', 'css')
            },
            {
                test: /\.js$/,
                loader: 'babel',

                exclude: /node_modules/,
                query: {
                    presets: ['es2015']
                }
            },
            {
                test: /\.(woff|svg|eot|ttf)\??.*$/,
                loader: 'url-loader?name=./fonts/[name].[ext]'
            },
            {
                test: /\.(png|jpg|gif|svg)$/,
                loader: 'url',
                query: {
                    limit: 30720, //30kb 图片转base64。设置图片大小,小于此数则转换。
                    name: './img/[name].[ext]?' //输出目录以及名称
                }
            }
        ]
    },
    plugins: [
        new webpack.ProvidePlugin({ //全局配置加载
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery"
        }),
        new CleanPlugin(['dist']),// 清空dist文件夹
        new webpack.optimize.CommonsChunkPlugin({
            name: 'common', // 将公共模块提取,生成名为`vendors`的chunk
            minChunks: 3 // 提取至少3个模块共有的部分
        }),
        new ExtractTextPlugin("css/[name].css"), //提取CSS行内样式,转化为link引入
        new webpack.optimize.UglifyJsPlugin({ // js压缩
            compress: {
                warnings: false
            }
        }),
        new CopyWebpackPlugin([
            {from: './src/img', to: './img'} //拷贝图片
        ]),
        new webpack.HotModuleReplacementPlugin() // 热加
    ],
    externals: {
        $: 'jQuery'
    },
    //devtool: '#source-map',
    //使用webpack-dev-server服务器,提高开发效率
    devServer: {
        contentBase: './dist',
        hot: true,
        inline: true,
        progress: true,//报错无法识别,删除后也能正常刷新
        hastoryApiFallback: true,
        port: 8080, //端口你可以自定义
        open: true
    }
};

module.exports = config;

var pages = Object.keys(getEntry('./src/*.html'));
var confTitle = [
    {name: 'index', title: '这是首页标题'},
    {name: 'list', title: '这是列表标题'},
    {name: 'about', title: '这是关于我标题'}
]
//生成HTML模板
pages.forEach(function (pathname) {
    var itemName = pathname.split('src\\') //根据系统路径来取文件名,window下的做法//,其它系统另测
    var conf = {
        filename: itemName[1] + '.html', //生成的html存放路径,相对于path
        template: pathname + '.html', //html模板路径
        inject: true, //允许插件修改哪些内容,包括head与body
        hash: false //是否添加hash值
        // minify: { //压缩HTML文件
        //     removeComments: true,//移除HTML中的注释
        //     collapseWhitespace: false //删除空白符与换行符
        // }
    };
    conf.chunks = ['common', itemName[1]]
    for (var i in confTitle) {
        if (confTitle[i].name === itemName[1]) {
            conf.title = confTitle[i].title
        }
    }
    config.plugins.push(new HtmlWebpackPlugin(conf));
});


//按文件名来获取入口文件(即需要生成的模板文件数量)
function getEntry(globPath) {
    var files = glob.sync(globPath);
    var entries = {},
        entry, dirname, basename, pathname, extname;

    for (var i = 0; i < files.length; i++) {
        entry = files[i];
        dirname = path.dirname(entry);
        extname = path.extname(entry);
        basename = path.basename(entry, extname);
        pathname = path.join(dirname, basename);
        entries[pathname] = './' + entry;
    }
    return entries;
}

求各位大神看一下哪里出问题了,热加载总是无效

回复
阅读 4.1k
2 个回答

webpack 3.0以上的版本,热更新中要去掉hot:true选项并且命令启动要取消--hot参数。

你可以试试,虽然你的版本不是3.0以上。

彭彭彭
  • 5
新手上路,请多包涵

解决了我的问题,谢谢

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏