Grunt HTML Processor
grunt-processhtml is a Grunt plugin designed for modifying HTML files during the build process, adapting their content based on the target release environment. It operates by interpreting special `<!-- build:type[:target] -->...<!-- /build -->` comments within HTML files. These directives allow for operations such as concatenating and replacing script or stylesheet tags with a single optimized file, inlining JavaScript or CSS directly into the HTML, removing specific blocks of content for certain environments, or modifying HTML attributes like `src` or `href`. The current stable version is `0.4.4`, last published 2 years ago, but the latest commit was 8 years ago, and the project is considered effectively abandoned, having seen no significant updates since its last release. Its primary differentiator is its integration with the Grunt task runner and its comment-driven approach to build-time HTML manipulation, but it lacks modern tooling support and is tied to an older ecosystem. While functional for its intended purpose within older Grunt-based workflows, it is not recommended for new projects.
Common errors
-
Warning: Task "processhtml" not found. Use --force to continue.
cause The 'grunt-processhtml' plugin was not properly loaded in the `Gruntfile.js`, or the task name was misspelled when attempting to run it.fixEnsure that `grunt.loadNpmTasks('grunt-processhtml');` is present in your `Gruntfile.js` and that the task is correctly referenced in your `registerTask` calls or command-line arguments (e.g., `grunt processhtml:dist`). -
Error: Parse error on line X: <!-- build:invalid-type --> Expected build type 'js', 'css', 'remove', 'template', 'include' or attribute like '[href]'. Got 'invalid-type'.
cause An unsupported or misspelled 'type' was used within an HTML build comment. The plugin expects specific types or attribute names.fixReview the `<!-- build: -->` comment in your HTML file. The `type` must be one of `js`, `css`, `remove`, `template`, `include`, or an HTML attribute enclosed in brackets like `[src]` or `[href]`. -
Grunt-processhtml remove not working when specifying a target
cause Misunderstanding the `build:type[:target]` syntax, specifically how targets interact with the `remove` type. The target in the comment refers to the *Grunt task target* that *should* process the block, not to *remove* it for that specific target by default.fixFor a `remove` block to be removed for a specific target (e.g., `dist`), you typically define it as `<!-- build:remove:dist -->`. This means it *will be removed when the `dist` target is active*. If you want it removed for *all but* a specific target, you might need inverse logic or multiple blocks, or configure `customBlockTypes`.
Warnings
- breaking The 'grunt-processhtml' package is no longer actively maintained. Its last code commit was over 8 years ago, and the last npm publish was 2 years ago, largely for dependency updates. There will be no further bug fixes, security patches, or feature enhancements.
- gotcha The plugin relies heavily on a specific HTML comment syntax (<!-- build:type[:target] -->) for all its transformations. Deviations from this precise syntax, or conflicts with other HTML comments, can lead to parsing errors, unexpected output, or blocks being ignored.
- gotcha This plugin is tightly coupled with the Grunt.js task runner ecosystem. While Grunt remains functional, its usage has declined in favor of more modern build tools like Gulp, Webpack, or Vite. Integrating `grunt-processhtml` requires a full Grunt setup, potentially adding unnecessary complexity to projects not already using Grunt.
Install
-
npm install grunt-processhtml -
yarn add grunt-processhtml -
pnpm add grunt-processhtml
Imports
- loadNpmTasks
import { loadNpmTasks } from 'grunt';grunt.loadNpmTasks('grunt-processhtml'); - processhtml task configuration
const processhtml = require('grunt-processhtml');grunt.initConfig({ processhtml: { /* ... task target configuration ... */ } }); - processhtml task execution
processhtml.run();
grunt.registerTask('default', ['processhtml:dist']);
Quickstart
// Gruntfile.js
module.exports = function(grunt) {
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
processhtml: {
options: {
data: {
version: '<%= pkg.version %>',
env: grunt.option('env') || 'development'
}
},
dist: {
files: {
'dist/index.html': ['src/index.html']
}
},
dev: {
options: {
data: {
env: 'development'
}
},
files: {
'dev/index.html': ['src/index.html']
}
}
}
});
grunt.loadNpmTasks('grunt-processhtml');
grunt.registerTask('build', ['processhtml:dist']);
grunt.registerTask('serve', ['processhtml:dev']);
grunt.registerTask('default', ['serve']);
};
// src/index.html
<!DOCTYPE html>
<html>
<head>
<title>My App - <!-- build:template %%= options.env %%= --></title>
<!-- build:css css/app.min.css -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<!-- /build -->
</head>
<body>
<h1>Welcome to Version <!-- build:template %%= options.version %%= --></h1>
<!-- build:js js/app.min.js -->
<script src="js/lib.js"></script>
<script src="js/app.js"></script>
<!-- /build -->
<!-- build:remove:dist -->
<p>This content is for development only.</p>
<!-- /build -->
<!-- build:[data-env]:dev -->
<div data-env="development">Development Environment</div>
<!-- /build -->
</body>
</html>