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

时间:2016-02-28 10:10:40

标签: javascript css gulp

结构:

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或semantic ui)时,它们有相对路径 - 因此, main.css 只有相对路径,服务器无法找到它们

我如何解决这个问题?

1 个答案:

答案 0 :(得分: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";

如果我正确理解你的问题,这就是全部。