Skip to content

gulp-useref.assets is not a function #34

Description

@BrandonSchreck

While following along your pluralsight video, the following gulp task results in an error 'TypeError: $GLP.useref.assets is not a function'.

gulp.task('optimize', ['inject', 'fonts', 'images'], function () {
    log('Optimizing the javascript, css, and html');

    var assets = $GLP.useref.assets({searchPath: './'}),
        templateCache = config.temp + config.templateCache.file,
        cssFilter = $GLP.filter('**/*.css'),
        jsLibFilter = $GLP.filter('**/' + config.optimized.lib),
        jsAppFilter = $GLP.filter('**/' + config.optimized.app);

    return gulp
        .src(config.index)
        .pipe($GLP.plumber())
        .pipe($GLP.inject(gulp.src(templateCache, { read: false }), {
            starttag: '<!-- inject:templates:js -->'
        }))
        .pipe(assets)
            .pipe(cssFilter)
            .pipe($GLP.csso())
            .pipe(cssFilter.restore())

            .pipe(jsLibFilter)
            .pipe($GLP.uglify())
            .pipe(jsLibFilter.restore())

            .pipe(jsAppFilter)
            .pipe($GLP.ngAnnotate())
            .pipe($GLP.uglify())
            .pipe(jsAppFilter.restore())

            .pipe($GLP.rev())
        .pipe(assets.restore)
        .pipe($GLP.useref())
        .pipe($GLP.revReplace())
        .pipe(gulp.dest(config.build))
        .pipe($GLP.rev.manifest())
        .pipe(gulp.dest(config.build));
});

As per gulp-useref documentation, v3 no longer uses:

var gulp = require('gulp'),
    useref = require('gulp-useref');

gulp.task('default', function () {
    var assets = useref.assets();

    return gulp.src('app/*.html')
        .pipe(assets)
        .pipe(assets.restore())
        .pipe(useref())
        .pipe(gulp.dest('dist'));
});

but instead uses the following syntax:

var gulp = require('gulp'),
    useref = require('gulp-useref');

gulp.task('default', function () {
    return gulp.src('app/*.html')
        .pipe(useref())
        .pipe(gulp.dest('dist'));
});

I was able to find a workaround while digging around the inner tubes but it does not append a version to the index.html page itself. Hopefully this will help others.

gulp.task('optimize', ['inject', 'fonts', 'images'], function () {
    log('Optimizing the javascript, css, and html');

    var lazypipe = require('lazypipe'),
        templateCache = config.temp + config.templateCache.file,
        cssFilter = $GLP.filter('**/*.css', {restore: true}),
        jsLibFilter = $GLP.filter('**/' + config.optimized.lib, {restore: true}),
        jsAppFilter = $GLP.filter('**/' + config.optimized.app, {restore: true}),
        notIndexFilter = $GLP.filter(['**/*', '!**/index.html'], {restore: true});

    return gulp
        .src(config.index)
        .pipe($GLP.plumber())
        .pipe($GLP.inject(
            gulp.src(templateCache, { read: false }),
            { starttag: '<!-- inject:templates:js -->' }
        ))
        // Apply the concat and file replacement with useref
        .pipe($GLP.useref({searchPath: './'}, lazypipe().pipe($GLP.sourcemaps.init,{loadMaps: true})))

        // Get the css
        .pipe(cssFilter)
        .pipe($GLP.csso())
        .pipe(cssFilter.restore)

        // Get the custom javascript
        .pipe(jsAppFilter)
        .pipe($GLP.ngAnnotate())
        .pipe($GLP.uglify())
        .pipe(jsAppFilter.restore)

        // Get the vendor javascript
        .pipe(jsLibFilter)
        .pipe($GLP.uglify())
        .pipe(jsLibFilter.restore)

        // Take inventory of the file names for future rev numbers
        .pipe(notIndexFilter)
        .pipe($GLP.rev())
        .pipe(notIndexFilter.restore)

        // Replace the file names in the html with rev numbers
        .pipe($GLP.revReplace())
        .pipe($GLP.sourcemaps.write('.'))
        .pipe(gulp.dest(config.build))
        .pipe($GLP.rev.manifest())
        .pipe(gulp.dest(config.build));
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions