-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
302 lines (263 loc) · 7.57 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env node
// Plugin to handle parameters.
var argv = require('yargs')
.alias('s', 'sync')
.alias('t', 'theme')
.alias('d', 'domain')
.alias('p', 'production')
.default('sync', false)
.default('theme', ['grundsalg-design'])
.default('domain', 'grundsalg-design.vm')
.default('production', false)
.argv;
// Gulp basic.
var chalk = require('chalk');
var gulpif = require('gulp-if');
var gulp = require('gulp-help')(require('gulp'), {
'afterPrintCallback' : function() {
var args = [];
console.log(chalk.underline('Global parameters'));
args.push('');
args.push(chalk.cyan('--theme (-t)'));
args.push('Limit the theme(s) that should be used. Use one --theme for each theme you want' + '\n');
args.push(chalk.cyan('--sync (-s)'));
args.push('Enabled browser-sync' + '\n');
args.push(chalk.cyan('--domain (-d)'));
args.push('Domain to use with browser-sync' + '\n\n');
args.push(chalk.cyan('--production (-p)'));
args.push('Production compile (remove sourcemaps etc.)' + '\n\n');
console.log.apply(console, args);
console.log(chalk.underline('Usage examples:'));
console.log(chalk.cyan(' *') + chalk.green(' gulp watch --theme base --theme blue --domain test.grundsalg-design.vm --sync'));
console.log(chalk.cyan(' *') + chalk.green(' gulp watch --sync'));
console.log(chalk.cyan(' *') + chalk.green(' gulp sass -t base'));
console.log('\n' + 'It will always default to use all themes' + '\n');
}
});
// Gulp 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');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');
// ESLint stuff.
var eslint = require('eslint/lib/cli');
var globby = require('globby');
// Browser-sync needs to be a globe variable.
var browserSync;
/**
* Configuration object.
*/
var configuration = {
// Base theme.
'grundsalg-design': {
"js": {
"paths": ['./js/*.js', '!./js/*.min.*'],
"dest": './js'
},
"sass": {
"paths": './scss/**/*.scss',
'dest': './css'
},
"twig": {
"paths": './templates/**/*.twig'
}
}
};
/**
* 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 () {
// Configure auto-prefixer.
var processors = [
autoprefixer({browsers: ['last 2 versions']})
];
var pipe = gulp.src(config.sass.paths)
.pipe(sourcemaps.init())
.pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError))
.pipe(postcss(processors))
.pipe(gulpif(!argv.production, sourcemaps.write()))
.pipe(gulp.dest(config.sass.dest));
// It's unknow why gulp-if don't work with browser-sync, so this if
// statement is as little hack.
if (argv.sync) {
pipe.pipe(browserSync.stream());
}
});
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']);
}
if (argv.sync && config.hasOwnProperty('twig')) {
gulp.watch(config.twig.paths).on('change', browserSync.reload);
}
});
return taskName;
}
/**
* ESLint tasks (JavaScript)
*
* Note: We don't use gulp-eslint as that package is out-dated.
*
* @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 ESLintTasks(theme, config) {
var taskName = 'eslint_' + theme;
gulp.task(taskName, false, function(done) {
globby(config.js.paths).then(
function(paths) {
// Additional CLI options can be added here.
var code = eslint.execute(paths.join(' '));
done();
},
function(err) {
// Unexpected failure, include stack.
done(err);
}
);
});
return taskName;
}
/**
* Minify JavaScript.
*
* @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 minifyJSTasks(theme, config) {
var taskName = 'eslint_' + theme;
gulp.task(taskName, false, function () {
gulp.src(config.js.paths)
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(gulpif(!argv.production, sourcemaps.write('/maps')))
.pipe(rename({extname: ".min.js"}))
.pipe(gulp.dest(config.js.dest));
}
);
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) {
// Start browser sync.
if (argv.sync) {
browserSync = require('browser-sync').create();
browserSync.init({
proxy: argv.domain,
host: argv.domain
});
}
// Define task arrays.
var sassTaskNames = [];
var stylelintTaskNames = [];
var eslintTaskNames = [];
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));
// JavaScript tasks.
if (config.hasOwnProperty('js')) {
// Lint JavaScript.
eslintTaskNames.push(ESLintTasks(theme, config));
// Minify JavaScript
jsMinifyTaskNames.push(minifyJSTasks(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('eslint', 'Lint JavaScript with ESLint (using .eslintrc rules)', eslintTaskNames);
gulp.task('jsMinify', 'Minify JavaScript files', jsMinifyTaskNames);
gulp.task('watch', 'Watch for changes in files', watchTasksNames);
// Default task;
gulp.task('default', ['sass', 'jsMinify', 'stylelint', 'eslint']);
}
/**
* ------- Run task's -------
*/
setupTasks(argv.theme);