1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| const gulp = require('gulp'); const terser = require('gulp-terser'); const htmlmin = require('gulp-htmlmin'); const svgo = require('gulp-svgo'); const gulpSquoosh = require("gulp-squoosh"); const cleanCSS = require('gulp-clean-css'); const minifyInline = require('gulp-minify-inline');
gulp.task('minify-html', () => { return gulp.src('./public/**/*.html') .pipe(htmlmin({ minifyURLs: true, // Minify URLs in various attributes (uses relateurl) useShortDoctype: true, // Replaces the doctype with the short (HTML5) doctype collapseWhitespace: true, // Collapse white space that contributes to text nodes in a document tree conservativeCollapse: true, // Always collapse to 1 space (never remove it entirely). Must be used in conjunction with collapseWhitespace=true collapseInlineTagWhitespace: true, // Don't leave any spaces between display:inline; elements when collapsing. Must be used in conjunction with collapseWhitespace=true collapseBooleanAttributes: true, // Omit attribute values from boolean attributes removeComments: true, // Strip HTML comments removeEmptyAttributes: true, // Remove all attributes with whitespace-only values removeScriptTypeAttributes: true, // Remove type="text/javascript" from script tags. Other type attribute values are left intact removeStyleLinkTypeAttributes: true // Remove type="text/css" from style and link tags. Other type attribute values are left intact })) .pipe(minifyInline()) .pipe(gulp.dest('./public')); });
gulp.task('minify-css', () => { return gulp.src('./public/**/*.css', '!./public/**/*.min.css') .pipe(cleanCSS({debug: true}, details => { console.log(`${details.name}: ${details.stats.originalSize}`); console.log(`${details.name}: ${details.stats.minifiedSize}`); })) .pipe(gulp.dest('./public')); });
gulp.task('minify-js', () => { return gulp.src([ './public/**/*.js', '!./public/**/*.min.js', '!./public/**/*.umd.js' ]) .pipe(terser({ keep_fnames: true, mangle: false })) .pipe(gulp.dest('./public')); });
gulp.task('minify-img', () => { return gulp.src('./public/image/**/*.*', './public/image/**/*.webp') .pipe(svgo()) .pipe(gulpSquoosh()) .pipe(gulp.dest('./public/image')) });
gulp.task( 'default', gulp.series( 'minify-html', 'minify-css', 'minify-js', 'minify-img' ) );
|