Advertisement
Guest User

Untitled

a guest
Jun 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. var gulp = require('gulp'),
  2. gutil = require('gulp-util' ),
  3. sass = require('gulp-sass'),
  4. browserSync = require('browser-sync'),
  5. concat = require('gulp-concat'),
  6. uglify = require('gulp-uglify'),
  7. cleanCSS = require('gulp-clean-css'),
  8. rename = require('gulp-rename'),
  9. del = require('del'),
  10. imagemin = require('gulp-imagemin'),
  11. pngquant = require('imagemin-pngquant'),
  12. cache = require('gulp-cache'),
  13. rigger = require('gulp-rigger'),
  14. autoprefixer = require('gulp-autoprefixer'),
  15. ftp = require('vinyl-ftp'),
  16. sourcemaps = require('gulp-sourcemaps'),
  17. reload = browserSync.reload,
  18. notify = require("gulp-notify");
  19.  
  20. var path = {
  21. build: { //Тут мы укажем куда складывать готовые после сборки файлы
  22. html: 'build/',
  23. js: 'build/js/',
  24. css: 'build/css/',
  25. img: 'build/img/',
  26. fonts: 'build/fonts/'
  27. },
  28. src: { //Пути откуда брать исходники
  29. html: 'src/*.html', //Синтаксис src/*.html говорит gulp что мы хотим взять все файлы с расширением .html
  30. js: 'src/js/common.js',//В стилях и скриптах нам понадобятся только main файлы
  31. style: 'src/style/main.sass',
  32. img: 'src/img/**/*.*', //Синтаксис img/**/*.* означает - взять все файлы всех расширений из папки и из вложенных каталогов
  33. fonts: 'src/fonts/**/*.*'
  34. },
  35. watch: { //Тут мы укажем, за изменением каких файлов мы хотим наблюдать
  36. html: 'src/**/*.html',
  37. js: 'src/js/**/*.js',
  38. style: 'src/style/**/*.sass',
  39. img: 'src/img/**/*.*',
  40. fonts: 'src/fonts/**/*.*'
  41. },
  42. clean: './build'
  43. };
  44. var config = {
  45. server: {
  46. baseDir: "./build"
  47. },
  48. tunnel: true,
  49. host: 'localhost',
  50. port: 9000,
  51. logPrefix: "Frontend_Devil"
  52. };
  53. gulp.task('html:build', function () {
  54. gulp.src(path.src.html) //Выберем файлы по нужному пути
  55. .pipe(rigger()) //Прогоним через rigger
  56. .pipe(gulp.dest(path.build.html)) //Выплюнем их в папку build
  57. .pipe(reload({stream: true})); //И перезагрузим наш сервер для обновлений
  58. });
  59. gulp.task('js:build', function () {
  60. gulp.src(path.src.js) //Найдем наш main файл
  61. .pipe(rigger()) //Прогоним через rigger
  62. .pipe(sourcemaps.init()) //Инициализируем sourcemap
  63. .pipe(uglify()) //Сожмем наш js
  64. .pipe(sourcemaps.write()) //Пропишем карты
  65. .pipe(gulp.dest(path.build.js)) //Выплюнем готовый файл в build
  66. .pipe(reload({stream: true})); //И перезагрузим сервер
  67. });
  68. gulp.task('style:build', function () {
  69. gulp.src(path.src.style) //Выберем наш main.scss
  70. .pipe(sourcemaps.init()) //То же самое что и с js
  71. .pipe(sass({outputStyle: 'expand'}).on("error", notify.onError()))
  72. .pipe(autoprefixer(['last 15 versions']))
  73. .pipe(cleanCSS())
  74. .pipe(sourcemaps.write())
  75. .pipe(gulp.dest(path.build.css)) //И в build
  76. .pipe(reload({stream: true}));
  77. });
  78. gulp.task('img:build', function () {
  79. gulp.src(path.src.img) //Выберем наши картинки
  80. .pipe(imagemin({ //Сожмем их
  81. progressive: true,
  82. svgoPlugins: [{removeViewBox: false}],
  83. use: [pngquant()],
  84. interlaced: true
  85. }))
  86. .pipe(gulp.dest(path.build.img)) //И бросим в build
  87. .pipe(reload({stream: true}));
  88. });
  89. gulp.task('fonts:build', function() {
  90. gulp.src(path.src.fonts)
  91. .pipe(gulp.dest(path.build.fonts))
  92. });
  93. gulp.task('webserver', function () {
  94. browserSync(config);
  95. });
  96. gulp.task('build', [
  97. 'html:build',
  98. 'js:build',
  99. 'style:build',
  100. 'fonts:build',
  101. 'img:build'
  102. ]);
  103. gulp.task('watch', function(){
  104. gulp.watch([path.watch.html], function(event, cb) {
  105. gulp.start('html:build');
  106. });
  107. gulp.watch([path.watch.style], function(event, cb) {
  108. gulp.start('style:build');
  109. });
  110. gulp.watch([path.watch.js], function(event, cb) {
  111. gulp.start('js:build');
  112. });
  113. gulp.watch([path.watch.img], function(event, cb) {
  114. gulp.start('img:build');
  115. });
  116. gulp.watch([path.watch.fonts], function(event, cb) {
  117. gulp.start('fonts:build');
  118. });
  119. });
  120. gulp.task('clean', function (cb) {
  121. rimraf(path.clean, cb);
  122. });
  123. gulp.task('default', ['build', 'webserver', 'watch']);
  124.  
  125. gulp.task('deploy', function() {
  126.  
  127. var conn = ftp.create({
  128. host: 'hostname.com',
  129. user: 'username',
  130. password: 'userpassword',
  131. parallel: 10,
  132. log: gutil.log
  133. });
  134.  
  135. var globs = [
  136. 'dist/**',
  137. 'dist/.htaccess',
  138. ];
  139. return gulp.src(globs, {buffer: false})
  140. .pipe(conn.dest('/path/to/folder/on/server'));
  141.  
  142. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement