假设我有一个档案/Users/me/app/src/scripts/foo.js.我设置了一个gulp任务,将此文件写入/Users/me/app/dist/scripts/foo.js:
gulp.src('src/scripts/foo.js', base: 'src')
.pipe(...)
.pipe(gulp.dest('dist'))
Run Code Online (Sandbox Code Playgroud)
我正在写一个简单的插件,需要知道scripts/foo.js.我期待file.relative成为这个部分路径,但它提供了foo.js.我不明白的方式来获得scripts/foo.js来自任意组合file.path,file.cwd,file.base,等.
我怎样才能获得我需要的路径?
Lim*_* H. 10
假设您希望路径相对于指定的基数,您可能希望使用类似节点的路径模块来进行提取:
var path = require('path');
// this is how you get the relative path from a vinyl file instance
path.relative(path.join(file.cwd, file.base), file.path);
Run Code Online (Sandbox Code Playgroud)
以下是使用您的示例的示例gulpfile:
var path = require('path'),
gulp = require('gulp'),
through = require('through2');
function parsePath() {
return through.obj(function (file, enc, cb) {
console.log(file.base);
console.log(file.cwd);
console.log(file.path);
console.log(path.relative(path.join(file.cwd, file.base), file.path))
cb();
});
}
gulp.task('default', function () {
gulp.src('src/scripts/foo.js', { base: 'src'})
.pipe(parsePath())
.pipe(gulp.dest('dist'));
});
Run Code Online (Sandbox Code Playgroud)
这是我运行这个gulpfile时的输出:
src
/Volumes/Data/Project/sandbox/gulp-ex
/Volumes/Data/Project/sandbox/gulp-ex/src/scripts/foo.js
scripts/foo.js
Run Code Online (Sandbox Code Playgroud)
这是项目文件夹结构
gulp-ex/
|_ gulpfile.js
|_ src/scripts/foo.js
|_ dist
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8807 次 |
| 最近记录: |