gulp-useref is a Gulp plugin designed to optimize front-end asset loading by processing special "build blocks" within HTML files. It concatenates multiple script or stylesheet references into a single optimized file, subsequently replacing the original tags with a single, updated reference. Currently at version 5.0.0, the package primarily focuses on concatenation and reference management, explicitly stating that it does not handle minification. For minification and other asset transformations, it is intended to be used in conjunction with other Gulp plugins like `gulp-if`, `gulp-uglify`, and `gulp-clean-css`. Its core differentiator lies in streamlining HTML asset references within a Gulp build pipeline, reducing HTTP requests and simplifying asset delivery. The project has not seen recent updates (last published 5 years ago), suggesting it is in maintenance mode rather than active development.
npm install gulp-userefVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse HTML build blocks, concatenate JavaScript and CSS files, and then minify them using `gulp-if`, `gulp-uglify`, and `gulp-clean-css` before outputting to a `dist` directory.
Rewrite Gulp pipelines to directly pipe HTML streams to `useref()` without intermediate `assets()` calls. Refer to the v3 migration notes for examples.
Always combine `gulp-useref` with other Gulp plugins like `gulp-if`, `gulp-uglify`, and `gulp-clean-css` (or `gulp-terser`, `gulp-postcss`, etc.) to achieve full asset optimization.
Consider evaluating alternative Gulp plugins or modern build tools like Webpack, Rollup, or Vite for projects requiring active development and broader optimization features. If staying with Gulp, ensure thorough testing.
Remove any calls to `.pipe(useref.assets())` and `.pipe(assets.restore())`. The v3+ API simplifies the process by returning both HTML and asset streams directly from `useref()`.
If running a pure ESM Gulp setup, you might need to use dynamic `import()` (which returns a promise and complicates Gulp's synchronous pipeline) or, more practically, ensure your Gulpfile is CommonJS (`gulpfile.js` without `"type": "module"`) or find a different plugin. Alternatively, `import useref from 'gulp-useref'` might work in some ESM contexts, but compatibility can vary.