webpack-dev-server是webpack官方提供的一个小型Express服务器。使用它能够为webpack打包生成的资源文件提供web服务。webpack-dev-server官方文档javascript
webpack-dev-server 主要提供两个功能:html
|--src | |--views | |--index.js | |--module_a | |--list.js | |--module_b | |--list.js |--index.html |--webpack.config.base.js |--webpack.config.dev.js
[/src/.../module_a/list.js]前端
export function getName() { console.log("module_a_list"); }
[/src/.../module_a/list.js]java
export function getName() { console.log("module_b_list"); }
[/src/.../index.js]node
import * as a_list from "./module_a/list.js" import * as b_list from "./module_b/list.js" a_list.getName(); b_list.getName();
[/index.html]python
<html> <head> <meta charset="UTF-8"> </head> <body> <h1>RUN WEBPACK_DEMO</h1> <script src="/dist/app.js"></script> </body> </html>
[/webpack.config.base.js]react
var webpack = require("webpack"); var path = require("path"); module.exports = { cache: true, debug: true, entry: { "app": "./src/views/index.js" }, output: { filename: "[name].js", path: __dirname + "/dist", publicPath: "/dist/" }, module: { loaders: [ { test: /.jsx?$/, loader: "babel", query: { presets: ["react", "es2015", "stage-0"], cacheDirectory: true }, exclude: /node_modules/ } ] }, plugins: [] }
[/webpack.config.base.js]webpack
var webpack = require("webpack"); var webpackBase = require("./webpack.config.base.js"); var cfg = Object.assign(webpackBase, { devtool: "cheap-module-eval-source-map" }); module.exports = cfg;
首先须要全局安装webpack-dev-server
git
npm i webpack-dev-server -g
package.json配置scripts:github
"scripts":{ "start":"webpack-dev-server --config webpack.config.dev.js" }
执行npm start
,输出以下:
命令要求webpack-dev-server执行当前目录下webpack.config.dev.js文件,发布地址默认为localhost:8080
浏览器访问http://localhost:8080
,能够看到,项目根目录下的index.html开始运行
F12打开浏览器控制台,输出js文件log
以上,一个简单的devServer配置就完成了
在实际开发中,咱们每每有如下需求:
一、每次修改代码后,webpack能够自动从新打包
二、浏览器能够响应代码变化并自动刷新
webpack-dev-server提供了两种自动刷新模式:iframe和inline
页面被嵌套在一个iframe下,代码发生改动后,iframe会从新加载
使用此模式无需额外配置,只需访问http://localhost:8080/webpack-dev-server/index.html
便可,显然webpack-dev-server默认的模式就是iframe
浏览器访问http://localhost:8080/webpack-dev-server/index.html
修改js代码后保存,命令行log显示module_a/list.js变化致使app.js从新生成:
同时浏览器自动刷新,控制台输出以下:
此方式会将webpack-dev-server客户端加入到webpack入口文件的配置中。
配置方式有两种:CLI配置和经过Node.js Api手动配置
此方式比较简单,只需在webpack.dev.server启动的命令中加入--inline
便可
修改package.json中scripts配置,添加--inline
:
"scripts":{ "start":"webpack-dev-server --inline --config webpack.config.dev.js" }
从新运行npm start
,浏览器访问http://localhost:8080
便可,修改代码后保存,浏览器自动刷新
固然webpack-dev-server相似的命令还有不少,好比,咱们就能够规定项目可访问的地址为http://localhost:9093
:
"scripts":{ "start":"webpack-dev-server --inline --host localhost --port 9093 --config webpack.config.dev.js" }
更多配置参考webpack-dev-server CLI文档
此方式须要手动将webpack-dev-server客户端配置到webpack打包的入口文件中
var webpack = require("webpack"); var webpackBase = require("./webpack.config.base.js"); var cfg = Object.assign(webpackBase, { devtool: "cheap-module-eval-source-map" }); //entry Object.getOwnPropertyNames((webpackBase.entry || {})).map(function (name) { cfg.entry[name] = [] //添加webpack-dev-server客户端 .concat("webpack-dev-server/client?http://localhost:9091") .concat(webpackBase.entry[name]) }); module.exports = cfg;
var path = require("path"); var webpack = require("webpack"); var webpackDevServer = require("webpack-dev-server"); var webpackCfg = require("./webpack.config.dev.js"); var compiler = webpack(webpackCfg); //init server var app = new webpackDevServer(compiler, { //注意此处publicPath必填 publicPath: webpackCfg.output.publicPath }); app.listen(9390, "localhost", function (err) { if (err) { console.log(err); } }); console.log("listen at http://localhost:9390");
"scripts":{ "start":"node devServer.js" }
npm start
,浏览器访问http://localhost:9390
便可,修改代码后保存,浏览器自动刷新当咱们使用webpack-dev-server的自动刷新功能时,浏览器会整页刷新。
而热替换的区别就在于,当前端代码变更时,无需刷新整个页面,只把变化的部分替换掉。
配置的关键在于将webpack/hot/dev-server
文件加入到webpack全部入口文件中。
使用HMR一样一样有两种方式:CLI和Node.js Api
命令行配置比较简单,只需在自动刷新的基础上,加上--hot
配置便可。
此配置会自动将webpack/hot/dev-server添加到webpack全部入口点中。
修改package.json中scripts配置,添加--hot
:
"scripts":{ "start":"webpack-dev-server --inline --hot --config webpack.config.dev.js" }
执行npm start
,浏览器访问http://localhost:8080
便可,当控制台出现以下信息,说明HMR配置成功
此方式须要手动将webpack/hot/dev-server配置到全部webpack入口文件中
webpack/hot/dev-server
,添加插件HotModuleReplacementPlugin
:var webpack = require("webpack"); var webpackBase = require("./webpack.config.base.js"); var cfg = Object.assign(webpackBase, { devtool: "cheap-module-eval-source-map" }); //entry Object.getOwnPropertyNames((webpackBase.entry || {})).map(function (name) { cfg.entry[name] = [] //添加HMR文件 .concat("webpack/hot/dev-server") .concat("webpack-dev-server/client?http://localhost:9390") .concat(webpackBase.entry[name]) }); //plugins cfg.plugins = (webpackBase.plugins || []).concat( new webpack.optimize.OccurrenceOrderPlugin(), //添加HMR插件 new webpack.HotModuleReplacementPlugin(), new webpack.NoErrorsPlugin() ) module.exports = cfg;
hot:true
var path = require("path"); var webpack = require("webpack"); var webpackDevServer = require("webpack-dev-server"); var webpackCfg = require("./webpack.config.dev.js"); var compiler = webpack(webpackCfg); //init server var app = new webpackDevServer(compiler, { //注意此处publicPath必填 publicPath: webpackCfg.output.publicPath, //HMR配置 hot:true }); app.listen(9390, "localhost", function (err) { if (err) { console.log(err); } }); console.log("listen at http://localhost:9390");
"scripts":{ "start":"node devServer.js" }
npm start
,浏览器访问http://localhost:9390
便可,当控制台出现以下信息,说明HMR配置成功可见,使用webpack-dev-server辅助开发,能够极大的方便前端调试。特别是在先后端分离的场景下,使前端能够更加灵活的构建本身的开发环境。