您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

webpack-dev-server使用详解(附代码)

2025/7/20 15:58:24发布20次查看
这次给大家带来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-dev-server/index.html.
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();
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vue项目分环境打包步奏详解
使用angular2时避免dom误区
以上就是webpack-dev-server使用详解(附代码)的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product