Ad

Gulp Useref Remove Files From Pipeline

- 1 answer

Is there a way to not output the gulp.src file? My goal is to bundle javascript only and output .js only, not html.

In base.html the following blocks are used to bundle Javascript with gulp-useref:

<!-- build:js app.core.js -->
<script src="{{ STATIC_URL }}etherflex/js/vendor/conditionizr_4.5.1.js"></script>
<script src="{{ STATIC_URL }}etherflex/js/app/conditionizr.detects.js"></script>
<script src="{{ STATIC_URL }}etherflex/js/app/conditionizr.config.js"></script>
<script src="{{ STATIC_URL }}etherflex/js/vendor/mootools-core_1.4.5.js"></script>
<!-- endbuild -->

The gulp task

var gulp = require('gulp');
var notify = require('gulp-notify');
var changed = require('gulp-changed');
var plumber = require('gulp-plumber');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var gzip = require('gulp-gzip');
var useref = require('gulp-useref');
var gulpif = require('gulp-if');

module.exports = function (path) {
    return gulp.src('templates/**/*.html')
        .pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
        .pipe(useref({
            searchPath: path.source,
            transformPath: function(filePath) {
                return filePath.replace('{{ STATIC_URL }}/','')
            }
        }))
        .pipe(changed(path.build + 'js'))
        .pipe(gulpif('*.js', uglify()))
        .pipe(rename({
            suffix: ".min",
        }))
        .pipe(gulp.dest(path.build + 'js'))
        .pipe(notify("Javascript concatenated, minified and gzip compressed: <%= file.relative %>"))
        .pipe(gzip())
        .pipe(gulp.dest(path.build + 'js'));
};

The goal is simply to read the block comments in base.html only and output the bundled javascript app.core.js.

Any suggestions?

Ad

Answer

To expand on my comment, you can simply use gulp-if to filter the stream for gulp.dest.

.pipe(gulpif('*.js', gulp.dest(path.build + 'js')))
Ad
source: stackoverflow.com
Ad