webpack-dev-server 热替换配置详解

webpack-dev-server 配置在官网已经很详细了
http://webpack.github.io/docs...
仍是简单的总结和整理一下!!!
webpack-dev-server 提供两种模式的热替换html

  • iframenode

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

Iframe mode
To use the iframe mode no additional configuration is needed. Just navigate the browser to 
http://<host>:<port>/webpack-dev-server/<path>. 
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 mode
To 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://<host>:<port>/<path>.
 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 有两种使用模式:git

  • commond line (命令行)github

  • nodejsweb

命令行模式比较简单,只须要在启动的时候增长对应的参数便可app

  • iframe 热替换模式:webpack-dev-server

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

    webpack-dev-server --hot --inline

特别须要注意不须要如下配置(发现不少人都乱写,不过也能正常启动就是,由于命令行模式启动,webpack-dev-server 会自动给entry添加webpack-dev-server运行时)url

  • 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!!!

相关文章
相关标签/搜索