2016-02-28 78 views
1

结构:Gulp.js:如何重写相对路径?

static 
├── build 
│ ├── css 
│ ├── fonts 
│ ├── img 
│ └── js 
└── src 
    ├── blocks 
    ├── fonts 
    └── img 

件gulpfile.js的:

var path = { 
     build: { 
      js: 'static/build/js', 
      css: 'static/build/css', 
      fonts: 'static/build/fonts', 
      img: 'static/build/img' 

     }, 
     src: { 
      vendor_fonts: ['bower_components/**/*.{svg,woff,eot,ttf}', 'semantic/**/*.{svg,woff,eot,ttf}'], 
      vendor_img: ['bower_components/**/*.{png,jpg,jpeg,gif}', 'semantic/**/*.{png,jpg,jpeg,gif}'] 

     } 
}; 

gulp.task('vendor:img', function(){ 
    return gulp.src(path.src.vendor_img) 
     .pipe(imagemin({ 
      progressive: true, 
      interlaced: true, 
      use: [pngguant()] 
     })) 
     .pipe(gulp.dest(path.build.img)) 
}); 

gulp.task('vendor:fonts', function() { 
    gulp.src(path.src.vendor_fonts) 
     .pipe(gulp.dest(path.build.fonts)) 
}); 

当我建立3方包(如fotorama或语义UI),它们具有相对路径 - 其结果是, main.css只有相对路径,服务器找不到它们。

我该如何解决这个问题?

+1

如果您使用'gulp-sass'来生成.css文件,那么您将有'includePaths:[lookup_paths]'选项来搜索指定路径中的文件。 – cl3m

回答

1

如果您gulpfile.jss是在你的根,你应该能够只是节点的全局对象前缀你的路__dirname

__dirname# 
{String} 
The name of the directory that the currently executing script resides in. 

Example: running node example.js from /Users/mjr 

console.log(__dirname); 
// /Users/mjr 
__dirname isn't actually a global but rather local to each module. 

https://nodejs.org/api/globals.html#globals_dirname

因此,如果您gulpfile是你的根在你的路只是做

__dirname + "/build/whatever/whatever";

这一切是我的理解哟你的问题正确。