javascripthtmlnode.jsgulpgulp-rename

remove body content of index.html using gulp


I can able to rename file name from index. using following configuration

Gulp.js

var rename = require("gulp-rename");

gulp.task('modify-html', function () {
  gulp.src('reports/index.html')
    .pipe(rename('/app.html'))
    .pipe(gulp.dest('reports/'));
});

Does anyone know how to delete the body content of my html file using gulp

index.html

<html>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html>

Solution

  • You can use gulp-html-replace it replaces the HTML block.

    Mark the content which needs to be removed

    <!-- build:remove -->
    Put all the content which needs to be removed, in the block
    <!-- endbuild -->
    

    and use it like

    var htmlreplace = require('gulp-html-replace')
    gulp.task('modify-html', function () {
      gulp.src('reports/index.html')
        .pipe(htmlreplace({ remove : '' }))
        .pipe(rename('/app.html'))
        .pipe(gulp.dest('reports/'));
    });
    

    Then, You can use gulp-dom

    var dom = require('gulp-dom');
    gulp.task('modify-html', function () {
        gulp.src('reports/index.html')
        .pipe(dom(function () {
                this.querySelector('body').innerHTML = '';
                return this;
            }))
        .pipe(rename('/app.html'))
        .pipe(gulp.dest('reports/'));
    });
    

    Note: I have not tested it.