const path = require('path')
module.exports = {
mode: 'development',
entry: './index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'bundle')
},
module: {
rules: [{
test: /\.(jpg|png|gif)$/,
use: {
loader: 'file-loader',
options: {
name: '[name]_[hash].[ext]',
outputPath: 'image/'
}
}
}]
}
}
- 使用url-loader打包图片,可以使用base64格式打包。
base64的优点:减少http请求
base64的缺点:如果图片文件过大,打包后js文件变大,只适用于小图片
module: {
rules: [{
test: /\.(jpg|png|gif)$/,
use: {
loader: 'url-loader',
options: {
name: '[name]_[hash].[ext]',
outputPath: 'image/',
limit: 2048
}
}
}]
}