詳解webpack-dev-server的簡單使用
webpack-dev-server
webpack-dev-server是一個小型的Node.js Express服務(wù)器,它使用webpack-dev-middleware來服務(wù)于webpack的包,除此自外,它還有一個通過Sock.js來連接到服務(wù)器的微型運(yùn)行時.
我們來看一下下面的配置文件(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是一個獨(dú)立的NPM包,你可以通過npm install webpack-dev-server來安裝它.
基本目錄
webpack-dev-server默認(rèn)會以當(dāng)前目錄為基本目錄,除非你制定它.
webpack-dev-server --content-base build/
上述命令是在命令行中執(zhí)行的,它將build目錄作為根目錄.有一點(diǎn)需要注意的是:webpack-dev-server生成的包并沒有放在你的真實(shí)目錄中,而是放在了內(nèi)存中.
我們在基本目錄下新建一個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中,當(dāng)發(fā)生改變時重載)
- 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不用發(fā)生變化,啟用這種模式分兩種情況:
1 當(dāng)以命令行啟動webpack-dev-server時,需要做兩點(diǎn):
- 在命令行中添加--inline命令
- 在webpack.config.js中添加devServer:{inline:true}
2 當(dāng)以Node.js API啟動webpack-dev-server時,我們也需要做兩點(diǎn):
- 由于webpack-dev-server的配置中無inline選項(xiàng),我們需要添加webpack-dev-server/client?http://«path»:«port»/到webpack配置的entry入口點(diǎn)中.
- 將<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中運(yùn)行上面的代碼即可。
注意:webpack配置中的devSever配置項(xiàng)只對在命令行模式有效。
(Hot Module Replacement)熱模塊替換
在命令行中運(yùn)行inline模式,并啟用熱模塊替換
這里只需要多增加 --hot指令就OK了.如下所示.
webpack-dev-server --content-base build --inline --hot
注意:命令行模式下,webpack.config.js中一定要配置output.publicPath來指定編譯后的包(bundle)的訪問位置.
在Nodejs API中運(yùn)行inline模式,并啟用熱模塊替換
這里需要做以下三點(diǎn):
- 在webpack.config.js的entry選項(xiàng)中添加:webpack/hot/dev-server
- 在webpack.config.js的plugins選項(xiàng)中添加:new webpack.HotModuleReplacementPlugin()
- 在webpack-dev-server的配置中添加:hot:true
webpack-dev-server中的配置選項(xiàng)
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();
參考:http://webpack.github.io/docs/webpack-dev-server.html
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- webpack-dev-server原理解析及跨域解決方法
- 'webpack-dev-server'?不是內(nèi)部或外部命令也不是可運(yùn)行的程序?或批處理文件的最新解決方法
- webpack-dev-server核心概念案例詳解
- 詳解Webpack-dev-server的proxy用法
- 使用webpack3.0配置webpack-dev-server教程
- 淺談webpack-dev-server的配置和使用
- 使用webpack-dev-server處理跨域請求的方法
- 詳解webpack-dev-server 設(shè)置反向代理解決跨域問題
- webpack-dev-server自動更新頁面方法
- webpack-dev-server 的 host 配置 0.0.0.0的方法
相關(guān)文章
Javascript基礎(chǔ)學(xué)習(xí)之十個重要問題
本文特別適合正在尋找Javascript開發(fā)工作的初學(xué)者。再搜索了許多Javascript面試問題后,發(fā)現(xiàn)這10個知識點(diǎn)很重要。讓我們一起深入研究一下2021-12-12
javascript實(shí)現(xiàn)跨域的方法匯總
這篇文章主要給大家匯總介紹了javascript實(shí)現(xiàn)跨域的方法的相關(guān)資料,需要的朋友可以參考下2015-06-06
Javascript數(shù)組的排序 sort()方法和reverse()方法
JavaScript提供了sort()方法和reverse()方法,使得我們可以簡單的對數(shù)組進(jìn)行排序操作和逆序操作2012-06-06
JavaScript實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼及校驗(yàn)
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼及校驗(yàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
Javascript的無new構(gòu)建實(shí)例詳解
這篇文章主要介紹了Javascript的無new構(gòu)建實(shí)例詳解的相關(guān)資料,小編感覺介紹的非常詳細(xì),具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
javascript之querySelector和querySelectorAll使用說明
其實(shí)關(guān)于querySelector和querySelectorAll的介紹說明很多,在此主要是做個記錄2011-10-10

