Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp.js get filename from .src()

I'm trying to use gulp-proceesshtml (https://github.com/julien/gulp-processhtml) to remove some unwanted code in my build version, the problem is that the task requires a filename to be given.

gulp.src('test.html').pipe(processhtml('test.html'));

But I can't figure out how this would work when I'm processing all HTML files in a folder

gulp.src('*.html).pipe(processhtml('filename here'));

like image 465
woutr_be Avatar asked Jan 22 '26 02:01

woutr_be


1 Answers

Personally, it sounds like that's the wrong plugin for what you are trying to accomplish. See below.

However, because it's not clear what you are using it for, you can be able to use node-glob to process each file one-by-one:

var glob = require('glob')
    // you also need event-stream for merging the streams
    es = require('event-stream');

gulp.task('myTask', function() {
    var files = glob.sync('*.html'),
        streams;
    streams = files.map(function(file) {
                // add the *base* option if your files are stored in
                // multiple subdirectories
        return gulp.src(file, {base: 'relative/base/path'})
                // may need require('path').filename(file)
                .pipe(processhtml(file));
    });

    return es.merge.apply(es, streams);
});

This will create a single asynchronous stream out of every file that matches your initial pattern.


For simply removing some text from your files, you can use gulp-replace, like so:

var replace = require('gulp-replace');

gulp.src('*.html')
   // replaces all text between
   // <!-- remove-this --> and <!-- /remove-this -->
   .pipe(replace(/<!--\s*remove-this\s*-->[\s\S]*?<!--\s*\/remove-this\s*-->/g, ''));
like image 133
OverZealous Avatar answered Jan 24 '26 17:01

OverZealous



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!