reworkcss / rework-plugin-url

url() plugin for rework, formerly included in core

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pass file path of CSS file as second argument

felixfbecker opened this issue · comments

I'm using this plugin with gulp-rework to resolve relative paths to fonts and include the files in my build directory. The problem is that my function needs to know about the file path of the CSS file it is currently processing in order to resolve the url. Please pass it as a second argument.

The node object is passed into the function as this, so you can get the source file path with this.position.source. Example:

rework.url(function(url) {
  return path.dirname(this.position.source) + url;
})

Awesome, thanks!!
Just in case someone is interested, here is my working solution:

gulp.src('./app/index.html')
    .pipe(htmlSrc({
        selector: 'link[rel="stylesheet"]',
        getFileName: function (element) {
            return element.attr('href');
        }
    }))
    .pipe(rework(reworkUrl(function(url) {

        gulp.src(path.dirname(this.position.source) + '/' + url)
            .pipe(gulp.dest('dist/media'));

        return 'dist/media/' + path.basename(url);
    })))
    //.pipe(cssBase64()) this was my temporary solution
    .pipe(concat('bundle.css'))
    .pipe(minifyCss())
    .pipe(gulp.dest('dist'))

Cool, thanks for sharing what worked for you!