|
| 1 | +import browserSync from 'browser-sync' |
| 2 | +import bump from 'gulp-bump' |
| 3 | +import combineMQ from 'gulp-combine-mq' |
| 4 | +import cssnano from 'gulp-cssnano' |
| 5 | +import del from 'del' |
| 6 | +import gulp from 'gulp' |
| 7 | +import gulpIf from 'gulp-if' |
| 8 | +import imagemin from 'gulp-imagemin' |
| 9 | +import include from 'gulp-file-include' |
| 10 | +import plumber from 'gulp-plumber' |
| 11 | +import { resolve } from 'path' |
| 12 | +import runSequence from 'run-sequence' |
| 13 | +import sass from 'gulp-sass' |
| 14 | +import sassGlob from 'gulp-sass-glob' |
| 15 | +import sourcemaps from 'gulp-sourcemaps' |
| 16 | +import spritesmith from 'gulp.spritesmith' |
| 17 | +import watch from 'gulp-watch' |
| 18 | +import webpack from 'webpack' |
| 19 | +import webpackStream from 'webpack-stream' |
| 20 | +import webpackDevMiddleware from 'webpack-dev-middleware' |
| 21 | +import webpackHotMiddleware from 'webpack-hot-middleware' |
| 22 | +import webpackConfig from './webpack.config.babel' |
| 23 | + |
| 24 | +const isProduction = process.env.NODE_ENV === 'production' |
| 25 | +const server = browserSync.create() |
| 26 | + |
| 27 | +/** |
| 28 | + * The development server. |
| 29 | + */ |
| 30 | +gulp.task('server', ['build'], done => { |
| 31 | + const bundler = webpack(webpackConfig) |
| 32 | + |
| 33 | + server.init({ |
| 34 | + notify: false, |
| 35 | + logLevel: 'none', |
| 36 | + server: { |
| 37 | + baseDir: resolve(__dirname, 'dist'), |
| 38 | + middleware: [ |
| 39 | + webpackDevMiddleware(bundler, webpackConfig.devServer), |
| 40 | + webpackHotMiddleware(bundler) |
| 41 | + ] |
| 42 | + } |
| 43 | + }, done) |
| 44 | +}) |
| 45 | + |
| 46 | +/** |
| 47 | + * Webpack build stream. |
| 48 | + */ |
| 49 | +gulp.task('webpack', () => { |
| 50 | + return gulp.src(resolve(__dirname, 'src/js/app.js')) |
| 51 | + .pipe(webpackStream(webpackConfig, webpack)) |
| 52 | + .pipe(gulp.dest(resolve(__dirname, 'dist/js'))) |
| 53 | +}) |
| 54 | + |
| 55 | +/** |
| 56 | + * Compile scss files into css files. |
| 57 | + */ |
| 58 | +gulp.task('styles', () => { |
| 59 | + return gulp.src(resolve(__dirname, 'src/scss/**/*.scss')) |
| 60 | + .pipe(plumber()) |
| 61 | + .pipe(sassGlob()) |
| 62 | + .pipe(gulpIf(isProduction, sourcemaps.init())) |
| 63 | + .pipe(sass({ |
| 64 | + includePaths: ['node_modules'] |
| 65 | + }).on('error', sass.logError)) |
| 66 | + .pipe(combineMQ()) |
| 67 | + .pipe(gulpIf(isProduction, cssnano())) |
| 68 | + .pipe(gulpIf(isProduction, sourcemaps.write('./'))) |
| 69 | + .pipe(gulp.dest(resolve(__dirname, 'dist/css'))) |
| 70 | + .pipe(server.stream()) |
| 71 | +}) |
| 72 | + |
| 73 | +/** |
| 74 | + * Render html files and copy into dist on build. |
| 75 | + */ |
| 76 | +gulp.task('html', () => { |
| 77 | + return gulp.src(resolve(__dirname, 'src/**/*.html')) |
| 78 | + .pipe(plumber()) |
| 79 | + .pipe(include({ |
| 80 | + prefix: '@@', |
| 81 | + basepath: resolve(__dirname, 'src') |
| 82 | + })) |
| 83 | + .pipe(gulp.dest(resolve(__dirname, 'dist'))) |
| 84 | + .pipe(server.stream({ reload: true })) |
| 85 | +}) |
| 86 | + |
| 87 | +/** |
| 88 | + * Minify and copy our images into dist on build. |
| 89 | + */ |
| 90 | +gulp.task('images', () => { |
| 91 | + return gulp.src([ |
| 92 | + resolve(__dirname, 'src/img/**'), |
| 93 | + `!${resolve(__dirname, 'src/img/sprites{,/**}')}` |
| 94 | + ]) |
| 95 | + .pipe(plumber()) |
| 96 | + .pipe(gulpIf(isProduction, imagemin())) |
| 97 | + .pipe(gulp.dest(resolve(__dirname, 'dist/img'))) |
| 98 | + .pipe(server.stream()) |
| 99 | +}) |
| 100 | + |
| 101 | +/** |
| 102 | + * Creates a spritesheet from our img/sprites folder. |
| 103 | + */ |
| 104 | +gulp.task('sprite', () => { |
| 105 | + return gulp.src(resolve(__dirname, 'src/img/sprites/**/*')) |
| 106 | + .pipe(plumber()) |
| 107 | + .pipe(spritesmith({ |
| 108 | + imgName: 'img/sprite.png', |
| 109 | + cssName: 'scss/_sprite.scss' |
| 110 | + })) |
| 111 | + .pipe(gulp.dest(resolve(__dirname, 'src'))) |
| 112 | + .pipe(server.stream()) |
| 113 | +}) |
| 114 | + |
| 115 | +/** |
| 116 | + * Copy font files into dist on build. |
| 117 | + */ |
| 118 | +gulp.task('fonts', () => { |
| 119 | + return gulp.src([ |
| 120 | + resolve(__dirname, 'src/fonts/**/*.{woff,woff2,eot,ttf,otf,svg}'), |
| 121 | + resolve(__dirname, 'node_modules/font-awesome/fonts/**/*.{woff,woff2,eot,ttf,otf,svg}') |
| 122 | + ]) |
| 123 | + .pipe(plumber()) |
| 124 | + .pipe(gulp.dest(resolve(__dirname, 'dist/fonts'))) |
| 125 | + .pipe(server.stream()) |
| 126 | +}) |
| 127 | + |
| 128 | +/** |
| 129 | + * Clean our dist folder before building. |
| 130 | + */ |
| 131 | +gulp.task('clean', () => del([resolve(__dirname, 'dist')], { |
| 132 | + force: true |
| 133 | +})) |
| 134 | + |
| 135 | +/** |
| 136 | + * Watch for file changes. gulp.watch wasn't picking up image changes |
| 137 | + * so switched over to gulp-watch. |
| 138 | + * |
| 139 | + * Note: |
| 140 | + * If we start having issues with multiple file changes at once, |
| 141 | + * gulp-batch (https://github.com/floatdrop/gulp-batch) will help |
| 142 | + * solve those issues. For now I'm leaving out. |
| 143 | + * |
| 144 | + * See for more info: https://github.com/gulpjs/gulp/issues/80 |
| 145 | + */ |
| 146 | +gulp.task('watch', () => { |
| 147 | + watch(resolve(__dirname, 'src/scss/**/*.scss'), () => gulp.start('styles')) |
| 148 | + watch(resolve(__dirname, 'src/**/*.html'), () => gulp.start('html')) |
| 149 | + watch(resolve(__dirname, 'src/img/**'), () => gulp.start('images')) |
| 150 | + watch(resolve(__dirname, 'src/img/sprites/**/*'), () => gulp.start('sprite')) |
| 151 | + watch([ |
| 152 | + resolve(__dirname, 'src/fonts/**/*.{woff,woff2,eot,ttf,otf,svg}'), |
| 153 | + resolve(__dirname, 'node_modules/font-awesome/fonts/**/*.{woff,woff2,eot,ttf,otf,svg}') |
| 154 | + ], () => gulp.start('fonts')) |
| 155 | +}) |
| 156 | + |
| 157 | +/** |
| 158 | + * Bumps the semver patch up by 1 |
| 159 | + */ |
| 160 | +gulp.task('patch', () => { |
| 161 | + return gulp.src(resolve(__dirname, 'package.json')) |
| 162 | + .pipe(bump({ type: 'patch' })) |
| 163 | + .pipe(gulp.dest(resolve(__dirname))) |
| 164 | +}) |
| 165 | + |
| 166 | +/** |
| 167 | + * Bumps the semver minor up by 1 |
| 168 | + */ |
| 169 | +gulp.task('minor', () => { |
| 170 | + return gulp.src(resolve(__dirname, 'package.json')) |
| 171 | + .pipe(bump({ type: 'minor' })) |
| 172 | + .pipe(gulp.dest(resolve(__dirname))) |
| 173 | +}) |
| 174 | + |
| 175 | +/** |
| 176 | + * Bumps the semver major up by 1 |
| 177 | + */ |
| 178 | +gulp.task('major', () => { |
| 179 | + return gulp.src(resolve(__dirname, 'package.json')) |
| 180 | + .pipe(bump({ type: 'major' })) |
| 181 | + .pipe(gulp.dest(resolve(__dirname))) |
| 182 | +}) |
| 183 | + |
| 184 | +/** |
| 185 | + * Dist folder build without cleaning. |
| 186 | + * |
| 187 | + * Note: Webpack is handled by the dev middleware. We don't need to run the task now. |
| 188 | + */ |
| 189 | +gulp.task('build', done => runSequence('sprite', ['styles', 'images', 'html', 'fonts'], done)) |
| 190 | + |
| 191 | +/** |
| 192 | + * Clean dist folder build |
| 193 | + */ |
| 194 | +gulp.task('dist', done => runSequence('clean', 'build', 'webpack', done)) |
| 195 | + |
| 196 | +/** |
| 197 | + * Default Task |
| 198 | + */ |
| 199 | +gulp.task('default', done => runSequence('server', 'watch', done)) |
0 commit comments