有没有办法更改当前 gulp 基本上下文?

Is there a way to change current gulp base context?

有没有办法改变我的 gulpfile.js 中的 gulp 上下文?

现在我正在为所有 gulp.src 添加我想要的位置前缀。但是有没有办法在全球范围内改变这一点?

这是我目前的代码:

var base = '../';
gulp.task('copy', function() {
   gulp.src(base + 'sampleFile.js').pipe(gulp.dest('dist/'));
});

gulp.task('clean', function() {
   del.sync([base + options.dist]);
});

这正是我要找的东西:

gulp.base('../', function() {
    gulp.task('copy', function() {
       gulp.src('sampleFile.js').pipe(gulp.dest('dist/'));
    });

    gulp.task('clean', function() {
       del.sync([options.dist]);
    });
});

CLI 标志 --cwd 将允许您在全局范围内更改它——尽管要执行您想要的操作,您还需要将 gulpfile 在您调用它的目录中的位置提供给它,所以

gulp --gulpfile gulpfile.js --cwd ../

将替换您拥有的路径前缀。

您还可以为 gulp.src 调用创建一个选项对象,其中包含 cwd: base

var opts = {cwd: '..'}

gulp.task('copy', function() {
   gulp.src('sampleFile.js', opts).pipe(gulp.dest('dist/'));
});

将 Gulpfile 移动到一个目录中可能也很有意义,在该目录中它可以更清楚地访问它将读取和写入的内容。

根据@ddprrt 的评论,我找到了解决方案。

想法是使用 process.chdir 导航到不同的位置。更新后的代码如下:

if (options.base) {
   process.chdir(options.base);
}

gulp.task('copy', function() {
   gulp.src('sampleFile.js').pipe(gulp.dest('dist/'));
});

gulp.task('clean', function() {
   del.sync([options.dist]);
});