All Projects → jonkemp → Gulp Useref

jonkemp / Gulp Useref

Licence: mit
Parse build blocks in HTML files to replace references to non-optimized scripts or stylesheets.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Gulp Useref

gulp-micro
Ensure your micro-lib stays micro
Stars: ✭ 15 (-97.92%)
Mutual labels:  gulp-plugin
Gulp Zip
ZIP compress files
Stars: ✭ 262 (-63.61%)
Mutual labels:  gulp-plugin
Gulp Ruby Sass
Compile Sass to CSS with Ruby Sass
Stars: ✭ 476 (-33.89%)
Mutual labels:  gulp-plugin
gulp-golang
gulp plugin for golang projects
Stars: ✭ 13 (-98.19%)
Mutual labels:  gulp-plugin
gulp-yaml
A Gulp plugin to convert YAML to JSON
Stars: ✭ 24 (-96.67%)
Mutual labels:  gulp-plugin
Gulp Template
Render/precompile Lodash templates
Stars: ✭ 276 (-61.67%)
Mutual labels:  gulp-plugin
gulp-tinypng-compress
TinyPNG API wrapper for compressing PNG & JPG images
Stars: ✭ 49 (-93.19%)
Mutual labels:  gulp-plugin
Gulp Gh Pages
A gulp 4 plugin to publish contents to Github pages
Stars: ✭ 611 (-15.14%)
Mutual labels:  gulp-plugin
gulp-markdown-to-json
Parse Markdown and YAML → compile Markdown to HTML → wrap it all up in JSON
Stars: ✭ 76 (-89.44%)
Mutual labels:  gulp-plugin
Gulp Shell
A handy command line interface for gulp
Stars: ✭ 474 (-34.17%)
Mutual labels:  gulp-plugin
anyfs
Portable file system for Node
Stars: ✭ 17 (-97.64%)
Mutual labels:  gulp-plugin
gulp-convert-encoding
Plugin for gulp to convert files from one encoding to another.
Stars: ✭ 15 (-97.92%)
Mutual labels:  gulp-plugin
Gulp Filter
Filter files in a `vinyl` stream
Stars: ✭ 308 (-57.22%)
Mutual labels:  gulp-plugin
gulp-purgecss
Gulp plugin for purgecss
Stars: ✭ 21 (-97.08%)
Mutual labels:  gulp-plugin
Gulp Pug
Gulp plugin for compiling Pug templates
Stars: ✭ 512 (-28.89%)
Mutual labels:  gulp-plugin
gulp-iife
A Gulp plugin for wrapping JavaScript code in IIFEs.
Stars: ✭ 39 (-94.58%)
Mutual labels:  gulp-plugin
Gulp Inline Css
Inline linked css in an html file. Useful for emails.
Stars: ✭ 264 (-63.33%)
Mutual labels:  gulp-plugin
Gulp Autoprefixer
Prefix CSS
Stars: ✭ 676 (-6.11%)
Mutual labels:  gulp-plugin
Gulp Angular Templatecache
Concatenates and registers AngularJS templates in the $templateCache.
Stars: ✭ 530 (-26.39%)
Mutual labels:  gulp-plugin
Gulp Mocha
Run Mocha tests
Stars: ✭ 374 (-48.06%)
Mutual labels:  gulp-plugin

gulp-useref Build Status Coverage Status

NPM

Parse build blocks in HTML files to replace references to non-optimized scripts or stylesheets with useref

Inspired by the grunt plugin grunt-useref. It can handle file concatenation but not minification. Files are then passed down the stream. For minification of assets or other modifications, use gulp-if to conditionally handle specific types of assets.

What's new in 3.0?

Changes under the hood have made the code more efficient and simplified the API. Since the API has changed, please observe the usage examples below.

If you get errors like

TypeError: useref.assets is not a function

or

TypeError: $.useref.assets is not a function

please read the Migration Notes below.

Install

Install with npm

npm install --save-dev gulp-useref

Usage

The following example will parse the build blocks in the HTML, replace them and pass those files through. Assets inside the build blocks will be concatenated and passed through in a stream as well.

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

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

