首頁 > web前端 > js教程 > 主體

webpack-dev-server的使用步奏

php中世界最好的语言
發布: 2018-04-08 14:33:55
原創
1642 人瀏覽過

這次帶給大家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支援兩種模式來自動刷新頁面.

  1. iframe模式(頁面放在iframe中,當改變時重載)

  2. 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時,需要做兩點:

  1. #在命令列中加入--inline指令

  2. 在webpack.config.js中加入devServer:{inline:true}

2 當以Node.js API啟動webpack-dev-server時,我們也需要做兩點:

  1. 由於webpack-dev-server的配置中無inline選項,我們需要加入webpack-dev-server/client?http://«path»:«port»/到webpack配置的entry入口點.

  2. 加入到html檔案中

  3.  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模式,並啟用熱模組替換

這裡需要做以下三點:

  1. 在webpack.config. js的entry選項中新增:webpack/hot/dev-server

  2. 在webpack.config.js的plugins選項中新增:new webpack.HotModuleReplacementPlugin()

  3. #在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();
登入後複製
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!

推薦閱讀:

H5的canvas圖表實作長條圖

Vue專案應該怎麼分環境打包

以上是webpack-dev-server的使用步奏的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!