打包css与images资源,和获取实时资源自动跳转
webpack.config.js文件
const path = require("path")
const HtmlWebpackPlugin = require("html-webpack-plugin")
module.exports={
mode:"none",
entry:"./src/main.js",
output:{
path:path.join(__dirname,"./dist/"),
filename:"bundle.js"
},
plugins:[
new HtmlWebpackPlugin({
template:"./index.html"
})
],
//实时重新加载
devServer:{
contentBase:"./dist"
},
module:{
//模块
rules:[
//规则
{
test:/\.css$/,
use:[
"style-loader",
"css-loader"
]
},
{
test:/\.(png|svg|jpg|gif)$/,
use:[
"file-loader"
]
}
]
}
}
package.json 文件
{
"name": "webpack-demo5",
"version": "1.0.0",
"description": "",
"main": "webpack.config.js",
"scripts": {
"show": "webpack -v",
"start": "node ./src/main.js",
"build": "webpack",
"test": "echo \"Error: no test specified\" && exit 1",
"dev":"webpack-dev-server --open"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^3.4.2",
"file-loader": "^5.1.0",
"html-webpack-plugin": "^3.2.0",
"style-loader": "^1.1.3",
"webpack": "^4.35.2",
"webpack-cli": "^3.3.6",
"webpack-dev-server": "^3.10.3"
}
}
配置之后就可以改变css中样式查看实时更改后的样式了。
npm i --save-dev webpack-dev-server@3.10.3
npm i --save-dev html-webpack-plugin@3.2.0
npm i --save-dev file-loader@5.1.0
npm i --save-dev file-loader@5.1.0
npm install --save-dev style-loader@1.1.3 css-loader@3.4.2
安装低版本会更稳定些,强烈建议安装低版本的插件!兄弟姐妹们,欢迎一起讨论。