国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > JS > 正文

詳解webpack-dev-server的簡(jiǎn)單使用

2024-05-06 16:43:30
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

webpack-dev-server

webpack-dev-server是一個(gè)小型的Node.js Express服務(wù)器,它使用webpack-dev-middleware來(lái)服務(wù)于webpack的包,除此自外,它還有一個(gè)通過(guò)Sock.js來(lái)連接到服務(wù)器的微型運(yùn)行時(shí).

我們來(lái)看一下下面的配置文件(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文件夾下,并通過(guò)webpack將其打包到build文件夾下的bundle.js中.

注意:webpack-dev-server是一個(gè)獨(dú)立的NPM包,你可以通過(guò)npm install webpack-dev-server來(lái)安裝它.

基本目錄

webpack-dev-server默認(rèn)會(huì)以當(dāng)前目錄為基本目錄,除非你制定它.

webpack-dev-server --content-base build/

上述命令是在命令行中執(zhí)行的,它將build目錄作為根目錄.有一點(diǎn)需要注意的是:webpack-dev-server生成的包并沒(méi)有放在你的真實(shí)目錄中,而是放在了內(nèi)存中.

我們?cè)诨灸夸浵滦陆ㄒ粋€(gè)index.html文件,然后在瀏覽器中輸入http://localhost:8080訪問(wèn).

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title></head><body> <script src="assets/bundle.js"></script></body></html>

自動(dòng)刷新

webpack-dev-server支持兩種模式來(lái)自動(dòng)刷新頁(yè)面.

  1. iframe模式(頁(yè)面放在iframe中,當(dāng)發(fā)生改變時(shí)重載)
  2. inline模式(將webpack-dev-sever的客戶端入口添加到包(bundle)中)

兩種模式都支持熱模塊替換(Hot Module Replacement).熱模塊替換的好處是只替換更新的部分,而不是頁(yè)面重載.

iframe模式
使用這種模式不需要額外的配置,只需要以下面這種URL格式訪問(wèn)即可

http://«host»:«port»/webpack-dev-server/«path»

例如:http://localhost:8080/webpack-dev-server/index.html.

inline模式

inline模式下我們?cè)L問(wèn)的URL不用發(fā)生變化,啟用這種模式分兩種情況:

1 當(dāng)以命令行啟動(dòng)webpack-dev-server時(shí),需要做兩點(diǎn):

  1. 在命令行中添加--inline命令
  2. 在webpack.config.js中添加devServer:{inline:true}

2 當(dāng)以Node.js API啟動(dòng)webpack-dev-server時(shí),我們也需要做兩點(diǎn):

  1. 由于webpack-dev-server的配置中無(wú)inline選項(xiàng),我們需要添加webpack-dev-server/client?http://«path»:«port»/到webpack配置的entry入口點(diǎn)中.
  2. 將<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)只對(duì)在命令行模式有效。

(Hot Module Replacement)熱模塊替換

在命令行中運(yùn)行inline模式,并啟用熱模塊替換

這里只需要多增加 --hot指令就OK了.如下所示.

webpack-dev-server --content-base build --inline --hot

注意:命令行模式下,webpack.config.js中一定要配置output.publicPath來(lái)指定編譯后的包(bundle)的訪問(wèn)位置.

在Nodejs API中運(yùn)行inline模式,并啟用熱模塊替換

這里需要做以下三點(diǎn):

  1. 在webpack.config.js的entry選項(xiàng)中添加:webpack/hot/dev-server
  2. 在webpack.config.js的plugins選項(xiàng)中添加:new webpack.HotModuleReplacementPlugin()
  3. 在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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宝鸡市| 恩施市| 呼伦贝尔市| 准格尔旗| 汉沽区| 英德市| 浑源县| 石渠县| 蕲春县| 隆安县| 武强县| 舞钢市| 都匀市| 稷山县| 志丹县| 五指山市| 乐都县| 铜梁县| 双江| 奉化市| 武城县| 罗田县| 吉木萨尔县| 新乡县| 手游| 顺昌县| 内丘县| 衡阳市| 通州市| 扬州市| 偃师市| 登封市| 镇坪县| 商都县| 陈巴尔虎旗| 措美县| 徐水县| 葵青区| 吕梁市| 安泽县| 新安县|