博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
webpack-dev-server 热替换配置详解
阅读量:7080 次
发布时间:2019-06-28

本文共 2494 字,大约阅读时间需要 8 分钟。

webpack-dev-server 配置在官网已经很详细了

还是简单的总结和整理一下!!!
webpack-dev-server 提供两种模式的热替换

  • iframe

  • inline

    这两种的区别官网上有说明:

Iframe modeTo use the iframe mode no additional configuration is needed. Just navigate the browser to http://
:
/webpack-dev-server/
. I. e. with the above configuration http://localhost:8080/webpack-dev-server/index.html. * No configuration change needed. * Nice information bar on top of your app. * Url changes in the app are not reflected in the browsers url bar.Inline modeTo use the inline mode, specify --inline on the command line (you cannot specify it in the configuration). This adds the webpack-dev-server client entry point to the webpack configuration. There is no change in the url required. Just navigate to http://
:
/
. I. e. with the above configuration http://localhost:8080/index.html. * Command line flag needed. * Status information in the browser log. * Url changes in the app are reflected in the browsers url bar.

接下来说以下比较重要的事情,webpack-dev-server 有两种使用模式:

  • commond line (命令行)

  • nodejs

命令行模式比较简单,只需要在启动的时候增加对应的参数即可

  • iframe 热替换模式:

    webpack-dev-server --hot
  • inline 热替换模式:

    webpack-dev-server --hot --inline

特别需要注意不需要以下配置(发现很多人都乱写,不过也能正常启动就是,因为命令行模式启动,webpack-dev-server 会自动给entry添加webpack-dev-server运行时)

  • entry 不需要增加头(webpack-dev-server 自动会添加)

entry: {    "index":[        //'webpack-dev-server/client?http://localhost:8080',        //'webpack/hot/dev-server'    ]}

Node模式下inline模式的热替换配置比较复杂:

/* * new webpackDevServer这种模式,就是node环境下使用了。 */var webpack = require("webpack");var WebpackDevServer = require("webpack-dev-server");var config = require("./webpack.config.js");config.entry.app.unshift("webpack-dev-server/client?http://localhost:8080/");var compiler = webpack(config);var server = new webpackDevServer(compiler, {     hot:true     //...    });server.listen(8080);
  • 需要在entry中加入运行时:

entry: {    "index":[        //'webpack-dev-server/client?http://localhost:8080',        //'webpack/hot/dev-server'    ]}
  • 需要添加HotModuleReplacementPlugin 插件

plugins:[    //new webpack.HotModuleReplacementPlugin()];
  • 另外一个非常重要,需要配置publishPath路径

output: {        path: __dirname+"/dist",        filename: "[name].bundle.js",        publicPath: "http://localhost:8080/dist/"},
  • 还要增加 hot:true 配置

var server = new webpackDevServer(compiler, {     hot:true,     publicPath:config.output.publicPath     //...    });

webpack-dev-server 配置就这样完了,只要静下心来看webpack-dev-server 官网的说明,其实挺简单的。BAY!!!

转载地址:http://usvml.baihongyu.com/

你可能感兴趣的文章
nginx相同端口多站点配置
查看>>
运维人员监控web服务器常用命令
查看>>
注销Apache
查看>>
修改redo_logfile组成员和大小
查看>>
Maven学习总结(九)——使用Nexus搭建Maven私服
查看>>
Uva 1606
查看>>
普加项目管理甘特图使用--安装部署
查看>>
RabbitMQ学习总结(3)——入门实例教程详解
查看>>
chmod 命令详解
查看>>
鸟哥的linux私房菜-首次登陆与在线求助1
查看>>
layui模板引擎和Tab标签一起使用
查看>>
FastDFS集群(一张图)
查看>>
shell编程(三)--- 条件判断之中括号和整数测试
查看>>
唯一索引和非唯一索引的区别简析
查看>>
JavaScript中几个特殊的对象:window对象、this对象、global对象
查看>>
Linux下的日志功能
查看>>
ssh自动连接执行命令
查看>>
小蚂蚁学习mysql性能优化(9)--操作系统配置优化--mysql配置文件优化
查看>>
Debian 8 配置开机启动 CLI、Root用户登陆
查看>>
我的友情链接
查看>>