Questions tagged [gulp-useref]

a gulp plugin that can replace references to non-optimized scripts or stylesheets in HTML files.

gulp-useref is a gulp plugin that can replace references to non-optimized scripts or stylesheets in HTML files. In order to indicate which of the scripts or stylesheets must be optimized a special build block commentary syntax is used:

<!-- build:<type>(alternate search path) <path> -->
List of script or link tags goes here
<!-- endbuild -->

The build block includes the following parameters:

  • type: either js, css or remove (remove deletes the build block entirely without generating an output file)
  • path: the file path of the optimized file, the target output
  • alternate search path (optional): by default the input files are relative to the treated file, this option allows to change that

The example of HTML file using build blocks:

<html>
  <head>
    <!-- build:css css/combined.css -->
    <link href="css/one.css" rel="stylesheet">
    <link href="css/two.css" rel="stylesheet">
    <link href="css/three.css" rel="stylesheet">
    <!-- endbuild -->
  </head>
  <body>
    <!-- build:js scripts/combined.js -->
    <script type="text/javascript" src="scripts/one.js"></script>
    <script type="text/javascript" src="scripts/two.js"></script>
    <script type="text/javascript" src="scripts/three.js"></script>
    <!-- endbuild -->
  </body>
</html>

The resulting HTML file after plugin usage would look like that

<html>
  <head>
    <link rel="stylesheet" href="css/combined.css"/>
  </head>
  <body>
    <script src="scripts/combined.js"></script>
  </body>
</html>

Example of plugin usage:

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

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

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

useref.assets call in the example above returns a stream with the concatenated asset files mentioned in the build blocks of the HTML files. restore brings back the contents of previously filtered out HTML files.

51 questions
35
votes
4 answers

Why does gulp-useref does not seem to work with a comment in the replacement section?

I am trying to build a gulp pipeline – I want to inject some CSS into my index.html (this works fine) and then grab all the other links from source index.html and replace them in the outputted version. I noticed that the useref call is mangling the…
Rodney
  • 5,417
  • 7
  • 54
  • 98
30
votes
2 answers

Gulp throwing error: File not found with singular glob

After renaming my file I am getting the following error when running the gulp build task: Error: Error: File not found with singular glob: F:\Projects\xyz\HTML\app\css\main.css at DestroyableTransform.
Imran Bughio
  • 4,811
  • 2
  • 30
  • 53
4
votes
3 answers

gulp-rev with useref renaming index.html file as well

I'm new to using gulp and am trying to create a gulpfile for my project. My first task is to combine all my script and link tags present in index.html and replace them with only a single tag. To achieve this purpose, I'm making use of gulp-useref…
Yash Kapila
  • 261
  • 7
  • 17
4
votes
2 answers

Yeoman generator-angular : 'task wiredep' is not in gulpfile & $.useref.restore is not a function

I've just started using Yeoman to scaffold a new Angular app. I followed the installation guide at generator-angular but chose to use gulp instead of grunt for the task runner. After installation I received error : task 'wiredep' is not in you…
3
votes
1 answer

How apply ng-annotate and then uglify inside gulp-if?

Here is my minification gulp task: var gulp = require('gulp'), useref = require('gulp-useref'), gulpif = require('gulp-if'), uglify = require('gulp-uglify'), minifyCss = require('gulp-minify-css'), ngmin = require('gulp-ngmin'), ngAnnotate =…
splash27
  • 2,057
  • 6
  • 26
  • 49
3
votes
1 answer

How to use gulp use-ref with sass

In the project I have: and the gulp file: gulp.task('test', function () { return…
Vasiliy Schitov
  • 115
  • 1
  • 1
  • 7
3
votes
2 answers

Bundling and concatenation with gulp-useref Durandal and ASP.NET MVC

I have a Durandal 2 app based on ASP.NET MVC 5 and Web API, with the initial Index.cshtml (on HomeController) being served through the MVC router. From then on it's all regular html views being handled by the Durandal router. Anyway, I'm trying to…
Sergi Papaseit
  • 15,999
  • 16
  • 67
  • 101
2
votes
0 answers

How can I directly inject css into my index.html?

I'm trying to concatenate external css and js files (that are referenced from my index.html file) and then putting them directly into my html file. I know how to concatenate and then put them into one single file. but haven't managed to write them…
Dave D
  • 21
  • 1
2
votes
0 answers

Gulp useref generates empty file

I have a gulp setup with useref to concatenate and minify my css files: var gulpIf = require('gulp-if'); var useref = require('gulp-useref'); var cssnano = require('gulp-cssnano'); gulp.task('useref', function() { return gulp.src('app/*.html') …
sdvnksv
  • 9,350
  • 18
  • 56
  • 108
2
votes
1 answer

How can you return original path in Gulp useref?

I am using gulp useref to replace/concat my files in my index.html after I minify them. I can get useref to work when I pass a path, but I would like to have it keep the file's original path when I don't define a path. I know I could go through…
RooksStrife
  • 1,647
  • 3
  • 22
  • 54
2
votes
3 answers

Using gulp-if with gulp-useref and gulp-uglify

I want to use gulp-useref to concatenate all of my JavaScript files into one. Within my JavaScript files I have a mixture of pre-minified and non-minified files. I would like to only uglify files which are not already minified (for build…
atwright147
  • 3,694
  • 4
  • 31
  • 57
2
votes
0 answers

gulp concat javascript files in order same as in index.html

I would like use gulp-useref for concant js files in same order as in index.html. Solution has this structure: Solution Folder App Folder Services Folder service.js Scripts Folder angular.js index.html