|
| 1 | +// Get things set up |
| 2 | +// ------------------------------------------------------------------- |
| 3 | + // Include Gulp |
| 4 | +var gulp = require("gulp"), |
| 5 | + |
| 6 | + // HTML plugins |
| 7 | + fileinclude = require("gulp-file-include"), |
| 8 | + htmlmin = require("gulp-htmlmin"), |
| 9 | + |
| 10 | + // CSS plugins |
| 11 | + sass = require("gulp-sass"), |
| 12 | + autoprefixer = require("gulp-autoprefixer"), |
| 13 | + cssmin = require("gulp-clean-css"), |
| 14 | + rename = require("gulp-rename"), |
| 15 | + |
| 16 | + // JS plugins |
| 17 | + concat = require("gulp-concat"), |
| 18 | + uglify = require("gulp-uglify"), |
| 19 | + |
| 20 | + // Image plugin |
| 21 | + imagemin = require("gulp-imagemin"), |
| 22 | + |
| 23 | + // General plugins |
| 24 | + gutil = require("gulp-util"), |
| 25 | + plumber = require("gulp-plumber"), |
| 26 | + size = require("gulp-size"), |
| 27 | + watch = require("gulp-watch"), |
| 28 | + browserSync = require("browser-sync"), |
| 29 | + reload = browserSync.reload; |
| 30 | + |
| 31 | +// Tasks |
| 32 | +// ------------------------------------------------------------------- |
| 33 | +// Start server |
| 34 | +gulp.task("browser-sync", function() { |
| 35 | + browserSync({ |
| 36 | + server: { |
| 37 | + baseDir: "dist" |
| 38 | + } |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +// Notify on error with a beep |
| 43 | +var onError = function(error) { |
| 44 | + console.log(gutil.colors.red(error.message)); |
| 45 | + // https://github.com/floatdrop/gulp-plumber/issues/17 |
| 46 | + this.emit("end"); |
| 47 | + gutil.beep(); |
| 48 | +}; |
| 49 | + |
| 50 | +// HTML task |
| 51 | +gulp.task("html", function() { |
| 52 | + return gulp.src("src/html/*.html") |
| 53 | + // Prevent gulp.watch from crashing |
| 54 | + .pipe(plumber(onError)) |
| 55 | + // Set up HTML templating |
| 56 | + .pipe(fileinclude({ |
| 57 | + prefix: "@@", |
| 58 | + basepath: "src/html" |
| 59 | + })) |
| 60 | + // Clean up HTML a little |
| 61 | + .pipe(htmlmin({ |
| 62 | + removeCommentsFromCDATA: true, |
| 63 | + removeRedundantAttributes: true, |
| 64 | + removeEmptyAttributes: true, |
| 65 | + removeScriptTypeAttributes: true, |
| 66 | + removeStyleLinkTypeAttributes: true, |
| 67 | + caseSensitive: true, |
| 68 | + minifyCSS: true |
| 69 | + })) |
| 70 | + // Where to store the finalized HTML |
| 71 | + .pipe(gulp.dest("dist")); |
| 72 | +}); |
| 73 | + |
| 74 | +// CSS task |
| 75 | +gulp.task("css", function() { |
| 76 | + return gulp.src("src/scss/main.scss") |
| 77 | + // Prevent gulp.watch from crashing |
| 78 | + .pipe(plumber(onError)) |
| 79 | + // Compile Sass |
| 80 | + .pipe(sass({ style: "compressed", noCache: true })) |
| 81 | + // parse CSS and add vendor-prefixed CSS properties |
| 82 | + .pipe(autoprefixer({ |
| 83 | + browsers: ["last 2 versions"] |
| 84 | + })) |
| 85 | + // Minify CSS |
| 86 | + .pipe(cssmin()) |
| 87 | + // Rename the file |
| 88 | + .pipe(rename("production.css")) |
| 89 | + // Show sizes of minified CSS files |
| 90 | + .pipe(size({ showFiles: true })) |
| 91 | + // Where to store the finalized CSS |
| 92 | + .pipe(gulp.dest("dist/css")); |
| 93 | +}); |
| 94 | + |
| 95 | +// JS task |
| 96 | +gulp.task("js", function() { |
| 97 | + return gulp.src("src/js/**/*") |
| 98 | + // Prevent gulp.watch from crashing |
| 99 | + .pipe(plumber(onError)) |
| 100 | + // Concatenate all JS files into one |
| 101 | + .pipe(concat("production.js")) |
| 102 | + // Where to store the finalized JS |
| 103 | + .pipe(gulp.dest("dist/js")); |
| 104 | +}); |
| 105 | + |
| 106 | +// Image task |
| 107 | +gulp.task("images", function() { |
| 108 | + return gulp.src("src/img/**/*.+(png|jpeg|jpg|gif|svg)") |
| 109 | + // Prevent gulp.watch from crashing |
| 110 | + .pipe(plumber(onError)) |
| 111 | + // Minify the images |
| 112 | + .pipe(imagemin()) |
| 113 | + // Where to store the finalized images |
| 114 | + .pipe(gulp.dest("dist/img")); |
| 115 | +}); |
| 116 | + |
| 117 | +// Use default task to launch BrowserSync and watch all files |
| 118 | +gulp.task("default", gulp.parallel("browser-sync"), function () { |
| 119 | + // All browsers reload after tasks are complete |
| 120 | + // Watch HTML files |
| 121 | + watch("src/html/**/*", function () { |
| 122 | + gulp.start("html", reload); |
| 123 | + }); |
| 124 | + // Watch Sass files |
| 125 | + watch("src/scss/**/*", function () { |
| 126 | + gulp.start('css', reload); |
| 127 | + }); |
| 128 | + // Watch JS files |
| 129 | + watch("src/js/**/*", function () { |
| 130 | + gulp.start("js", reload); |
| 131 | + }); |
| 132 | + // Watch image files |
| 133 | + watch("src/img/**/*.+(png|jpeg|jpg|gif|svg)", function () { |
| 134 | + gulp.start("images", reload); |
| 135 | + }); |
| 136 | +}); |
0 commit comments