Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. "use strict";
  2.  
  3. const gulp = require("gulp");
  4. const sass = require("gulp-sass");
  5. const plumber = require("gulp-plumber");
  6. const postcss = require("gulp-postcss");
  7. const autoprefixer = require("autoprefixer");
  8. const browserSync = require("browser-sync").create();
  9. const mqpacker = require("css-mqpacker");
  10. const csso = require("gulp-csso");
  11. const rename = require("gulp-rename");
  12. const svgmin = require("gulp-svgmin");
  13. const svgstore = require("gulp-svgstore");
  14. const run = require("run-sequence");
  15. const del = require("del");
  16. const imagemin = require("gulp-imagemin");
  17. const pngquant = require("imagemin-pngquant");
  18. const cheerio = require("gulp-cheerio");
  19. const ghPages = require('gulp-gh-pages');
  20.  
  21.  
  22. // ЗАДАЧА: Сборка стилей
  23. gulp.task("style", function() {
  24. gulp.src("sass/style.scss")
  25. .pipe(plumber())
  26. .pipe(sass())
  27. .pipe(postcss([
  28. autoprefixer({browsers: [
  29. "last 2 versions"
  30. ]}),
  31. mqpacker({
  32. sort: false
  33. })
  34. ]))
  35. .pipe(gulp.dest("build/css"))
  36. .pipe(browserSync.stream())
  37. .pipe(csso())
  38. .pipe(rename("style.min.css"))
  39. .pipe(gulp.dest("build/css"));
  40. });
  41.  
  42. // ЗАДАЧА: Копирование изображений
  43. gulp.task("img", function () {
  44. return gulp.src("img/*.{gif,png,jpg,jpeg,svg}")
  45. .pipe(plumber())
  46. .pipe(imagemin({
  47. progressive: true,
  48. svgoPlugins: [{removeViewBox: false}],
  49. use: [pngquant()]
  50. }))
  51. .pipe(gulp.dest("build/img"));
  52. });
  53.  
  54. // ЗАДАЧА: Сборка SVG-спрайта
  55. gulp.task("svgstore", function (callback) {
  56. let spritePath = "img/svg-sprite";
  57. if(fileExist(spritePath) !== false) {
  58. return gulp.src(spritePath + "/*.svg")
  59. .pipe(svgmin(function (file) {
  60. return {
  61. plugins: [{
  62. cleanupIDs: {
  63. minify: true
  64. }
  65. }]
  66. }
  67. }))
  68. .pipe(svgstore({ inlineSvg: true }))
  69. .pipe(cheerio(function ($) {
  70. $("svg").attr("style", "display:none");
  71. }))
  72. .pipe(rename("sprite-svg.svg"))
  73. .pipe(gulp.dest("build/img/"));
  74. }
  75. else {
  76. console.log("Нет файлов для сборки SVG-спрайта");
  77. callback();
  78. }
  79. });
  80.  
  81. // ЗАДАЧА: Копирование шрифтов, скриптов и html
  82. gulp.task("copy", function() {
  83. return gulp.src([
  84. "fonts/**/*.{woff,woff2}",
  85. "js/**",
  86. "*.html"
  87. ], {
  88. base: "."
  89. })
  90. .pipe(gulp.dest("build"));
  91. });
  92.  
  93. gulp.task("clean", function() {
  94. return del("build");
  95. });
  96.  
  97. gulp.task("build", function(fn) {
  98. run(
  99. "clean",
  100. "copy",
  101. "style",
  102. "img",
  103. "svgstore",
  104. fn
  105. );
  106. });
  107.  
  108. gulp.task("html:copy", function() {
  109. return gulp.src("*.html")
  110. .pipe(gulp.dest("build"));
  111. });
  112.  
  113. gulp.task("html:update", ["html:copy"], function(done) {
  114. browserSync.reload();
  115. done();
  116. });
  117.  
  118. gulp.task("serve", ["style"], function() {
  119. browserSync.init({
  120. server: "build",
  121. notify: false,
  122. open: true,
  123. cors: true,
  124. ui: false
  125. });
  126.  
  127. gulp.watch("sass/**/*.{scss,sass}", ["style"]);
  128. gulp.watch("*.html", ["html:update"]);
  129. });
  130.  
  131. gulp.task('deploy', function() {
  132. return gulp.src('./build/**/*')
  133. .pipe(ghPages());
  134. });
  135.  
  136. // ЗАДАЧА: Задача по умолчанию
  137. gulp.task("default", ["serve"]);
  138.  
  139. // Проверка существования файла/папки
  140. function fileExist(path) {
  141. const fs = require("fs");
  142. try {
  143. fs.statSync(path);
  144. } catch(err) {
  145. return !(err && err.code === "ENOENT");
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement