| 1 |
/* ==Requirements to build the gulp js file== */ |
| 2 |
const gulp = require('gulp'); |
| 3 |
const runSequence = require('gulp4-run-sequence'); |
| 4 |
const uglify = require('gulp-uglify'); |
| 5 |
const uglifycss = require('gulp-uglifycss'); |
| 6 |
const rename = require('gulp-rename'); |
| 7 |
const postcss = require('gulp-postcss'); |
| 8 |
const sourcemaps = require('gulp-sourcemaps'); |
| 9 |
const gutil = require('gulp-util'); |
| 10 |
const autoprefixer = require('autoprefixer'); |
| 11 |
const clean = require('gulp-clean'); |
| 12 |
const plumber = require('gulp-plumber'); |
| 13 |
const sass = require('gulp-sass')(require('sass')); |
| 14 |
const notify = require('gulp-notify') |
| 15 |
const webpack = require('webpack'); |
| 16 |
const webpackStream = require('webpack-stream'); |
| 17 |
|
| 18 |
const outputPath = 'dist/src'; |
| 19 |
|
| 20 |
// set clean paths |
| 21 |
const cleanPaths = [ |
| 22 |
'dist/*', |
| 23 |
'composer.lock', |
| 24 |
'package-lock.json' |
| 25 |
]; |
| 26 |
|
| 27 |
const handlebarsPaths = [ |
| 28 |
'node_modules/handlebars/dist/handlebars.js', |
| 29 |
'node_modules/handlebars/dist/handlebars.min.js', |
| 30 |
'node_modules/handlebars/dist/handlebars.runtime.js', |
| 31 |
'node_modules/handlebars/dist/handlebars.runtime.min.js', |
| 32 |
'node_modules/handlebars/LICENSE' |
| 33 |
]; |
| 34 |
|
| 35 |
const jsWatchPaths = [ |
| 36 |
'./src/**/*.js', |
| 37 |
]; |
| 38 |
|
| 39 |
const jsToBeCompiledPaths = { |
| 40 |
'updraft-theme/theme': './src/updraft-theme/theme.js', |
| 41 |
}; |
| 42 |
|
| 43 |
const jsPaths = [ |
| 44 |
'./src/**/*.js', |
| 45 |
...( |
| 46 |
Object.keys(jsToBeCompiledPaths).map(name => name.split('/')[0]).map(folder => `!./src/${folder}/**/*.js`) |
| 47 |
) |
| 48 |
]; |
| 49 |
|
| 50 |
const phpPaths = [ |
| 51 |
'src/**/*.php' |
| 52 |
]; |
| 53 |
|
| 54 |
const svgsPaths = [ |
| 55 |
'src/**/*.svg' |
| 56 |
]; |
| 57 |
|
| 58 |
var fontPaths = [ |
| 59 |
'src/**/*.woff2', |
| 60 |
'src/**/*.ttf' |
| 61 |
]; |
| 62 |
|
| 63 |
const scssWatchPaths = [ |
| 64 |
'src/updraft-theme/**/*.scss', |
| 65 |
]; |
| 66 |
|
| 67 |
const scssPaths = [ |
| 68 |
'src/updraft-theme/theme.scss', |
| 69 |
'src/updraft-theme/theme-colors.scss', |
| 70 |
]; |
| 71 |
|
| 72 |
const cssWatchPaths = [ |
| 73 |
'src/**/*.css', |
| 74 |
]; |
| 75 |
|
| 76 |
const cssPaths = [ |
| 77 |
'src/**/*.css', |
| 78 |
]; |
| 79 |
|
| 80 |
const htmlPaths = [ |
| 81 |
'src/**/*.html' |
| 82 |
]; |
| 83 |
|
| 84 |
/* ==Start Gulp Process== */ |
| 85 |
gulp.copy = function(src,dest) { |
| 86 |
return gulp.src(src, {base:"."}) |
| 87 |
.pipe(plumber(reportError)) |
| 88 |
.pipe(gulp.dest(dest)); |
| 89 |
}; |
| 90 |
|
| 91 |
/* ==Images=== */ |
| 92 |
gulp.task('svgs_move', function(){ |
| 93 |
return gulp.src(svgsPaths) |
| 94 |
.pipe(plumber(reportError)) |
| 95 |
.pipe(gulp.dest(outputPath)); |
| 96 |
}) |
| 97 |
|
| 98 |
/* ==Fonts=== */ |
| 99 |
gulp.task('fonts_move', function(){ |
| 100 |
return gulp.src(fontPaths) |
| 101 |
.pipe(plumber(reportError)) |
| 102 |
.pipe(gulp.dest(outputPath)); |
| 103 |
}); |
| 104 |
|
| 105 |
/* ==Sorting HTML=== */ |
| 106 |
gulp.task('html_move', function(){ |
| 107 |
return gulp.src(htmlPaths) |
| 108 |
.pipe(plumber(reportError)) |
| 109 |
.pipe(gulp.dest(outputPath)); |
| 110 |
}); |
| 111 |
|
| 112 |
/* ===Sorting SCSS=== */ |
| 113 |
gulp.task('scss_compile', function(){ |
| 114 |
return gulp.src(scssPaths, { base: 'src' }) |
| 115 |
.pipe(plumber(reportError)) |
| 116 |
.pipe(sourcemaps.init()) |
| 117 |
.pipe(sass({silenceDeprecations: ['legacy-js-api', 'mixed-decls', 'color-functions', 'global-builtin', 'import']})) |
| 118 |
.pipe(postcss([ autoprefixer({ browsers: ['last 2 versions'] }) ])) |
| 119 |
.pipe(plumber(reportError)) |
| 120 |
.pipe(gulp.dest(outputPath)) |
| 121 |
.pipe(uglifycss({'maxLineLen': 0, 'uglyComments': true}).on('error', console.error)) |
| 122 |
.pipe(rename({suffix: '.min'})) |
| 123 |
.pipe(sourcemaps.write('.')) |
| 124 |
.pipe(plumber.stop()) |
| 125 |
.pipe(gulp.dest(outputPath)); |
| 126 |
}); |
| 127 |
|
| 128 |
gulp.task('copy_css', function () { |
| 129 |
return gulp.src(cssPaths, { base: './src' }) |
| 130 |
.pipe(gulp.dest(outputPath)); |
| 131 |
}); |
| 132 |
|
| 133 |
/* ===Sorting JS=== */ |
| 134 |
gulp.task('js_compile', function(){ |
| 135 |
return webpackStream({ |
| 136 |
mode: 'production', |
| 137 |
entry: jsToBeCompiledPaths, |
| 138 |
output: { |
| 139 |
filename: '[name].js', |
| 140 |
}, |
| 141 |
}, webpack) |
| 142 |
.pipe(gulp.dest(outputPath)); |
| 143 |
}); |
| 144 |
|
| 145 |
gulp.task('copy_plain_js', function () { |
| 146 |
return gulp.src(jsPaths, { base: './src' }) |
| 147 |
.pipe(gulp.dest(outputPath)); |
| 148 |
}); |
| 149 |
|
| 150 |
/* ====Sorting PHP======= */ |
| 151 |
gulp.task('php_move', function(){ |
| 152 |
return gulp.src(phpPaths) |
| 153 |
.pipe(plumber(reportError)) |
| 154 |
.pipe(gulp.dest(outputPath)); |
| 155 |
}) |
| 156 |
|
| 157 |
gulp.task('copy_handlebar_to_updraft_theme', function(){ |
| 158 |
return gulp.src(handlebarsPaths) |
| 159 |
.pipe(plumber(reportError)) |
| 160 |
.pipe(gulp.dest(outputPath + '/updraft-theme/handlebar-library')); |
| 161 |
}); |
| 162 |
|
| 163 |
gulp.task('copy_readme', function () { |
| 164 |
return gulp.src('README.md') |
| 165 |
.pipe(gulp.dest('dist')); |
| 166 |
}); |
| 167 |
|
| 168 |
gulp.task('copy_composer_json', function () { |
| 169 |
return gulp.src('composer.json') |
| 170 |
.pipe(gulp.dest('dist')); |
| 171 |
}); |
| 172 |
|
| 173 |
/*== Clean theme build ==*/ |
| 174 |
gulp.task('clean', function(){ |
| 175 |
return gulp.src(cleanPaths, { allowEmpty: true }) |
| 176 |
.pipe(plumber(reportError)) |
| 177 |
.pipe(clean({force:true})); |
| 178 |
}) |
| 179 |
|
| 180 |
/*== Building the files ==*/ |
| 181 |
gulp.task('build', function(done){ |
| 182 |
runSequence('clean', |
| 183 |
['js_compile', 'copy_plain_js'], |
| 184 |
['php_move'], |
| 185 |
['html_move', 'copy_css', 'scss_compile', 'svgs_move', 'fonts_move'], |
| 186 |
['copy_handlebar_to_updraft_theme'], |
| 187 |
['copy_readme', 'copy_composer_json'], |
| 188 |
done |
| 189 |
); |
| 190 |
}); |
| 191 |
|
| 192 |
/*== These tasks are ran to build gulp files ==*/ |
| 193 |
|
| 194 |
// ran with gulp build |
| 195 |
gulp.task('default', function(){ |
| 196 |
runSequence('clean', 'build', 'watch'); |
| 197 |
}); |
| 198 |
|
| 199 |
gulp.task('php', function(done){ |
| 200 |
runSequence('php_move', done); |
| 201 |
}); |
| 202 |
|
| 203 |
gulp.task('js', function(done) { |
| 204 |
runSequence('js_compile', 'copy_plain_js', done); |
| 205 |
}); |
| 206 |
|
| 207 |
gulp.task('scss', function(done) { |
| 208 |
runSequence('scss_compile', done); |
| 209 |
}); |
| 210 |
|
| 211 |
gulp.task('css', function(done) { |
| 212 |
runSequence('copy_css', done); |
| 213 |
}); |
| 214 |
|
| 215 |
gulp.task('html', function(done){ |
| 216 |
runSequence('html_move', done); |
| 217 |
}); |
| 218 |
|
| 219 |
gulp.task('watch', function(){ |
| 220 |
gulp.watch(scssWatchPaths, gulp.series(['scss'])); |
| 221 |
gulp.watch(cssWatchPaths, gulp.series(['css'])); |
| 222 |
gulp.watch(jsWatchPaths, gulp.series(['js'])); |
| 223 |
gulp.watch(phpPaths, gulp.series(['php'])); |
| 224 |
gulp.watch('src/updraft-theme/**/*.handlebars.html', gulp.series(['html'])); |
| 225 |
}); |
| 226 |
|
| 227 |
// Setup pretty error handling |
| 228 |
var reportError = function (error) { |
| 229 |
var lineNumber = (error.lineNumber) ? 'LINE ' + error.lineNumber + ' -- ' : ''; |
| 230 |
var report = ''; |
| 231 |
var chalk = gutil.colors.white.bgRed; |
| 232 |
|
| 233 |
// Shows a pop when errors |
| 234 |
notify({ |
| 235 |
title: 'Task Failed [' + error.plugin + ']', |
| 236 |
message: lineNumber + 'See console.', |
| 237 |
sound: 'Sosumi' // See: https://github.com/mikaelbr/node-notifier#all-notification-options-with-their-defaults |
| 238 |
}).write(error); |
| 239 |
|
| 240 |
report += chalk('GULP TASK:') + ' [' + error.plugin + ']\n'; |
| 241 |
report += chalk('PROB:') + ' ' + error.message + '\n'; |
| 242 |
if (error.lineNumber) { report += chalk('LINE:') + ' ' + error.lineNumber + '\n'; } |
| 243 |
if (error.fileName) { report += chalk('FILE:') + ' ' + error.fileName + '\n'; } |
| 244 |
console.error(report); |
| 245 |
// console.log(error); |
| 246 |
process.exit(1); |
| 247 |
} |
| 248 |
|