|
| 1 | +/*jshint node:true */ |
| 2 | + |
| 3 | +'use strict'; |
| 4 | + |
| 5 | +const path = require('path'); |
| 6 | +const gulp = require('gulp'); |
| 7 | +const gutil = require('gulp-util'); |
| 8 | +const concat = require('gulp-concat-util'); |
| 9 | +const runSequence = require('run-sequence'); |
| 10 | +const webpack = require('webpack-stream'); |
| 11 | +const imagemin = require('gulp-imagemin'); |
| 12 | +const del = require('del'); |
| 13 | +const bump = require('gulp-bump'); |
| 14 | +const sass = require('gulp-sass'); |
| 15 | +const ejs = require('gulp-ejs'); |
| 16 | +const PACKAGE = require('./package.json'); |
| 17 | + |
| 18 | +const assets = { |
| 19 | + views: { |
| 20 | + watch: 'src/frontend/views/**/*.ejs', |
| 21 | + src: 'src/frontend/views/*.ejs', |
| 22 | + dest: 'dist/' |
| 23 | + }, |
| 24 | + fonts: { |
| 25 | + watch: 'src/frontend/fonts/**/*.{ttf,woff,woff2,eof,eot,svg,otf}', |
| 26 | + dest: 'dist/fonts' |
| 27 | + }, |
| 28 | + images: { |
| 29 | + watch: 'src/frontend/images/**/*.{png,jpg,gif}', |
| 30 | + dest: 'dist/images' |
| 31 | + }, |
| 32 | + scss: { |
| 33 | + watch: 'src/frontend/scss/**/*.scss', |
| 34 | + loadPath: 'src/frontend/scss', |
| 35 | + src: 'src/frontend/scss/styles.scss', |
| 36 | + dest: 'dist/css' |
| 37 | + }, |
| 38 | + js: { |
| 39 | + watch: 'src/frontend/js/**/*', |
| 40 | + src: 'src/frontend/js/main.js', |
| 41 | + dest: 'dist/js/' |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +/** |
| 46 | + * @param color |
| 47 | + * @param label |
| 48 | + * @returns {Function} |
| 49 | + */ |
| 50 | +function logger (color, label) { |
| 51 | + return function () { |
| 52 | + let args = Array.prototype.slice.call(arguments); |
| 53 | + args.unshift(gutil.colors[color].bold(label.toUpperCase() + ':')); |
| 54 | + gutil.log.apply(null, args); |
| 55 | + }; |
| 56 | +} |
| 57 | + |
| 58 | +gutil.error = logger('red', 'error'); |
| 59 | +gutil.warn = logger('yellow', 'warn'); |
| 60 | +gutil.notice = logger('white', 'notice'); |
| 61 | + |
| 62 | +/** |
| 63 | + * @param err |
| 64 | + */ |
| 65 | +function handleError (err) { |
| 66 | + gutil.error(err.stack); |
| 67 | +} |
| 68 | + |
| 69 | +/***************************** |
| 70 | + TASKS |
| 71 | + ******************************/ |
| 72 | + |
| 73 | +/** |
| 74 | + * clean |
| 75 | + */ |
| 76 | +gulp.task('clean', function (cb) { |
| 77 | + del(['./dist/*']) |
| 78 | + .then(function () { |
| 79 | + cb(); |
| 80 | + }) |
| 81 | + .catch(handleError); |
| 82 | +}); |
| 83 | + |
| 84 | +/** |
| 85 | + * images |
| 86 | + */ |
| 87 | +gulp.task('images', function () { |
| 88 | + if (process.arch !== 'arm') { |
| 89 | + return gulp.src(assets.images.watch) |
| 90 | + .pipe(imagemin({ |
| 91 | + optimizationLevel: 7 |
| 92 | + })) |
| 93 | + .pipe(gulp.dest(assets.images.dest)) |
| 94 | + .on('error', handleError); |
| 95 | + } else { |
| 96 | + return gulp.src(assets.images.watch) |
| 97 | + .pipe(gulp.dest(assets.images.dest)) |
| 98 | + .on('error', handleError); |
| 99 | + } |
| 100 | +}); |
| 101 | + |
| 102 | +/** |
| 103 | + * fonts |
| 104 | + */ |
| 105 | +gulp.task('fonts', function () { |
| 106 | + return gulp.src(assets.fonts.watch) |
| 107 | + .pipe(gulp.dest(assets.fonts.dest)) |
| 108 | + .on('error', handleError); |
| 109 | +}); |
| 110 | + |
| 111 | +/** |
| 112 | + * scss |
| 113 | + */ |
| 114 | +gulp.task('scss', function () { |
| 115 | + return gulp.src(assets.scss.src) |
| 116 | + .pipe(sass().on('error', sass.logError)) |
| 117 | + .pipe(concat.header('@import url(\'https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700\');@import url(\'https://fonts.googleapis.com/css?family=Roboto:100,200,300,400,500,600,700|Roboto+Condensed:300,400,700\');')) |
| 118 | + .pipe(gulp.dest(path.resolve(assets.scss.dest))); |
| 119 | +}); |
| 120 | + |
| 121 | +/** |
| 122 | + * js |
| 123 | + */ |
| 124 | +gulp.task('js', function () { |
| 125 | + return gulp.src(assets.js.src) |
| 126 | + .pipe(webpack(require('./webpack.config.js'))) |
| 127 | + .pipe(gulp.dest(assets.js.dest)) |
| 128 | + .on('error', handleError); |
| 129 | +}); |
| 130 | + |
| 131 | +/** |
| 132 | + * views |
| 133 | + */ |
| 134 | +gulp.task('views', function () { |
| 135 | + return gulp.src(assets.views.src) |
| 136 | + .pipe(ejs({ |
| 137 | + version: PACKAGE.version |
| 138 | + }, {}, { |
| 139 | + ext: '.html' |
| 140 | + })) |
| 141 | + .on('error', handleError) |
| 142 | + .pipe(gulp.dest(assets.views.dest)); |
| 143 | +}); |
| 144 | + |
| 145 | +/** |
| 146 | + * bump |
| 147 | + */ |
| 148 | +gulp.task('bump', function () { |
| 149 | + gulp.src('./package.json') |
| 150 | + .pipe(bump({type: 'version'})) |
| 151 | + .pipe(gulp.dest('./')); |
| 152 | +}); |
| 153 | + |
| 154 | +/** |
| 155 | + * build |
| 156 | + */ |
| 157 | +gulp.task('build', function (cb) { |
| 158 | + runSequence('clean', ['images', 'fonts', 'scss', 'js', 'views'], cb); |
| 159 | +}); |
| 160 | + |
| 161 | +/** |
| 162 | + * default |
| 163 | + */ |
| 164 | +gulp.task('default', ['build'], function () { |
| 165 | + gulp.watch(assets.scss.watch, ['scss']); |
| 166 | + gulp.watch(assets.images.watch, ['images']); |
| 167 | + gulp.watch(assets.fonts.watch, ['fonts']); |
| 168 | + gulp.watch(assets.js.watch, ['js']); |
| 169 | + gulp.watch(assets.views.watch, ['views']); |
| 170 | + gulp.watch('./webpack.config.js', ['js']); |
| 171 | +}); |
0 commit comments