-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
190 lines (173 loc) · 5.3 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var gulp = require('gulp'),
src = gulp.src,
dest = gulp.dest,
less = require('gulp-less'),
autoprefixer = require('gulp-autoprefixer'),
jade = require('gulp-jade'),
//debug = require('gulp-debug'),
//jshint = require('gulp-jshint'),
//uglify = require('gulp-uglify'),
//concat = require('gulp-concat'),
contains = require('gulp-contains'),
watch = require('gulp-watch'),
environment = {
prod: {
name: 'prod'
},
beta: {
name: 'beta'
},
dev: {
name: 'dev'
}
},
ignore = [
'!node_modules/**',
'!compiled/**'
],
filesToCompile = function(extension){
return ['**/**.' + extension, '!**/_*.' + extension].concat(ignore);
},
filesNotToCompile = function(extension){
return['**/_*.' + extension].concat(ignore);
},
moduleFiles = function(extension) {
return ['./module/**/*.' + extension];
},
//pageFiles = function(extension) {
// return ['./page/**/*.' + extension];
//},
indexFile = function(extension) {
return ['./index.' + extension, './static.' + extension];
},
helpers = {
getFile: {},
compileImportingFiles: {},
compileless: {},
compilejade: {},
compileSavedFile: {},
applyFileWatcher: {}
};
environment.current = environment.prod;
/* gulp tasks */
/**
* default task, executes other tasks
*/
gulp.task('default', ['watchLess', 'watchJade']);
/**
* watchLess task, watches module-, page- and static less files
* and compiles them and or the files that are importing them
*/
gulp.task('watchLess', function(){
helpers.applyFileWatcher('less');
});
/**
* watchJade task, watches module-, page- and index jade files
* and compiles them and or files that are importing them
*/
gulp.task('watchJade', function(){
helpers.applyFileWatcher('jade');
});
gulp.task('watchJs', function() {
helpers.applyFileWatcher('js');
});
/**
* Helper function, gets the filename and extension from a path string.
* @param {string} path Path string of the file you want to get
* @returns {string} the file + extension string
*/
helpers.getFile = function(path){
return path.split('\\').pop();
};
/**
* Helper function that compiles all files that are importing a specific file.
* Files with '_' prefix don't get compiled!
* @param file the file that should be imported by ohter files so they get compiled
*/
helpers.compileImportingFiles = function(file) {
var extension = file.split('.').pop();
src(filesNotToCompile(extension))
.pipe(contains({
search: file,
onFound: function(string, file, cb){
helpers.compileImportingFiles(helpers.getFile(file.path));
return false;
}
}));
src(filesToCompile(extension))
.pipe(contains({
search: file,
onFound: function(string, file, cb){
helpers['compile' + extension](src(file.path));
return false;
}
}))
};
/**
* Helper function to compile a stream of less files.
* @param {vinyl} stream Vinyl representation of the less files to be compiled
*/
helpers.compileless = function (stream){
stream
.pipe(less({
globalVars: {
'environment' : environment.current
}
}))
.pipe(autoprefixer({
browsers: [
'Android >= 4',
'last 2 Chrome versions',
'last 2 ff versions',
'ie >= 8',
'iOS >= 6',
'last 2 Opera versions',
'Safari >= 5',
'last 2 op_mob versions',
'last 2 op_mini versions',
'last 2 and_chr versions',
'last 2 and_ff versions',
'last 2 ie_mob versions'
]
}))
.pipe(dest('compiled'));
};
/**
* Helper function to compile a stream of jade files
* @param {vinil} stream Vinyl representation of the jade files to be compiled
*/
helpers.compilejade = function(stream){
stream
.pipe(jade({
basedir: __dirname,
pretty: true,
locals: {
'environment': environment.current
}
}))
.pipe(dest('./'))
};
/**
* Helper function to compile a less or jade file that was saved before
* @param {string} path The path of the file that was saved
*/
helpers.compileSavedFile = function(path) {
var file,
extension = (file = path.split('\\').pop()).split('.').pop();
if (file.indexOf('_') === 0){
return;
}
helpers['compile' + extension](src(path));
};
/**
* Helper function to apply a file watcher for files to compile with a certain extension.
* Works for less and jade files.
* Files with '_' prefix are not compiled.
* @param extension The extension that the watcher should listen too
*/
helpers.applyFileWatcher = function(extension) {
watch(moduleFiles(extension).concat(indexFile(extension)), function(event){
helpers.compileImportingFiles(helpers.getFile(event.path));
helpers.compileSavedFile(event.path);
});
};