|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const gulp = require('gulp'); |
| 4 | +const css = require('gulp-clean-css'); |
| 5 | +const sourcemaps = require('gulp-sourcemaps'); |
| 6 | +const connect = require('gulp-connect'); |
| 7 | +const htmlmin = require('gulp-htmlmin'); |
| 8 | +const uglify = require('gulp-uglify-es').default; |
| 9 | + |
| 10 | +const paths = { |
| 11 | + html: 'src/*.html', |
| 12 | + css: 'src/css/**/*.css', |
| 13 | + script: 'src/js/**/*.js', |
| 14 | + sw: 'src/service-worker.js', |
| 15 | + images: 'src/images/**', |
| 16 | + vendor: 'src/js/vendors/*.js', |
| 17 | + manifest: 'src/manifest.json' |
| 18 | +}; |
| 19 | + |
| 20 | +const imagemin = require('gulp-imagemin'); |
| 21 | + |
| 22 | +const imagesGulp = function() { |
| 23 | + return gulp.src(paths.images) |
| 24 | + .pipe(imagemin([ |
| 25 | + imagemin.gifsicle({interlaced: true}), |
| 26 | + imagemin.jpegtran({progressive: true}), |
| 27 | + imagemin.optipng({optimizationLevel: 5}), |
| 28 | + imagemin.svgo({ |
| 29 | + plugins: [ |
| 30 | + {removeViewBox: true}, |
| 31 | + {cleanupIDs: false} |
| 32 | + ] |
| 33 | + }) |
| 34 | + ])) |
| 35 | + .pipe(gulp.dest('dist/images')); |
| 36 | +}; |
| 37 | + |
| 38 | +const gulpCss = function() { |
| 39 | + return gulp.src(paths.css) |
| 40 | + .pipe(sourcemaps.init()) |
| 41 | + .pipe(css()) |
| 42 | + .pipe(sourcemaps.write('.')) |
| 43 | + .pipe(gulp.dest('dist/css')); |
| 44 | +}; |
| 45 | + |
| 46 | +const jsGulp = function() { |
| 47 | + return gulp.src(paths.script, { |
| 48 | + ignore: [paths.sw, paths.vendor] |
| 49 | + }) |
| 50 | + .pipe(sourcemaps.init()) |
| 51 | + .pipe(uglify()) |
| 52 | + .pipe(sourcemaps.write('.')) |
| 53 | + .pipe(gulp.dest('dist/js')); |
| 54 | +}; |
| 55 | + |
| 56 | +const copyManifest = function() { |
| 57 | + return gulp.src(paths.manifest) |
| 58 | + .pipe(gulp.dest('./')) |
| 59 | +}; |
| 60 | + |
| 61 | +const swGulp = function() { |
| 62 | + return gulp.src(paths.sw) |
| 63 | + .pipe(uglify()) |
| 64 | + .pipe(gulp.dest('./')); |
| 65 | +}; |
| 66 | + |
| 67 | +const vendorGulp = function() { |
| 68 | + return gulp.src(paths.vendor) |
| 69 | + .pipe(uglify()) |
| 70 | + .pipe(gulp.dest('./dist/js/vendors')) |
| 71 | +}; |
| 72 | + |
| 73 | +const htmlGulp = function() { |
| 74 | + return gulp.src(paths.html) |
| 75 | + .pipe(htmlmin({ |
| 76 | + collapseWhitespace: true, |
| 77 | + removeComments: true |
| 78 | + })) |
| 79 | + .pipe(gulp.dest('./')); |
| 80 | +}; |
| 81 | + |
| 82 | +const watchJS = function() { |
| 83 | + return gulp.watch(paths.script, gulp.series(jsGulp, reload)); |
| 84 | +}; |
| 85 | + |
| 86 | +const watchCSS = function() { |
| 87 | + return gulp.watch(paths.css, gulp.series(gulpCss, reload)); |
| 88 | +}; |
| 89 | + |
| 90 | +const watchHTML = function() { |
| 91 | + return gulp.watch(paths.html, gulp.series(htmlGulp, reload)); |
| 92 | +}; |
| 93 | + |
| 94 | +const server = function() { |
| 95 | + return connect.server({livereload: true}); |
| 96 | +}; |
| 97 | + |
| 98 | +const reload = function() { |
| 99 | + return gulp.src(paths.html).pipe(connect.reload()); |
| 100 | +}; |
| 101 | + |
| 102 | +const watch = gulp.parallel( |
| 103 | + watchCSS, watchJS, watchHTML |
| 104 | +); |
| 105 | + |
| 106 | + |
| 107 | +exports.build = gulp.parallel(imagesGulp, jsGulp, gulpCss, htmlGulp, swGulp, vendorGulp, copyManifest); |
| 108 | + |
| 109 | +exports.watch = gulp.parallel(server, watch); |
0 commit comments