gpt4 book ai didi

javascript - gulp.dest 与 glob gulp.src - 使其相对

转载 作者:行者123 更新时间:2023-11-30 11:07:23 24 4
gpt4 key购买 nike

我在 gulp 中有一个简单的任务应该是转译 js:

function js() {
return gulp.src('lib/modules/*/source/js/*.js')
.pipe(babel({
presets: ['@babel/env']
}))
.pipe(gulp.dest((fileVinyl) => {
return fileVinyl.path.replace('/source/', '/public/').replace(/[^\/]*js$/, '')
}))
}

exports.default = gulp.parallel(js)

我的文件结构是这样的:

lib
- modules
- module1
- source
- js
- file.js
- module2
- source
- js
- file.js
- moduleN (...)

现在,我希望它创建这样的文件:

lib
- modules
- module1
- public
- js
- file.js
- source (...)
- module2
- public
- js
- file.js
- source (...)
- moduleN (...)

当然,上面的代码是行不通的,它输出文件到lib/modules/module1/public/js/module1/source/js/这样的文件夹。有什么方法可以强制 gulp 将文件输出到指定路径,而不是将其视为附加自己想法的基本路径?

最佳答案

我在这里遇到的问题是文件的基础错误。但是因为我需要动态更改它,所以我不能只在 src 选项中设置它。幸运的是,我可以在 dest 函数中轻松更改它,其中我有可用的路径:

 .pipe(gulp.dest((fileVinyl) => {
//changing the base so that it will include the whole path to the file,
//excluding the filename
fileVinyl.base = fileVinyl.path.replace(/[^\/]*js$/, '')

//outputing the file to a /public/ folder instead of /source/,
//keeping the structure
return path.relative(process.cwd(), fileVinyl.path.replace('/source/', '/public/').replace(/[^\/]*js$/, ''))
}))

关于javascript - gulp.dest 与 glob gulp.src - 使其相对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55103856/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com