-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
171 lines (146 loc) · 3.71 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
#!/usr/bin/env node
// Plugin to handle parameters.
var argv = require('yargs')
.alias('t', 'theme')
.default('theme', ['os2display'])
.argv;
// Gulp basic.
var gulp = require('gulp-help')(require('gulp'), {
'afterPrintCallback': function () {
var args = [];
}
});
// Plugins.
var sass = require('gulp-sass');
var sourcemaps = require('gulp-sourcemaps');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var stylelint = require('gulp-stylelint');
/**
* Configuration object.
*/
var configuration = {
// Base theme.
'os2display': {
"sass": {
"paths": ['./source/**/*.scss'],
"srcs": ['./source/*.scss'],
'dest': './source/css'
},
}
};
/**
* Setup task for sass compilation.
*
* @param theme
* Name of the theme to setup the task.
* @param config
* Selected theme configuration object.
*
* @return string
* The name of the new task.
*/
function sassTask(theme, config) {
var taskName = 'sass_' + theme;
// Process SCSS.
gulp.task(taskName, false, function () {
var pipe = gulp.src(config.sass.srcs)
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'compressed',
includePaths: [
'scss/assets/compass-mixins/lib'
]
}).on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(gulp.dest(config.sass.dest));
});
return taskName;
}
/**
* Setup stylelint tasks.
*
* Note: Because stylelint task is async it returns it's gulp process.
*
* @param theme
* Name of the theme to setup the task.
* @param config
* Selected theme configuration object.
*
* @return string
* The name of the new task.
*/
function stylelintTask(theme, config) {
var taskName = 'stylelint_' + theme;
gulp.task(taskName, false, function lintCssTask() {
return gulp.src(config.sass.paths)
.pipe(stylelint({
reporters: [
{formatter: 'string', console: true}
]
}));
});
return taskName;
}
/**
* Watch sass and stylelint tasks.
*
* @param theme
* Name of the theme to setup the task.
* @param config
* Selected theme configuration object.
*
* @return string
* The name of the new task.
*/
function watchTasks(theme, config) {
var taskName = 'watch_' + theme;
gulp.task(taskName, false, function () {
gulp.watch(config.sass.paths, ['sass']);
gulp.watch(config.sass.paths, ['stylelint']);
if (config.hasOwnProperty('js')) {
gulp.watch(config.js.paths, ['eslint']);
}
});
return taskName;
}
/**
* Dynamically setup tasks base on the selected theme.
*
* @param themes
* Theme name as an string array. Used as index in the
* configuration pobject.
*/
function setupTasks(themes) {
// Define task arrays.
var sassTaskNames = [];
var stylelintTaskNames = [];
var jsMinifyTaskNames = [];
var watchTasksNames = [];
// Ensure themes is an array and if not convert it.
if (Object.prototype.toString.call(themes) !== '[object Array]') {
themes = [themes];
}
// Loop over the selected themes.
for (var i in themes) {
var theme = themes[i];
var config = configuration[theme];
// SASS tasks.
sassTaskNames.push(sassTask(theme, config));
// Style-lint tasks.
stylelintTaskNames.push(stylelintTask(theme, config));
// Watch tasks.
watchTasksNames.push(watchTasks(theme, config));
}
// Define tasks.
gulp.task('sass', 'Compile SCSS into CSS', sassTaskNames);
gulp.task('stylelint', 'Use style-lint to check SCSS (using stylelintrc.json rules)', stylelintTaskNames);
gulp.task('watch', 'Watch for changes in files', watchTasksNames);
// Default task;
gulp.task('default', ['sass', 'stylelint']);
}
/**
* ------- Run task's -------
*/
setupTasks(argv.theme);