0

I'm wondering how to get gulp-rev-replace to replace file references with the same hash across HTML files in subfolders. Considering the following file structure

  • index.html
  • subfolder
    • index.html
  • scripts
    • main.js
  • styles
    • main.scss

And the following html statements:

index.html:

<!-- build:css styles/main.css -->
<link rel="stylesheet" href="/bower_components/normalize-css/normalize.css">
<link rel="stylesheet" href="styles/main.css">
<!-- endbuild -->

subfolder/index.html:

<!-- build:css ../styles/main.css -->
<link rel="stylesheet" href="/bower_components/normalize-css/normalize.css">
<link rel="stylesheet" href="../styles/main.css">
<!-- endbuild -->

And the following in my Gulpfile

gulp.task('html', ['styles'], () => {
  const assets = $.useref.assets({searchPath: ['.tmp', 'app', '.']});

  return gulp.src('app/**/*.html')
    .pipe(assets)
    .pipe($.if('*.js', $.uglify()))
    .pipe($.if('*.css', $.minifyCss({compatibility: '*'})))
    .pipe($.rev())
    .pipe(assets.restore())
    .pipe($.useref())
    .pipe($.revReplace())
    .pipe($.if('*.html', $.minifyHtml({conditionals: true, loose: true})))
    .pipe(gulp.dest('dist'));
});

The style link block in index.html will get replacced with the correct minified and hashed stylesheet reference. The same block in subfolder/index.html will instead get an entirely different hashed stylesheet reference. It does, however, get the path of the stylesheet correct.

Am I setting up my Gulpfile incorrectly?

NoR
  • 975
  • 1
  • 7
  • 8

1 Answers1

0

Figured it out with the help of this separate answer. I ended up setting the style references in each HTML file to the root of the app directory and supplying 3 directories to search:

<!-- build:css({.tmp,app,.}) /styles/main.css -->
<link rel="stylesheet" href="/bower_components/normalize-css/normalize.css">
<link rel="stylesheet" href="/styles/main.css">
<!-- endbuild -->

Which matches the three directories supplied to gulp-useref

const assets = $.useref.assets({searchPath: ['.tmp', 'app', '.']});
Community
  • 1
  • 1
NoR
  • 975
  • 1
  • 7
  • 8