Last active
November 10, 2016 09:48
-
-
Save sergeych-hyuna/b58766b4e928b8dc8aa7adf21755c064 to your computer and use it in GitHub Desktop.
Сборка нового проекта и компиляция scss и js файлов c помощью gulp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Статья про gulp: goo.gl/EpHx9N | |
// Для сборки нового проекта необходимо изменить значения переменных: | |
// src - путь к файлам проекта, на основе которых будет создаваться новый проект, | |
// dist - путь к новому каталогу (создается автоматически). | |
// После изменения переменных необходимо запустить задачу "gulp newProject" - происходит копирование необходимых | |
// для проекта файлов и открытие файлов необходимых для работы с проектом. | |
// Если необходимо проводить работу над существующим проектом, необходимо изменить значение переменной | |
// dist (путь к файлам нужного проекта) и использовать дефолтную задачу "gulp". | |
// Переменная src в этом случае задействована не будет. | |
// Для сжатия изображений предусмотрена задача "gulp imgmin" | |
// Переменные для путей к рабочим файлам | |
var path = { | |
src: 'hybrid_step0/c', | |
dist: 'hybrid_step0/e', | |
media: '../app-media-network/', | |
foundation: '../vendor/jomedia/frontcore/foundation/scss/', | |
htdocs: '' | |
} | |
//Необходимые плагины | |
var gulp = require('gulp'), // Подключаем Gulp | |
del = require('del'), // Подключаем библиотеку для удаления файлов и папок | |
cssnano = require('gulp-cssnano'), // Подключаем пакет для минификации CSS | |
csscomb = require('gulp-csscomb'), | |
concat = require('gulp-concat'), // Подключаем gulp-concat (для конкатенации файлов) | |
rename = require('gulp-rename'), // Подключаем библиотеку для переименования файлов | |
uglify = require('gulp-uglify'), // Подключаем gulp-uglify (для сжатия JS) | |
autoprefixer = require('gulp-autoprefixer'), // Подключаем библиотеку для автоматического добавления префиксов | |
open = require('gulp-open'), // Подключаем библиотеку открытия файлов | |
replace = require('gulp-replace'), // пакет для замены путей | |
notify = require('gulp-notify'), // оповещения Gulp | |
image = require('gulp-image'), // оптимизация изображений | |
uncss = require('gulp-uncss'), // удаление неиспользуемых классов | |
unusedImages = require('gulp-unused-images'), // находит неиспользуемые изображения | |
sass = require('gulp-sass'); //Подключаем Sass пакет | |
// Создание каталогов и копирование файлов в новый проект | |
gulp.task('create', function () { | |
gulp.src(path.media + 'views/signup/' + path.src + '/**/*.*') | |
.pipe(gulp.dest(path.media + 'views/signup/' + path.dist + '/')); // copy Step0 | |
gulp.src(path.media + 'views/layouts/signup/' + path.src + '/**/*.*') | |
.pipe(gulp.dest(path.media + 'views/layouts/signup/' + path.dist + '/')); // copy header.tpl, footer.tpl | |
gulp.src(path.htdocs + 'scss/signup/' + path.src + '/**/*.*') | |
.pipe(gulp.dest(path.htdocs + 'scss/signup/' + path.dist + '/')); // copy *.scss files | |
gulp.src(path.htdocs + 'css/signup/' + path.src + '/**/*.*') | |
.pipe(gulp.dest(path.htdocs + 'css/signup/' + path.dist + '/')); // copy css files | |
gulp.src(path.htdocs + 'js/signup/' + path.src + '/**/*.*') | |
.pipe(gulp.dest(path.htdocs + 'js/signup/' + path.dist + '/')); // copy js files | |
gulp.src(path.htdocs + 'images/signup/' + path.src + '/**/*.*') // copy images | |
.pipe(gulp.dest(path.htdocs + 'images/signup/' + path.dist + '/')); | |
}); | |
//Открытие необходимых файлов в редакторе | |
gulp.task('open', function(){ | |
setTimeout(function(){ | |
// Открываем файл для регистрации проекта | |
gulp.src(path.media + 'controllers/signup_controller.php') | |
.pipe(open()); | |
// Открываем файлы необходимые для работы | |
gulp.src(path.media + 'managers/word_translator_manager.php') | |
.pipe(open()); | |
gulp.src(path.htdocs + 'js/signup/' + path.dist + '/scripts.js') | |
.pipe(open()); | |
gulp.src(path.htdocs + 'scss/signup/' + path.dist + '/_style.scss') | |
.pipe(open()); | |
gulp.src(path.media + 'views/signup/' + path.dist + '/step0.tpl') | |
.pipe(open()); | |
// Изменяем пути | |
gulp.src(path.media + 'views/layouts/signup/' + path.dist + '/header.tpl') | |
.pipe(replace(path.src, path.dist)) | |
.pipe(gulp.dest(path.media + 'views/layouts/signup/' + path.dist + '/')) | |
.pipe(open()); | |
gulp.src(path.htdocs + 'css/signup/' + path.dist + '/main.php') | |
.pipe(replace(path.src, path.dist)) | |
.pipe(gulp.dest(path.htdocs + 'css/signup/' + path.dist + '/')); | |
gulp.src(path.htdocs + 'js/signup/' + path.dist + '/main.php') | |
.pipe(replace(path.src, path.dist)) | |
.pipe(gulp.dest(path.htdocs + 'js/signup/' + path.dist + '/')); | |
gulp.src('Gruntfile.js') | |
.pipe(replace(path.src, path.dist)) | |
.pipe(gulp.dest('/')); | |
}, 3000) | |
}); | |
// Создаем задачу Sass | |
gulp.task('sass', function(){ | |
return gulp.src(path.htdocs + 'scss/signup/' + path.dist + '/*.scss') | |
.pipe(sass({includePaths: [path.foundation]}).on( 'error', notify.onError({message: "<%= error.message %>", title : "Sass Error!"}))) // перехватываем ошибки | |
.pipe(concat('app.css')) | |
.pipe(autoprefixer(['last 30 versions', '> 1%', 'ie 8', 'ie 7'], { cascade: true })) // Создаем префиксы | |
.pipe(csscomb()) | |
.pipe(cssnano()) // Сжимаем | |
.pipe(gulp.dest(path.htdocs + 'css/signup/' + path.dist + '/')) // Выгружаем результата в папку назначения | |
.pipe(notify({title : "Sass success!"})); | |
}); | |
// Задача js | |
gulp.task('js', function() { | |
return gulp.src(path.htdocs + 'js/signup/' + path.dist + '/scripts.js') | |
.pipe(uglify().on( 'error', notify.onError({message: "<%= error.message %>", title : "JS Error!"}))) // перехватываем ошибки | |
.pipe(rename({suffix: '.min'})) // Минифицируем с добавления суффикса | |
.pipe(gulp.dest(path.htdocs + 'js/signup/' + path.dist + '/')) // Выгружаем результата в папку назначения | |
}); | |
gulp.task('un-img', function () { | |
gulp.src([ | |
path.htdocs + 'images/signup/' + path.dist + '/**/*', | |
path.htdocs + 'css/signup/' + path.dist + '/app.css', | |
path.media + 'views/signup/' + path.dist + '/step0.tpl', | |
path.media + 'views/layouts/signup/' + path.dist + '/header.tpl' | |
]) | |
.pipe(unusedImages({log: true}).on( 'error', notify.onError({message: "<%= error.message %>", title : "Unused images!!!"}))) | |
}); | |
gulp.task('imgmin', ['un-img'], function () { | |
gulp.src(path.htdocs + 'images/signup/' + path.dist + '/**/*') | |
.pipe(image()) | |
.pipe(gulp.dest(path.htdocs + 'images/signup/' + path.dist + '/')); | |
}); | |
gulp.task('watch', function() { | |
gulp.watch(path.htdocs + 'scss/signup/' + path.dist + '/*.scss', ['sass']); | |
gulp.watch(path.htdocs + 'js/signup/' + path.dist + '/scripts.js', ['js']); | |
gulp.watch(path.htdocs + 'images/signup/' + path.dist + '/**/*', ['un-img']); | |
}); | |
// Задача сборки нового проекта | |
gulp.task('newProject', ['create', 'open', 'watch']); | |
// Дефолтная задача gulp | |
gulp.task('default', ['watch']); | |
// gulp.task('clean-sass', function(){ | |
// return gulp.src(path.htdocs + 'scss/signup/' + path.dist + '/_style.scss') | |
// .pipe(uncss({ | |
// html: [path.media + 'views/signup/' + path.dist + '/step0.tpl'] | |
// })) | |
// .pipe(gulp.dest(path.htdocs + 'scss/signup/' + path.dist + '/')) | |
// .pipe(notify({title : "UN-Sass success!"})); | |
// }); | |
// // Задача удаления неиспользуемых классов в css | |
// gulp.task('unused', ['clean-sass'], function(){ | |
// gulp.src(path.htdocs + 'scss/signup/' + path.dist + '/*.scss') | |
// .pipe(sass({includePaths: [path.foundation]}).on( 'error', notify.onError({message: "<%= error.message %>", title : "Sass Error!"}))) | |
// .pipe(concat('app.css')) | |
// .pipe(autoprefixer(['last 30 versions', '> 1%', 'ie 8', 'ie 7'], { cascade: true })) | |
// .pipe(csscomb()) | |
// .pipe(cssnano()) | |
// .pipe(gulp.dest(path.htdocs + 'css/signup/' + path.dist + '/')) | |
// .pipe(notify({title : "Sass success!"})); | |
// }); | |
// Задача удаления проекта - ИСПОЛЬЗОВАТЬ ОСТОРОЖНО!!! | |
gulp.task('kill', function() { | |
del(path.media + 'views/signup/' + path.dist); | |
del(path.media + 'views/layouts/signup/' + path.dist); | |
del(path.htdocs + 'scss/signup/' + path.dist); | |
del(path.htdocs + 'css/signup/' + path.dist); | |
del(path.htdocs + 'js/signup/' + path.dist); | |
del(path.htdocs + 'images/signup/' + path.dist); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "newproject", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Author", | |
"license": "ISC", | |
"dependencies": { | |
"del": "^2.2.0", | |
"gulp": "^3.9.1", | |
"gulp-autoprefixer": "^3.1.0", | |
"gulp-concat": "^2.6.0", | |
"gulp-csscomb": "^3.0.8", | |
"gulp-cssnano": "^2.1.2", | |
"gulp-image": "^2.7.0", | |
"gulp-notify": "^2.2.0", | |
"gulp-open": "^2.0.0", | |
"gulp-rename": "^1.2.2", | |
"gulp-replace": "^0.5.4", | |
"gulp-sass": "^2.3.2", | |
"gulp-uglify": "^2.0.0", | |
"gulp-uncss": "^1.0.6", | |
"gulp-unused-images": "^1.1.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment