gulpfile.js 4.1 KB

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