webpack入門第7篇--熱載入
阿新 • • 發佈:2018-11-07
webpack研究一下熱載入技術,修改程式碼時頁面自動重新整理。
在webpack入門第6篇裡,已經說過了,新建幾個檔案,在這就不建了,因為那樣做,感覺無法突出重點,所以只寫改動的部分。
1、修改webpack.config.js
module.exports = {
entry: "./entry.js",
output: {
path: __dirname,
filename: "bundle.js"
},
module: {
loaders: [
{test: /\.css$/ , loader: "style-loader!css-loader"}
]
},
devServer: {
contentBase: './',
port: 9000
}
};
比以前增加了,這是配置服務的埠號,以及監聽的目錄
devServer: {
contentBase: './',
port: 9000
}
2、在命令列輸入,新增伺服器
npm install webpack -D
npm install webpack-dev-server -D
3、修改package.json
改後如下:
{
"name": "demo4",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server --hot",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^0.28.7" ,
"style-loader": "^0.18.2",
"webpack": "^3.6.0",
"webpack-dev-server": "^2.8.2"
}
}
主要修改了
"start": "webpack-dev-server --hot",
4、在命令列裡輸入
npm start
5、在瀏覽器輸入
http://localhost:9000/
檢視頁面執行結果。
6、讓我們來測試一下效果,修改content.js檔案
alert(555)
module.exports = "It works from content.js.";
發現頁面彈窗了,再修改.css檔案看看,儲存
body {
background: gold;
}
發現頁面自動變為了金色。但是並沒有彈窗,說明頁面並不是全部重新整理,而是修改部分,這種效果不錯吧。