javascript - 如何在 gulp 中动态设置目标路径?
问题描述
我有一个简单的 gulp 任务,将文件夹树从node-modules
特定目的地移动,保持文件夹结构如下:
function libraries(){
let libPaths = [
'./node_modules/jquery/dist/**/*.*', // Note: copying the ``dist`` folder
'./node_modules/bootstrap-icons/**/*.*' // Note: copying the module
]
return gulp.src(libPaths, {base: './node_modules/'})
.pipe(gulp.dest(
(vinyl) => {
// Something here?
return '/destination/'
}
)
);
}
结果:
destination
├── bootstrap-icons
│ ├── LICENSE.md
│ ├── README.md
│ ├── bootstrap-icons.svg
│ ├── font
│ ├── icons
│ └── package.json
└── jquery
└── dist // <- problem
├── jquery.js
├── jquery.min.js
├── jquery.min.map
├── jquery.slim.js
├── jquery.slim.min.js
└── jquery.slim.min.map
期待:
destination
├── bootstrap-icons
│ ├── LICENSE.md
│ ├── README.md
│ ├── bootstrap-icons.svg
│ ├── font
│ ├── icons
│ └── package.json
└── jquery
├── jquery.js
├── jquery.min.js
├── jquery.min.map
├── jquery.slim.js
├── jquery.slim.min.js
└── jquery.slim.min.map
我将如何检测源文件何时在dist/
例如./node_modules/jquery/dist/jquery.js
将目标输出设置为/destination/jquery/jquery.js
- 而不是在/destination/jquery/dist/jquery.js
.
解决方案
我让它像这样工作:
function libraries(){
let libPaths = [
'./node_modules/jquery/dist/**/*.*', // Note: copying the ``dist`` folder
'./node_modules/bootstrap-icons/**/*.*' // Note: copying the module
]
return gulp.src(libPaths, {base: './node_modules/'})
.pipe(gulp.dest(
(vinyl) => {
vinyl.path = path.join(
vinyl.cwd, vinyl.base,
vinyl.relative.replace('dist\\', ''));
return '/destination/'
}
)
);
}
推荐阅读
- python - Python函数不计算三个字母序列的所有连续实例
- java - 如何在java GUI swing中制作更多按钮
- python - 如何在python中监听变量变化
- android - 颤动的firebase推送通知图标
- javascript - husk 在我提交新文件时不执行预提交
- node.js - Cookies 在 localhost 中有效,但在生产环境中无效
- c++ - 模板结构中的静态函数不使用默认模板参数
- sql - 根据日期范围查找至少 2 个连续项目
- ffmpeg - ffmpeg,在连接时使用 filter_complex 仅将淡入淡出应用于视频输入
- python - 在 Python 中的 pandas.Series 的列表中查找子字符串