1.下載file-loader
cnpm i -D file-loader
2.在webpack.config.js中配置
1 const path = require('path'); 2 const htmlWebpackPlugin = require('html-webpack-plugin'); 3 module.exports={//node.js語法導出一個對象 4 entry:'./src/app.js', 5 output:{ 6 path:path.resolve(__dirname,'dist'),//絕對路徑 7 filename:'main.js', 8 }, 9 plugins:[//生成html 10 new htmlWebpackPlugin({ 11 filename:'index.html', 12 template:'src/index.html' 13 }) 14 ], 15 module:{ 16 rules:[{ 17 test:/\.js$/, 18 use:[{ 19 loader:'babel-loader', 20 options:{ 21 presets:['react'] 22 } 23 },]}, 24 { 25 test:/\.css$/, 26 use:['style-loader','css-loader'] 27 }, 28 { 29 test:/\.jpg$/, 30 use:['file-loader'] 31 }, 32 ] 33 }, 34 devServer:{ 35 open:true,//自動打開服務器首頁 36 port:9000, 37 inline:true, 38 } 39 }
3.app.css中引入圖片做背景圖
.rea{background: url("../img/11.jpg")}
4.重新運行
cnpm run start
5.退出服務器模式,用 cnpm run dev 命令使文件在輸出目錄生成