skip to Main Content

There is a problem with npm library for html minify. I downloaded and declared a library

import htmlmin from 'html-minifier'; 

but when i try to run a gulp task

export const html = () => {
  return gulp.src("source/*.html")
    .pipe(htmlmin({ collapseWhitespace: true }))
    .pipe(gulp.dest("build"));

}
but a got an error

TypeError: htmlmin is not a function

I am pretty sure there is no syntax error but I dont know what is a problem.

I tried different liabraries and more old node.js but I still got same problem

2

Answers


  1. Chosen as BEST ANSWER

    it worked after I instaled special library for gulp gulp-htmlmin after a have seen usage for this one

    const gulp = require('gulp');
    const htmlmin = require('gulp-htmlmin');
    
    gulp.task('minify', () => {
      return gulp.src('src/*.html')
        .pipe(htmlmin({ collapseWhitespace: true }))
        .pipe(gulp.dest('dist'));
    });
    

  2. Please take a look at library documentation.

    There is a precise example of how to use the library with gulp:

    const { src, dest, series } = require('gulp');
    const htmlMinify = require('html-minifier');
    
    const options = {
      includeAutoGeneratedTags: true,
      removeAttributeQuotes: true,
      removeComments: true,
      removeRedundantAttributes: true,
      removeScriptTypeAttributes: true,
      removeStyleLinkTypeAttributes: true,
      sortClassName: true,
      useShortDoctype: true,
      collapseWhitespace: true
    };
    
    function html() {
      return src('app/**/*.html')
        .on('data', function(file) {
          const buferFile = Buffer.from(htmlMinify.minify(file.contents.toString(), options))
          return file.contents = buferFile
        })
        .pipe(dest('build'))
    }
    
    exports.html = series(html)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search