webpack - 在 webpack 5 中保留子目录
问题描述
我有一个问题,我没有找到任何东西(对于 WebPack 5)我在我的文件
夹中创建了一个名为的文件夹,但是当 webpack 将它编译到该文件夹中时,所有图像都被展平到那个图像文件夹中。
有什么东西可以保存子目录吗?(在 webpack 5 中)svg
images
dist
{
test: /\.(png|jpe?g|gif|webp)$/i,
type: "asset/resource",
generator: {
filename: "images/[name][ext]",
},
}
谢谢
解决方案
我找到了解决方案,
该filename
属性可以是一个函数,通过这个属性我们可以获得完整路径,只需进行一些更改就可以保留子目录。
{
test: /\.(png|jpe?g|gif|webp)$/i,
type: "asset/resource",
generator: {
filename: (name) => {
/**
* @description Remove first & last item from ${path} array.
* @example
* Orginal Path: 'src/images/avatar/image.jpg'
* Changed To: 'images/avatar'
*/
const path = name.filename.split("/").slice(1, -1).join("/");
return `${path}/[name][ext]`;
},
},
}
推荐阅读
- ubuntu - 在这个 freeradius 配置中我缺少什么吗?
- c# - IIS:如何添加要由默认网站运行的模块?
- bash - 我可以使用 !="" 来表示“不为空”吗?
- laravel - 如何在多对多关系中使用 laravel sync() 同步 3 个值
- python - DateField 的 TypeError - Django XLWT 库
- php - php websocket中的对象销毁
- google-app-maker - 过滤计算模型
- python - 为什么代码 1 可以正常工作,而代码 2 不能?(Python)
- c - 将一个数组的十六进制值转换为另一个数组的字符串值
- java - 在 JMH @Setup 中初始化 FileInputStream 并在 @Benchmark 中使用