【问题标题】:How to access the path mentioned in gulp.src from the gulp.dest function?如何从 gulp.dest 函数访问 gulp.src 中提到的路径?
【发布时间】:2015-12-28 12:57:41
【问题描述】:

我在 gulp.src 函数中使用基于正则表达式的路径,即,

gulp.task('gen-templateCache', function() {
    return gulp.src('app/client/pages/**/views/**/markup/*.html')
        .pipe(templateCache('new-templates.js'))
        .pipe(gulp.dest(function(file){
            //Access the src path
            //Generate and Return the dest path based on the src path
        }));
});

如您所见,我正在使用正则表达式 'app/client/pages/**/views/**/markup/*.html' 来生成此 gulp 任务的 src 路径,因为我的源文件位于多个文件夹中。

我的要求是我还需要将我的结果保存在相对于我的 src 的多个文件夹中。

即,假设我的 gulp 任务结果文件是 'template.js'。现在我想把这个结果保存在'app/client/pages/**/views/template.js'

我该怎么做?

【问题讨论】:

  • 检查gulp-rename插件。
  • 由于某些奇怪的原因,更改 filepath.dirname 和 filepath.basename 并没有为我做这项工作。

标签: regex gulp


【解决方案1】:

我仍然无法找到解决此问题的通用解决方案,但我能够成功找到适合我特定 gulp 任务的解决方案:

基本上我在 src 文件上执行的任务是 'gulp-angular-templatecache'(https://www.npmjs.com/package/gulp-angular-templatecache)

gulp.task('gen-templateCache', function() {
    var destination = '';
    return gulp.src('app/client/pages/**/views/**/markup/*.html')
        .pipe(templateCache('partials.js', {
            root: '',
            module: 'pageApp',
            transformUrl: function(url) {
                var ind = url.lastIndexOf('\\');
                var partialName = url.substring(ind+1);
                var viewsIndex = url.indexOf('\\views\\');
                var viewsPath = url.substring(0,viewsIndex+7);
                destination = "./app/client/pages/"+viewsPath;
                return partialName;
            }
        }))
        .pipe(gulp.dest(function() {
            return destination;
        }));
});

此解决方案仅适用于上述任务的情况,因为上述任务在第二个参数中采用了一个名为 transformUrl 的选项,该选项又是一个以文件 url 作为参数的函数。但是,我仍然希望有一个通用的解决方案。

【讨论】:

    猜你喜欢
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    相关资源
    最近更新 更多