|
| 1 | +/* Import our Node.js modules */ |
| 2 | +const gulp = require('gulp'), |
| 3 | + cssmin = require('gulp-cssmin'), |
| 4 | + environments = require('gulp-environments'), |
| 5 | + gulpif = require('gulp-if'), |
| 6 | + imagemin = require('gulp-imagemin'), |
| 7 | + less = require('gulp-less'), |
| 8 | + sass = require('gulp-sass'), |
| 9 | + postcss = require('gulp-postcss'), |
| 10 | + rename = require('gulp-rename'), |
| 11 | + rimraf = require('rimraf'), |
| 12 | + sourcemaps = require('gulp-sourcemaps'), |
| 13 | + path = require('path'), |
| 14 | + config = require('./gulpfile-config'); |
| 15 | + |
| 16 | + |
| 17 | +/* Declare our environments */ |
| 18 | +const development = environments.development, |
| 19 | + staging = environments.make('staging'), |
| 20 | + production = environments.production; |
| 21 | + |
| 22 | +/* Set gulp.series and gulp.parallel to constants for convenience sake */ |
| 23 | +const series = gulp.series, |
| 24 | + parallel = gulp.parallel; |
| 25 | + |
| 26 | +/* Extract some config properties for convenience */ |
| 27 | +const shouldAddSourcemaps = config.sourcemaps, |
| 28 | + shouldMinify = config.minify; |
| 29 | + |
| 30 | + |
| 31 | +/* Declare our gulp tasks */ |
| 32 | +gulp.task('build', series(clean, parallel(series(styles, minifyStyles), images))); |
| 33 | +gulp.task('default', series('build', watch)); |
| 34 | + |
| 35 | +/* Describe our gulp tasks */ |
| 36 | +gulp.task('build').description = 'Clean out the build folder then compile styles, minify images, and copy assets into the build folder'; |
| 37 | +gulp.task('default').description = 'Run the build task and watch for any changes'; |
| 38 | + |
| 39 | + |
| 40 | +function clean(done) { |
| 41 | + rimraf(config.buildDir, done); |
| 42 | +} |
| 43 | +clean.description = 'Cleans the build folder'; |
| 44 | + |
| 45 | + |
| 46 | +function styles() { |
| 47 | + let sConfig = config.styles, |
| 48 | + files = sConfig.sourceFiles, |
| 49 | + source = sConfig.sourceDir, |
| 50 | + dest = sConfig.destinationDir, |
| 51 | + mapsDir = sConfig.mapsDir, |
| 52 | + postcssConfig = sConfig.postcss; |
| 53 | + |
| 54 | + return compileLess(source, dest, files, mapsDir, postcssConfig); |
| 55 | +} |
| 56 | +styles.description = 'Compiles SCSS files to CSS; adds source maps if specified'; |
| 57 | + |
| 58 | + |
| 59 | +function minifyStyles(done) { |
| 60 | + if (shouldMinify) { |
| 61 | + let dir = config.styles.destinationDir; |
| 62 | + return minifyCSS(dir, dir, false); |
| 63 | + } |
| 64 | + done(); |
| 65 | +} |
| 66 | +minifyStyles.description = 'Minify CSS files'; |
| 67 | + |
| 68 | + |
| 69 | +function images() { |
| 70 | + let iConfig = config.images, |
| 71 | + sourceDir = iConfig.sourceDir, |
| 72 | + destDir = iConfig.destinationDir; |
| 73 | + |
| 74 | + return minifyImages(sourceDir, destDir); |
| 75 | +} |
| 76 | +images.description = 'Minify images back into the same (source) folder'; |
| 77 | + |
| 78 | + |
| 79 | +function watch(done) { |
| 80 | + if (development()) { |
| 81 | + gulp.watch(config.styles.sourceFiles, styles); |
| 82 | + } |
| 83 | + done(); |
| 84 | +} |
| 85 | +watch.description = 'Watch relevant files and re-run their tasks (only in development environment)'; |
| 86 | + |
| 87 | + |
| 88 | +/** |
| 89 | + * Generic function to compile LESS files |
| 90 | + * @param {String} sourceDir a string representing the path to the directory for the LESS files |
| 91 | + * @param {String} destDir a string representing the path to the directory where the compiled files would be saved |
| 92 | + * @param {String|Array} files string or array of strings representing the glob match for the source files |
| 93 | + * @param {String} mapsDir the directory where sourcemaps would be stored (relative to the destination directory) |
| 94 | + * @param {Array} postcssConfig an array of postcss processors |
| 95 | + * @returns {*} the gulp stream |
| 96 | + */ |
| 97 | +function compileLess(sourceDir, destDir, files, mapsDir, postcssConfig) { |
| 98 | + return compileStyles(sourceDir, destDir, files, mapsDir, less, postcssConfig); |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * Generic function to compile Sass files |
| 103 | + * @param {String} sourceDir a string representing the path to the directory for the Sass files |
| 104 | + * @param {String} destDir a string representing the path to the directory where the compiled files would be saved |
| 105 | + * @param {String|Array} files string or array of strings representing the glob match for the source files |
| 106 | + * @param {String} mapsDir the directory where sourcemaps would be stored (relative to the destination directory) |
| 107 | + * @param {Array} postcssConfig an array of postcss processors |
| 108 | + * @returns {*} the gulp stream |
| 109 | + */ |
| 110 | +function compileSass(sourceDir, destDir, files, mapsDir, postcssConfig) { |
| 111 | + return compileStyles(sourceDir, destDir, files, mapsDir, function () { |
| 112 | + return sass().on('error', sass.logError); |
| 113 | + }, postcssConfig); |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * A generic function to compile both LESS and Sass files |
| 118 | + * @param {String} sourceDir a string representing the path to the directory for the source files |
| 119 | + * @param {String} destDir a string representing the path to the directory where the compiled files would be saved |
| 120 | + * @param {String|Array} files string or array of strings representing the glob match for the source files |
| 121 | + * @param {String} mapsDir the directory where sourcemaps would be stored (relative to the destination directory) |
| 122 | + * @param {Function} buildFxn the build function to use. could be less or sass functions |
| 123 | + * @param {Array} postcssConfig an array of postcss processors |
| 124 | + */ |
| 125 | +function compileStyles(sourceDir, destDir, files, mapsDir, buildFxn, postcssConfig) { |
| 126 | + return gulp.src(files, {base: sourceDir}) |
| 127 | + .pipe(gulpif(shouldAddSourcemaps, sourcemaps.init())) |
| 128 | + .pipe(buildFxn()) |
| 129 | + .pipe(postcss(postcssConfig)) |
| 130 | + .pipe(gulpif(shouldAddSourcemaps, sourcemaps.write(mapsDir))) |
| 131 | + .pipe(gulp.dest(destDir)); |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * A generic function to copy files/directories to a different directory |
| 136 | + * @param {String|Array} source a string or array of string representing the glob match for the source files/folders |
| 137 | + * @param {String} destination the new directory to copy |
| 138 | + */ |
| 139 | +function copy(source, destination) { |
| 140 | + return gulp.src(source) |
| 141 | + .pipe(gulp.dest(destination)); |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +/** |
| 146 | + * Generic function to minify CSS files |
| 147 | + * @param {string} sourceDir a string representing the path to the directory for the source files |
| 148 | + * @param {string} destDir a string representing the path to the directory where the minified files would be saved |
| 149 | + * @param {boolean} shouldRename determine whether the new file should be renamed or not |
| 150 | + * @returns {*} the gulp stream |
| 151 | + */ |
| 152 | +function minifyCSS(sourceDir, destDir, shouldRename) { |
| 153 | + let files = [path.join(sourceDir, '**/*.css'), path.join('!' + sourceDir, '**/*.min.css')]; |
| 154 | + return minify(files, destDir, cssmin, shouldRename); |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * A generic function to minify images |
| 159 | + * @param {string} sourceDir a string representing the path to the directory for the source files |
| 160 | + * @param {string} destDir a string representing the path to the directory where the minified files would be saved |
| 161 | + * @returns {*} the gulp stream |
| 162 | + */ |
| 163 | +function minifyImages(sourceDir, destDir) { |
| 164 | + let files = path.join(sourceDir, '**/*'); |
| 165 | + return minify(files, destDir, imagemin, false); |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + * A generic function to minify different types of files |
| 170 | + * @param {string|array} source a string or array of strings representing the glob match for the source files/folders |
| 171 | + * @param {string} destDir a string representing the path to the directory where the minified files would be saved |
| 172 | + * @param {function} minifyFxn the minify function to use |
| 173 | + * @param {boolean} shouldRename determine whether the new file should be renamed or not |
| 174 | + */ |
| 175 | +function minify(source, destDir, minifyFxn, shouldRename) { |
| 176 | + if ('undefined' === typeof shouldRename) { |
| 177 | + shouldRename = true; |
| 178 | + } |
| 179 | + return gulp.src(source) |
| 180 | + .pipe(minifyFxn()) |
| 181 | + .pipe(gulpif((shouldRename), rename({suffix: '.min'}))) |
| 182 | + .pipe(gulp.dest(destDir)); |
| 183 | +} |
0 commit comments