forked from frismedia/gulp-fritz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
147 lines (125 loc) · 4.99 KB
/
index.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict';
var gulp = require('gulp');
var shell = require('gulp-shell');
var jshint = require('gulp-jshint');
var sass = require('gulp-sass');
var concat = require('gulp-concat');
var cleanCSS = require('gulp-clean-css');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var argv = require('yargs').argv;
var _ = require('underscore');
var sourcemaps = require('gulp-sourcemaps');
var gutil = require('gulp-util');
var spritesmith = require('gulp.spritesmith');
//var critical = require('critical');
var imagemin = require('gulp-imagemin');
var pngquant = require('imagemin-pngquant'); // $ npm i -D imagemin-pngquant
// file can be a vinyl file object or a string
// when a string it will construct a new one
module.exports = function(config) {
var tasks = [];
_.each(config, function(value, key) {
tasks.push(key);
gulp.task(key, function() {
// Sprite css-files are automatically appended to the app's css file
var files = value.files,
filename = (value.filename ? value.filename : 'app');
if ('sass' === value.type) {
if ('undefined' !== typeof value.images) {
_.each(value.images, function(path, key) {
gutil.log('Creating sprite: ' + value.dest + '/' + key + '.png');
var spriteData = gulp.src(path).pipe(spritesmith({
imgName: key + '.png',
cssName: key + '.css'
}));
spriteData.pipe(gulp.dest(value.dest));
files.push(value.dest + '/' + key + '.css');
});
}
}
var task = gulp.src(files);
if ('imagemin' === value.type) {
gulp.src(value.files)
.pipe(imagemin({
progressive: true,
use: [pngquant()]
}))
.pipe(gulp.dest(value.dest));
}
else if ('scripts' === value.type) {
if (argv.production) {
gutil.log('minifying scripts for production.. This could take a while');
task = task.pipe(concat(filename+'.min.js'));
task = task.pipe(uglify());
}
else {
task = task.pipe(concat(filename+'.js'));
}
task = task.pipe(gulp.dest(value.dest));
}
else if ('sass' === value.type) {
task = task.pipe(sourcemaps.init());
task = task.pipe(sass({
sourceComments: 'map',
outputStyle: 'nested',
includePaths: ('undefined' === typeof value.includePaths) ? [] : value.includePaths
}).on('error', sass.logError));
if (argv.production) {
gutil.log('minifying styles for production');
task = task.pipe(concat(filename+'.min.css'));
task = task.pipe(cleanCSS());
task = task.pipe(sourcemaps.write('/'));
}
else {
task = task.pipe(concat(filename+'.css'));
task = task.pipe(sourcemaps.write('/'));
}
task = task.pipe(gulp.dest(value.dest));
}
else if ('copy' === value.type) {
task = task.pipe(gulp.dest(value.dest));
}
if ('undefined' !== typeof value.copy) {
_.each(value.copy, function(path, name) {
gulp.src([path]).pipe(gulp.dest(value.dest));
});
}
if (argv.watch) {
var watch = value.files;
if ('undefined' !== typeof value.watch) {
watch = watch.concat(value.watch);
}
gulp.watch(watch, [key]);
}
return task;
});
});
gulp.task('watch', function() {
if (argv.task) {
var tasks = argv.task.split(','),
files = [];
for (var i = 0; i < tasks.length; i++) {
files = config[tasks[i]].files;
if ('undefined' !== typeof config[tasks[i]].watch) {
files = files.concat(config[tasks[i]].watch);
}
gulp.watch(files, [tasks[i]]);
gulp.start(tasks[i]);
}
}
});
gulp.task('default', function() {
var files = [];
for (var i = 0; i < tasks.length; i++) {
files = config[tasks[i]].files;
if ('undefined' !== typeof config[tasks[i]].watch) {
files = files.concat(config[tasks[i]].watch);
}
if (argv.watch) {
gulp.watch(files, [tasks[i]]);
}
gulp.start(tasks[i]);
}
});
};