With options:

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

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

If you want to minify your assets or perform some other modification, you can use gulp-if to conditionally handle specific types of assets.

var gulp = require('gulp'),
    useref = require('gulp-useref'),
    gulpif = require('gulp-if'),
    uglify = require('gulp-uglify'),
    minifyCss = require('gulp-clean-css');

gulp.task('html', function () {
    return gulp.src('app/*.html')
        .pipe(useref())
        .pipe(gulpif('*.js', uglify()))
        .pipe(gulpif('*.css', minifyCss()))
        .pipe(gulp.dest('dist'));
});

Blocks are expressed as:

<!-- build:<type>(alternate search path) <path> <parameters> -->
... HTML Markup, list of script / link tags.
<!-- endbuild -->
  • type: either js, css or remove; remove will remove the build block entirely without generating a file
  • alternate search path: (optional) By default the input files are relative to the treated file. Alternate search path allows one to change that. The path can also contain a sequence of paths processed from right to left, using JSON brace array notation e.g <!-- build:js({path1,path2}) js/lib.js -->.
  • path: the file path of the optimized file, the target output
  • parameters: extra parameters that should be added to the tag

An example of this in completed form can be seen below:

<html>
<head>
    <!-- build:css css/combined.css -->
    <link href="css/one.css" rel="stylesheet">
    <link href="css/two.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>
    <!-- endbuild -->
</body>
</html>

The resulting HTML would be:

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

See useref for more information.

API

useref(options [, transformStream1 [, transformStream2 [, ... ]]])

Returns a stream with the asset replaced resulting HTML files as well as the concatenated asset files from the build blocks inside the HTML. Supports all options from useref.

Transform Streams

Type: Stream
Default: none

Transform assets before concat. For example, to integrate source maps:

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

gulp.task('default', function () {
    return gulp.src('index.html')
        .pipe(useref({}, lazypipe().pipe(sourcemaps.init, { loadMaps: true })))
        .pipe(sourcemaps.write('maps'))
        .pipe(gulp.dest('dist'));
});

Options

options.searchPath

Type: String or Array
Default: none

Specify the location to search for asset files, relative to the current working directory. Can be a string or array of strings.

options.base

Type: String
Default: process.cwd()

Specify the output folder relative to the cwd.

options.noAssets

Type: Boolean
Default: false

Skip assets and only process the HTML files.

options.noconcat

Type: Boolean
Default: false

Skip concatenation and add all assets to the stream instead.

options.newLine

Type: String
Default: none

Add a string that should separate the concatenated files.

options.additionalStreams

Type: Array<Stream>
Default: none

Use additional streams as sources of assets. Useful for combining gulp-useref with preprocessing tools. For example, to use with TypeScript:

var ts = require('gulp-typescript');

// create stream of virtual files
var tsStream = gulp.src('src/**/*.ts')
        .pipe(ts());

gulp.task('default', function () {
    // use gulp-useref normally
    return gulp.src('src/index.html')
        .pipe(useref({ additionalStreams: [tsStream] }))
        .pipe(gulp.dest('dist'));
});

options.transformPath

Type: Function
Default: none

Add a transformPath function in case the path needs to be modified before search happens.

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

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

Migration from v2 API

If you upgrade gulp-useref from v2 without changing your gulpfile, you will get errors like this:

TypeError: $.useref.assets is not a function

or

TypeError: useref.assets is not a function

For a simple configuration, you can replace this V2 code:

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'));
});

with this V3 code:

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

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

If you were previously using useref in a multi-stage pipe, you may need to rewrite the pipe, since the simplified V3 API may not allow for its previous usage.

If the gulpfile you are using came from a generator, (for example, in JohnPapa's excellent "opinionated" HotTowel generator), it may be more practical to go back to that generator project to see whether they have upgraded to the V3 gulp-useref API, rather than trying to understand their pipe.

Notes

Contributing

See the CONTRIBUTING Guidelines

License

MIT © Jonathan Kemp

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].