webpack-dev-server和webpack
指导小伙伴在webstorm+nodejs环境下新建项目时,小伙伴出现了一个很神奇的问题:没有执行webpack-dev-server情况下,即使执行npm init,也不会出现package.json文件。原本的印象中package.json是保存文件基本信息以及依赖包版本的文件。不知道为什么他和webpack-dev-server有关系。就开始研究webpack-dev-server的相关概念等。
webpack-dev-server
webpack-dev-server是一个小型的Node.js Express服务器,它使用webpack-dev-middleware来服务于webpack的包,除此自外,它还有一个通过Sock.js来连接到服务器的微型运行时.
我们来看一下下面的配置文件(webpack.config.js)
var path = require("path");
module.exports = {
entry:{
app:["./app/main.js"]
},
output:{
path:path.resolve(__dirname,"build"),
publicPath:"/assets/",
filename:"bundle.js"
}
}
这里你将你的源文件放在app文件夹下,并通过webpack将其打包到build文件夹下的bundle.js中.
注意:webpack-dev-server是一个独立的NPM包,你可以通过npm install webpack-dev-server来安装它.
基本目录
webpack-dev-server默认会以当前目录为基本目录,除非你制定它.
webpack-dev-server --content-base build/
上述命令是在命令行中执行的,它将build目录作为根目录.有一点需要注意的是:webpack-dev-server生成的包并没有放在你的真实目录中,而是放在了内存中.
我们在基本目录下新建一个index.html文件,然后在浏览器中输入http://localhost:8080访问.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script src="assets/bundle.js"></script>
</body>
</html>
自动刷新
webpack-dev-server支持两种模式来自动刷新页面.
iframe模式(页面放在iframe中,当发生改变时重载)
inline模式(将webpack-dev-sever的客户端入口添加到包(bundle)中)
两种模式都支持热模块替换(Hot Module Replacement).热模块替换的好处是只替换更新的部分,而不是页面重载.
iframe模式
使用这种模式不需要额外的配置,只需要以下面这种URL格式访问即可
http://«host»:«port»/webpack-dev-server/«path»
例如:http://localhost:8080/webpack...
inline模式
inline模式下我们访问的URL不用发生变化,启用这种模式分两种情况:
1 当以命令行启动webpack-dev-server时,需要做两点:
在命令行中添加
--inline命令在
webpack.config.js中添加devServer:{inline:true}
2 当以Node.js API启动webpack-dev-server时,我们也需要做两点:
由于
webpack-dev-server的配置中无inline选项,我们需要添加webpack-dev-server/client?http://«path»:«port»/到webpack配置的entry入口点中.将
<script src="http://localhost:8080/webpack-dev-server.js"></script>添加到html文件中
var config = require("./webpack.config.js");
var webpack = require('webpack');
var WebpackDevServer = require('webpack-dev-server');
config.entry.app.unshift("webpack-dev-server/client?http://localhost:8080/");
var compiler = webpack(config);
var server = new WebpackDevServer(compiler, {
contentBase:'build/',
publicPath: "/assets/"
});
server.listen(8080);
在Node中运行上面的代码即可。
注意:webpack配置中的devSever配置项只对在命令行模式有效。
(Hot Module Replacement)热模块替换
在命令行中运行inline模式,并启用热模块替换
这里只需要多增加 --hot指令就OK了.如下所示.
webpack-dev-server --content-base build --inline --hot
注意:命令行模式下,webpack.config.js中一定要配置output.publicPath来指定编译后的包(bundle)的访问位置.
在Nodejs API中运行inline模式,并启用热模块替换
这里需要做以下三点:
在
webpack.config.js的entry选项中添加:webpack/hot/dev-server在
webpack.config.js的plugins选项中添加:new webpack.HotModuleReplacementPlugin()在
webpack-dev-server的配置中添加:hot:true
webpack-dev-server中的配置选项
var WebpackDevServer = require("webpack-dev-server");
var webpack = require("webpack");
var compiler = webpack({
// configuration
});
var server = new WebpackDevServer(compiler, {
// webpack-dev-server options
contentBase: "/path/to/directory",
// Can also be an array, or: contentBase: "http://localhost/",
hot: true,
// Enable special support for Hot Module Replacement
// Page is no longer updated, but a "webpackHotUpdate" message is send to the content
// Use "webpack/hot/dev-server" as additional module in your entry point
// Note: this does _not_ add the `HotModuleReplacementPlugin` like the CLI option does.
// Set this as true if you want to access dev server from arbitrary url.
// This is handy if you are using a html5 router.
historyApiFallback: false,
// Set this if you want to enable gzip compression for assets
compress: true,
// Set this if you want webpack-dev-server to delegate a single path to an arbitrary server.
// Use "**" to proxy all paths to the specified server.
// This is useful if you want to get rid of 'http://localhost:8080/' in script[src],
// and has many other use cases (see https://github.com/webpack/webpack-dev-server/pull/127 ).
proxy: {
"**": "http://localhost:9090"
},
setup: function(app) {
// Here you can access the Express app object and add your own custom middleware to it.
// For example, to define custom handlers for some paths:
// app.get('/some/path', function(req, res) {
// res.json({ custom: 'response' });
// });
},
// pass [static options](http://expressjs.com/en/4x/api.html#express.static) to inner express server
staticOptions: {
},
// webpack-dev-middleware options
quiet: false,
noInfo: false,
lazy: true,
filename: "bundle.js",
watchOptions: {
aggregateTimeout: 300,
poll: 1000
},
// It's a required option.
publicPath: "/assets/",
headers: { "X-Custom-Header": "yes" },
stats: { colors: true }
});
server.listen(8080, "localhost", function() {});
// server.close();
webpack和webpack-dev-server的区别
参考:http://webpack.github.io/docs...
webstorm相关nodejs配置参考:http://www.cnblogs.com/xxx91hx/p/7055582.html
--------------------------------------------------------------------------------------------------------------------------------------------
2017-8-24添加:小伙伴出现的webpack-dev-server和package.json的问题是弄混了。package,json文件是需要在npm init之后,一直点击回车输入要填写进package.json的内容,因为小伙伴不了解,没有点击回车时就以为结束了。webpack-dev-server是启用服务器,具体的研究还是参照上方。
webpack-dev-server和webpack的更多相关文章
- 笔记:配置 webpack dev server
笔记:配置 webpack dev server 安装 webpack-dev-server 组件 配置 webpack.config.js 配置 增加 html-webpack-plugin 组件 ...
- [Webpack] Access Webpack Dev Server from Mobile Safari on an iPhone
Testing your sites on mobile devices is a critical part of the development process. Webpack dev serv ...
- 配置Webpack Dev Server 实战操作方法步骤
本文摘要:配置 Webpack Dev Server 可以解决本地开发前端应用时,手动执行 webpack 命令或 yarn build 命令,再去浏览器中访问 dist/index.html 的麻烦 ...
- webpack dev server 和 sublime text 配合时需要注意的地方
参考:https://webpack.js.org/guides/development/ Adjusting Your Text Editor Some text editors have a &q ...
- webpack dev server 配置 启动项目报错Error: listen EADDRINUSE
Error: listen EADDRINUSE 0.0.0.0:5601 它的意思是,端口5601被其他进程占用. 切换端口即可解决问题
- Vue.js如何搭建本地dev server和json-server 模拟请求服务器
前言:vue-cli(版本更新),由原来的2.8.1升级为2.9.1.主要改变是原来在build文件夹下的dev-server.js删掉了,增加了webpack.dev.conf.js. 所以这次讲的 ...
- 解决新版本webpack vue-cli生成文件没有dev.server.js问题
新版本webpack生成的dev.server.js 在webpack.dev.conf.js中 webpack.dev.conf.js const axios = require('axios') ...
- webpack 4 & dev server
webpack 4 & dev server proxy https://webpack.js.org/configuration/dev-server/#devserverproxy htt ...
- vue-cli脚手架之webpack.dev.conf.js
webpack.dev.conf.js 开发环境模式配置文件: 'use strict'//js按照严格模式执行 const utils = require('./utils')//导入utils. ...
- npm ERR! mathine_call@1.0.0 dev: `webpack-dev-server --inline --progress --config build/webpack.dev.conf.js` npm ERR! Exit status 1
internal/modules/cjs/loader.js:583 throw err; ^ Error: Cannot find module 'webpack' at Function.Modu ...
随机推荐
- 【POJ 3190】 Stall Reservations
[题目链接] http://poj.org/problem?id=3190 [算法] 将这些牛按开始吃草的时间排序 维护一个数组S,Si表示畜栏i进去的最后一头牛结束吃草的时间,对于每头牛,找任意一个 ...
- (函数即服务)Faas的现状与未来
刚看到jolestar一位从法律转行程序员的前辈写了一篇Faas现状与未来的文章,里面很多观点都很有启发,或许正如他说的那样,由于Faas能较好的解决资源利用率和开发效率问题,2018年Faas将变得 ...
- 洛谷P2668斗地主(搜索)noip2015
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...
- github fork项目更改后与原作者同步更新
1.进入你的GitHub发起Pull request 2.选择compare across forks 3.反向操作.base fork改为自己的,head fork改为原作者的 4.点击 creat ...
- 题解报告:hdu 2516 取石子游戏(斐波那契博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2516 Problem Description 1堆石子有n个,两人轮流取.先取者第1次可以取任意多个, ...
- Android内存管理(12)*「实例」用Monitor 生成.hprof文件 并分析内存泄漏
参考 http://blog.csdn.net/xiaanming/article/details/42396507 基本步骤: 1,准备一个有内存泄漏的代码 2,如何发现内存泄漏 3,生成.hpro ...
- c/c++ 参数传递 - 数组
对于函数参数中的数组类型:传递的是数组地址,可以理解成传递的是对数组的引用.不是值传递,这是由C/C++函数实现机制决定的.一下三种函数生命完全等价: void func(int array[10]) ...
- cocos2d-x 不规则碰撞检测 【转载】
原文:http://www.2cto.com/kf/201401/272331.html //判断有没有点到有材质的部分, p_point相对, CCSprite坐标 (p_point是相对 Spr ...
- SQL基本操作——row_number() over()
row_number() 与over()是在一起使用的,作用就是对表进行排序并记数. 语法: ROW_NUMBER ( ) OVER ( [ PARTITION BY value_expression ...
- dotnetnuke 添加用户属性 Profile
if (DotNetNuke.Entities.Profile.ProfileController.GetPropertyDefinitionByName(this.PortalId, "Q ...