Personal and Professional Blog of Rich Hauck

Integrating gulp-file-include into Yeoman’s generator-gulp-webapp

by: Rich Hauck

Using include files in my HTML templates has become an essential part of my web design workflow, and I’ve been using gulp-file-include in conjunction with generator-gulp-webapp. While gulp-webapp includes a gulpfile.babel.js file, it needs a few modifications to get HTML include files to display both when serving and when building.

I came across a helpful thread here on GitHub, though as I stated in the comments, I couldn’t get includes to display when building. Below is the complete solution. Basically,

  1. I create a fileinclude task (L44)
  2. I modify the html task to include a fileInclude() call (L57)
  3. I include fileinclude in the serve task (L95)
  4. I check for include file changes within the watch task (L114)

Here’s the full file:

// generated on 2016-01-02 using generator-gulp-webapp 1.0.4
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import browserSync from 'browser-sync';
import del from 'del';
import {stream as wiredep} from 'wiredep';

const $ = gulpLoadPlugins();
const reload = browserSync.reload;

gulp.task('styles', () => {
  return gulp.src('app/styles/*.scss')
      outputStyle: 'expanded',
      precision: 10,
      includePaths: ['.']
    }).on('error', $.sass.logError))
    .pipe($.autoprefixer({browsers: ['> 1%', 'last 2 versions', 'Firefox ESR']}))
    .pipe(reload({stream: true}));

function lint(files, options) {
  return () => {
    return gulp.src(files)
      .pipe(reload({stream: true, once: true}))
      .pipe($.if(!, $.eslint.failAfterError()));
const testLintOptions = {
  env: {
    mocha: true

gulp.task('lint', lint('app/scripts/**/*.js'));
gulp.task('lint:test', lint('test/spec/**/*.js', testLintOptions));

gulp.task('fileinclude', () => {
  return gulp.src('app/*.html')
    .pipe($.fileInclude({ prefix: '@@', basepath: '@file' }))
    .pipe(reload({stream: true}));

gulp.task('html', ['styles'], () => {
  return gulp.src('app/*.html')
    .pipe($.useref({searchPath: ['.tmp', 'app', '.']}))
    .pipe($.if('*.js', $.uglify()))
    .pipe($.if('*.css', $.cssnano()))
    //.pipe($.if('*.html', $.htmlmin()))
    .pipe($.if('*.html', $.fileInclude({prefix: '@@', basepath: '@file'})))

gulp.task('images', () => {
  return gulp.src('app/images/**/*')
    .pipe($.if($.if.isFile, $.cache($.imagemin({
      progressive: true,
      interlaced: true,
      // don't remove IDs from SVGs, they are often used
      // as hooks for embedding and styling
      svgoPlugins: [{cleanupIDs: false}]
    .on('error', function (err) {

gulp.task('fonts', () => {
  return gulp.src(require('main-bower-files')('**/*.{eot,svg,ttf,woff,woff2}', function (err) {})

gulp.task('extras', () => {
  return gulp.src([
  ], {
    dot: true

gulp.task('clean', del.bind(null, ['.tmp', 'dist']));

gulp.task('serve', ['fileinclude', 'styles', 'fonts'], () => {
    notify: false,
    port: 9000,
    server: {
      baseDir: ['.tmp', 'app'],
      routes: {
        '/bower_components': 'bower_components'
  ]).on('change', reload);'app/**/*.html', ['fileinclude']);'app/styles/**/*.scss', ['styles']);'app/fonts/**/*', ['fonts']);'bower.json', ['wiredep', 'fonts']);

gulp.task('serve:dist', () => {
    notify: false,
    port: 9000,
    server: {
      baseDir: ['dist']

gulp.task('serve:test', () => {
    notify: false,
    port: 9000,
    ui: false,
    server: {
      baseDir: 'test',
      routes: {
        '/scripts': 'app/scripts',
        '/bower_components': 'bower_components'
  });'test/spec/**/*.js').on('change', reload);'test/spec/**/*.js', ['lint:test']);

// inject bower components
gulp.task('wiredep', () => {
      ignorePath: /^(\.\.\/)+/

      ignorePath: /^(\.\.\/)*\.\./

gulp.task('build', ['lint', 'html', 'images', 'fonts', 'extras'], () => {
  return gulp.src('dist/**/*').pipe($.size({title: 'build', gzip: true}));

gulp.task('default', ['clean'], () => {

Comments: 1

One thought on “Integrating gulp-file-include into Yeoman’s generator-gulp-webapp

  1. just what i was looking for 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

About Rich Hauck

Rich Hauck

I'm a creative technologist at Hauck Interactive, Inc. and an adjunct instructor at HACC. I live in Harrisburg, Pa. with my wife and three boys. I enjoy good coffee, Trappist beers, Orioles baseball, and good design.