gulp-html-replace is a Gulp plugin designed for replacing HTML "build blocks" with specified content during a build process. It functions similarly to `useref` but aims for a more robust and correct implementation. The current stable version is 1.6.2. Releases appear to be somewhat infrequent but consistent, primarily focusing on bug fixes, performance improvements to the parsing engine, and minor enhancements. Key differentiators include its flexible replacement patterns, supporting simple strings, arrays of strings, objects with `src` and `tpl` for advanced formatting, and even Vinyl file streams as input. It also offers extended replacements for injecting the current filename (`%f`) and file extension (`%e`) into custom templates.
npm install gulp-html-replaceVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates a full Gulp build process using `gulp-html-replace`. It includes tasks for minifying and concatenating CSS and JavaScript, then a `processHtml` task that uses `gulp-html-replace` to update HTML build blocks with references to the processed assets. It shows simple string, array, object with template, and stream-based replacements, as well as extended filename (`%f`) replacements.
Thoroughly test HTML processing pipelines after upgrading to v1.5.0 or later, especially for complex or malformed HTML structures. Review the output HTML carefully for any unexpected changes or regressions in block replacement.
Ensure your Gulp setup uses `vinyl@0.5.0` or newer, or upgrade `gulp-html-replace` to v1.5.3 or later. Version 1.5.3 implemented a fix to use `path.dirname` for broader compatibility across `vinyl` versions.
For non-JS/CSS replacements or when you need custom HTML attributes or tag structures, always use the object format: `htmlreplace({ block: { src: 'path/to/file', tpl: '<custom-tag src="%s" type="module"></custom-tag>' } })`.Ensure strict case-sensitive matching between the block name in your HTML comments (e.g., `<!-- build:my-custom-block -->`) and the key in the `tasks` object (e.g., `{ 'my-custom-block': 'replacement' }`). Always double-check block names for typos.Ensure `const htmlreplace = require('gulp-html-replace');` is at the top of your `gulpfile.js` and that Gulp is installed and running correctly. Verify the package name in `require()` matches the installed package.Check the `tasks` object passed to `htmlreplace()` and ensure each block name has a valid corresponding replacement value (string, array of strings, object with `src` and `tpl`, or a Vinyl stream) as per the API documentation.
Verify that the block name in your HTML (e.g., `unmatched-block`) precisely matches a key in your `htmlreplace` tasks object. If `keepUnassigned` is `true`, blocks without a match will intentionally remain in the output.
Ensure that the `tpl` property is always a string. Additionally, verify that the count of `%s` placeholders in the `tpl` string exactly matches the number of items provided in the `src` array (or one `%s` if `src` is a single string).