Zoid Zoid - 4 months ago 34
Node.js Question

Gulp task not working?

Hey so I have a gulp task to minify my html files but it is not working.

gulpfile.js contents:

var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
var htmlmin = require('gulp-html-minifier');



gulp.task('autoprefixer', function () {
return gulp.src('assets/css/*.css')
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(gulp.dest('../../dist/css/'));
});

gulp.task('minify-html', function() {
gulp.src('*.html')
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest('dist/'))
});


Output:

C:\Users\Jack\Desktop\Zoid Studios\node_modules\map-stream\index.js:103 throw err ^ Error: Parse Error: Zoid Studios - Web Agency at new HTMLParser (C:\Users\Jack\Desktop\Zoid Studios\node_modules\gulp-html-minifier\node_modules\html-minifier\src\htmlparser.js:236:13)

at minify (C:\Users\Jack\Desktop\Zoid Studios\node_modules\gulp-html-minifier\node_modules\html-minifier\src\htmlminifier.js:861:3)
at Object.exports.minify (C:\Users\Jack\Desktop\Zoid Studios\node_modules\gulp-html-minifier\node_modules\html-minifier\src\htmlminifier.js:1216:10)
at C:\Users\Jack\Desktop\Zoid Studios\node_modules\gulp-html-minifier\index.js:51:40

at wrappedMapper (C:\Users\Jack\Desktop\Zoid Studios\node_modules\map-stream\index.js:84:19)

at Stream.stream.write (C:\Users\Jack\Desktop\Zoid Studios\node_modules\map-stream\index.js:96:21)

at write (C:\Users\Jack\Desktop\Zoid Studios\node_modules\vinyl-fs\node_modules\readable-stream\lib_stream_readable.js:623:24)
at flow (C:\Users\Jack\Desktop\Zoid Studios\node_modules\vinyl-fs\node_modules\readable-stream\lib_stream_readable.js:632:7)
at DestroyableTransform.pipeOnReadable (C:\Users\Jack\Desktop\Zoid Studios\node_modules\vinyl-fs\node_modules\readable-stream\lib_stream_readable.js:664:5)
at emitNone (events.js:67:13) C:\Users\Jack\Desktop\Zoid Studios>

I use the "gulp minify-html" command in the npm cmd prompt.

Any help?

Answer

My project is working with this code. Note that the task is watching my src folder at the root. And the output is written to the dist folder in my root.

I have installed the watch plugin with: npm install --save-dev gulp-watch and html min with: npm i gulp-htmlmin --save-dev

var gulp = require('gulp');
var htmlmin = require('gulp-htmlmin');
var watch = require('gulp-watch');

gulp.task('default', ['watch','minify']);

gulp.task('watch', function() {
    gulp.watch('src/*.html', ['minify']);
});

gulp.task('minify', function() {
  return gulp.src('src/*.html')
    .pipe(htmlmin({collapseWhitespace: true}))
    .pipe(gulp.dest('dist'))
});