图片处理
使用loader
- file-loader
-
file-loader
[ext]: 扩展名
- [name]: 文件名
- [hash]: 每次打包的hash
- [contentHash]: 根据内容变动的hash
```javascript
const path = require(‘path’)
/**
- [ext]: 扩展名
- [name]: 文件名
- [hash]: 文件内容
- [contentHash]:
- [hash:
] - [path]:
*/
module.exports = {
entry: ‘./src/index.js’,
output: {
filename: ‘main.js’,
path: path.resolve(__dirname, ‘dist’)
},
module: {
rules: [
{
} ] } }test: /\.(png|svg|gif|jpe?g)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'img/[name].[hash:6].[ext]',
// outputPath: 'img'
}
}
]
<a name="yYXjI"></a>
##### 注意:
在css中引入的静态资源,file-loader 在webpack5后到处的文件如果是require导入的如果不去配置esModule: false ,则需.default 导出,这个问题会导致css-loader 加载资源的时候出现问题
```javascript
{
test: /\.(png|svg|gif|jpe?g)$/,
use: [
{
loader: 'file-loader',
options: {
esModule: false // 不转为 esModule
}
}
]
}
所以需要再css-loader中设置esModule: false
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
esModule: false // 就是这里的
}
},
'postcss-loader'
]
},
{
test: /\.less$/,
use: [
'style-loader',
'css-loader',
'postcss-loader',
'less-loader'
]
},
{
test: /\.(png|svg|gif|jpe?g)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'img/[name].[hash:6].[ext]',
}
}
]
}
]
}
}
url-loader
file-loader就是将文件复制过去,url-loader 包含了file-loader,配置了limit之后大于limit的才会用file-loader,小于的会被打包到js内去
const path = require('path')
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
esModule: false
}
},
'postcss-loader'
]
},
{
test: /\.less$/,
use: [
'style-loader',
'css-loader',
'postcss-loader',
'less-loader'
]
},
{
test: /\.(png|svg|gif|jpe?g)$/,
use: [
{
loader: 'url-loader',
options: {
name: 'img/[name].[hash:6].[ext]',
limit: 25 * 1024
}
}
]
}
]
}
}
asset模块
asset模块是webpack5提供的
asset/resource | file-loader |
---|---|
asset/inline | url-loader |
asset | url-loader : limit |
asset 打包地址的话,分两个属性
全局
全局控制
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
assetModuleFilename: "img/[name].[hash:4][ext]"
},
局部
{
test: /\.(png|svg|gif|jpe?g)$/,
type: 'asset/resource',
generator: {
filename: "img/[name].[hash:4][ext]"
}
},
asset 如果想达到url-loader + limit的效果的话,需要配置parser
{
test: /\.(png|svg|gif|jpe?g)$/,
type: 'asset',
generator: {
filename: "img/[name].[hash:4][ext]"
},
parser: {
dataUrlCondition: {
maxSize: 30 * 1024
}
}
}
整体代码
const path = require('path')
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
// assetModuleFilename: "img/[name].[hash:4][ext]"
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
esModule: false
}
},
'postcss-loader'
]
},
{
test: /\.less$/,
use: [
'style-loader',
'css-loader',
'postcss-loader',
'less-loader'
]
},
// {
// test: /\.(png|svg|gif|jpe?g)$/,
// type: 'asset/resource',
// generator: {
// filename: "img/[name].[hash:4][ext]"
// }
// },
// {
// test: /\.(png|svg|gif|jpe?g)$/,
// type: 'asset/inline'
// }
{
test: /\.(png|svg|gif|jpe?g)$/,
type: 'asset',
generator: {
filename: "img/[name].[hash:4][ext]"
},
parser: {
dataUrlCondition: {
maxSize: 30 * 1024
}
}
}
]
}
}
字体处理
font 资源处理就使用asset/resource 直接拷贝
{
test: /\.(ttf|woff2?)$/,
type: 'asset/resource',
generator: {
filename: 'font/[name].[hash:3][ext]'
}
}
整体代码
const path = require('path')
module.exports = {
entry: './src/index.js',
output: {
filename: 'main.js',
path: path.resolve(__dirname, 'dist'),
// assetModuleFilename: "img/[name].[hash:4][ext]"
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{
loader: 'css-loader',
options: {
importLoaders: 1,
esModule: false
}
},
'postcss-loader'
]
},
{
test: /\.less$/,
use: [
'style-loader',
'css-loader',
'postcss-loader',
'less-loader'
]
},
{
test: /\.(png|svg|gif|jpe?g)$/,
type: 'asset',
generator: {
filename: "img/[name].[hash:4][ext]"
},
parser: {
dataUrlCondition: {
maxSize: 30 * 1024
}
}
},
{
test: /\.(ttf|woff2?)$/,
type: 'asset/resource',
generator: {
filename: 'font/[name].[hash:3][ext]'
}
}
]
}
}