This commit is contained in:
张成
2025-10-08 15:39:46 +08:00
parent e513185dd8
commit befc359487

View File

@@ -74,84 +74,41 @@ module.exports = {
] ]
}, },
{ {
// 图片文件:根据文件类型分类存放 // 图片文件:全部转为 base64 内联到代码中
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/, test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader', loader: 'url-loader',
options: { options: {
limit: 10000, // 小于 10KB 转为 base64 limit: 999999999, // 所有图片都转为 base64 内联(设置超大限制)
name: (resourcePath, resourceQuery) => {
// 根据文件扩展名分类
const ext = path.extname(resourcePath).toLowerCase()
if (ext === '.svg') {
return 'images/svg/[name].[hash:7][ext]'
} else if (ext === '.png') {
return 'images/png/[name].[hash:7][ext]'
} else if (ext === '.jpg' || ext === '.jpeg') {
return 'images/jpg/[name].[hash:7][ext]'
} else if (ext === '.gif') {
return 'images/gif/[name].[hash:7][ext]'
}
return 'images/[name].[hash:7][ext]'
},
esModule: false esModule: false
} }
}, },
{ {
// 字体文件:根据字体类型分类存放 // 字体文件:全部转为 base64 内联到代码中
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/, test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader', loader: 'url-loader',
options: { options: {
limit: 10000, // 小于 10KB 转为 base64 limit: 999999999, // 所有字体都转为 base64 内联
name: (resourcePath, resourceQuery) => {
// 根据文件扩展名分类
const ext = path.extname(resourcePath).toLowerCase()
if (ext === '.woff' || ext === '.woff2') {
return 'fonts/woff/[name].[hash:7][ext]'
} else if (ext === '.ttf') {
return 'fonts/ttf/[name].[hash:7][ext]'
} else if (ext === '.eot') {
return 'fonts/eot/[name].[hash:7][ext]'
} else if (ext === '.otf') {
return 'fonts/otf/[name].[hash:7][ext]'
}
return 'fonts/[name].[hash:7][ext]'
},
esModule: false esModule: false
} }
}, },
{ {
// 媒体文件:视频和音频 // 媒体文件:全部转为 base64 内联到代码中
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/, test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader', loader: 'url-loader',
options: { options: {
limit: 10000, limit: 999999999, // 所有媒体文件都转为 base64 内联
name: (resourcePath, resourceQuery) => {
const ext = path.extname(resourcePath).toLowerCase()
if (['.mp4', '.webm', '.ogg'].includes(ext)) {
return 'media/video/[name].[hash:7][ext]'
} else if (['.mp3', '.wav', '.flac', '.aac'].includes(ext)) {
return 'media/audio/[name].[hash:7][ext]'
}
return 'media/[name].[hash:7][ext]'
},
esModule: false esModule: false
} }
}, },
{ {
// JSON 文件 // JSON 文件:内联到代码中
test: /\.json$/, test: /\.json$/,
type: 'asset/resource', type: 'asset/inline'
generator: {
filename: 'data/[name].[hash:7][ext]'
}
}, },
{ {
// 其他资源文件 // 其他资源文件:内联到代码中
test: /\.(txt|xml|md)$/, test: /\.(txt|xml|md)$/,
type: 'asset/resource', type: 'asset/inline'
generator: {
filename: 'assets/[name].[hash:7][ext]'
}
} }
] ]
}, },