F
F
faragly2015-09-11 20:10:51
gulp.js
faragly, 2015-09-11 20:10:51

How to use vinyl-sourcemaps-apply (Gulp)?

Hello! There is a function to post-process the css file and the source map (sourcemap), the task is to change the url of all files in css and not spoil the map, for this, as I understand it, you need vinyl-sourcemaps-apply and rework + rework-url . So what am I doing:

gulp.src('./source/css/**/*.css')
            .pipe(sourcemaps.init({loadMaps: true}))
            .pipe(concat('main.css'))
            .pipe(autoprefixer({
                browsers: ['last 2 versions'],
                cascade: false
            }))
            .pipe(antiCache(fileHashes))
            .pipe(sourcemaps.write())
            .pipe(gulp.dest('./css/'));
, where fileHashes is an object with content of the form "path/to/image.png":"a9ecbba8ef". The antiCache function should not mess up anything, just change the urls:
function antiCache(fileHashes) {
    return through.obj(function (file, enc, cb) {
        var css = file.contents.toString();
        css = rework(css)
            .use(reworkUrl(function (url) {
                if (fileHashes.hasOwnProperty(path.basename(url))) {
                    return url + '?' + fileHashes[path.basename(url)];
                }
                return url;
            })).toString();
        file.contents = new Buffer(css);
        this.push(file);
    });
}

The vinyil-sourcemaps-apply plugin page has an example of how to apply a source map to a modified file. At this step, a stupor. Help me please.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question