-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
125 lines (103 loc) · 3.23 KB
/
gulpfile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const gulp = require('gulp');
gutil = require('gulp-util');
cssmin = require('gulp-cssmin');
postcss = require('gulp-postcss');
sass = require('gulp-sass');
plumber = require('gulp-plumber');
sourcemaps = require('gulp-sourcemaps');
clone = require('gulp-clone');
livereload = require('gulp-livereload');
concat = require('gulp-concat');
uglify = require('gulp-uglify');
jshint = require('gulp-jshint');
rm = require('gulp-rimraf');
rename = require('gulp-rename');
watch = require('gulp-watch');
const autoprefixer = require('autoprefixer');
const merge = require('merge-stream');
gulp.task('default', ['build']);
// Style config
gulp.task('scss:style', function () {
return scssTask('style');
});
gulp.task('scss', groupTasks('scss:'));
// Script config
gulp.task('scripts:main', function() {
return scriptTask('main');
});
gulp.task('scripts', groupTasks('scripts:'));
gulp.task('clean', function() {
return gulp.src(['dist']).pipe(rm());
});
gulp.task('build', ['clean'], function(){
gulp.start(['scripts', 'scss']);
});
gulp.task('watch', function () {
gulp.watch(srcFiles.stylesheets.style.watch, ['scss']);
gulp.watch(srcFiles.scripts.main.watch, ['scripts']);
});
let srcFiles = {
scripts : {
main : {
files: ['assets/js/**/*.js'],
watch: ['assets/js/**/*.js'],
dest: 'dist/js',
skipLint: false
}
},
stylesheets: {
style : {
files: ['assets/scss/global.scss'],
watch: ['assets/scss/**/*.scss'],
dest: 'dist/css'
}
},
assets: {
images : ['assets/images/**/*'],
}
};
function err(error) {
let displayError = gutil.colors.red(error.message);
gutil.log(displayError);
gutil.beep();
this.emit('end');
}
function scssTask(name) {
let dest = srcFiles.stylesheets[name].dest;
let source = gulp.src(srcFiles.stylesheets[name].files)
.pipe(plumber())
.pipe(sourcemaps.init())
.pipe(sass({ outputStyle: 'expanded' })).on('error', err)
.pipe(postcss([
autoprefixer({browsers: ['last 2 versions']}),
]));
let pipeMinify = source.pipe(clone())
.pipe(cssmin())
.pipe(rename({suffix: '.min'}))
.pipe(sourcemaps.write('.', { sourceRoot: null }))
.pipe(gulp.dest(dest));
return merge(pipeMinify).pipe(livereload());
}
function scriptTask(name) {
let dest = srcFiles.scripts[name].dest;
let filesSource = gulp.src(srcFiles.scripts[name].files);
if (!srcFiles.scripts[name].skipLint) {
filesSource = filesSource.pipe(jshint()).pipe(jshint.reporter('jshint-stylish'));
}
filesSource = filesSource.pipe(sourcemaps.init())
.pipe(concat(name + '.js'));
let pipeUglify = filesSource.pipe(clone())
.pipe(rename({
basename: name,
suffix: '.min'
}))
.pipe(uglify()).on('error', err)
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest(dest));
return merge(pipeUglify).pipe(livereload());
}
function groupTasks(taskName) {
return Object.keys(gulp.tasks).filter(function(task){
return task.indexOf(taskName) !== -1;
});
}