-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
228 lines (213 loc) · 5.67 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
const assemble = require('fabricator-assemble');
const autoprefixer = require('gulp-autoprefixer');
const browserSync = require('browser-sync');
const cssnano = require('gulp-cssnano');
const del = require('del');
const gulp = require('gulp');
const gulpif = require('gulp-if');
const gutil = require('gulp-util');
const imagemin = require('gulp-imagemin');
const notifier = require('node-notifier');
const runSequence = require('run-sequence');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const webpack = require('webpack');
const reload = browserSync.reload;
// configuration
const config = {
templates: {
src: ['src/templates/**/*', '!src/templates/layouts/**'],
dest: 'dist',
watch: ['src/templates/**/*', 'src/data/**/*.json'],
layouts: 'src/templates/layouts/*',
partials: ['src/templates/components/**/*'],
data: 'src/data/**/*.{json,yml}',
},
scripts: {
src: './src/assets/scripts/main.js',
dest: 'dist/assets/scripts',
watch: 'src/assets/scripts/**/*',
},
styles: {
src: 'src/assets/styles/main.scss',
dest: 'dist/assets/styles',
watch: 'src/assets/styles/**/*',
browsers: ['last 1 version'],
},
images: {
src: 'src/assets/images/**/*',
dest: 'dist/assets/images',
watch: 'src/assets/images/**/*',
},
fonts: {
src: 'src/assets/fonts/**/*',
dest: 'dist/assets/fonts',
watch: 'src/assets/fonts/**/*',
},
data: {
src: 'src/data/**/*',
watch: 'src/data/**/*',
},
dev: gutil.env.dev,
};
// clean
gulp.task('clean', del.bind(null, ['dist']));
// postclean
gulp.task('postclean', del.bind(null, ['dist/components']));
// templates
gulp.task('templates', (done) => {
assemble({
layouts: config.templates.layouts,
views: config.templates.src,
materials: config.templates.partials,
data: config.templates.data,
keys: {
views: 'templates',
materials: 'components',
},
dest: config.templates.dest,
logErrors: config.dev,
helpers: {
// {{ default description "string of content used if description var is undefined" }}
default: function defaultFn(...args) {
return args.find(value => !!value);
},
// {{ concat str1 "string 2" }}
concat: function concat(...args) {
return args.slice(0, args.length - 1).join('');
},
// {{> (dynamicPartial name) }} ---- name = 'nameOfComponent'
dynamicPartial: function dynamicPartial(name) {
return name;
},
eq: function eq(v1, v2) {
return v1 === v2;
},
ne: function ne(v1, v2) {
return v1 !== v2;
},
and: function and(v1, v2) {
return v1 && v2;
},
or: function or(v1, v2) {
return v1 || v2;
},
not: function not(v1) {
return !v1;
},
gte: function gte(a, b) {
return +a >= +b;
},
lte: function lte(a, b) {
return +a <= +b;
},
plus: function plus(a, b) {
return +a + +b;
},
minus: function minus(a, b) {
return +a - +b;
},
divide: function divide(a, b) {
return +a / +b;
},
multiply: function multiply(a, b) {
return +a * +b;
},
abs: function abs(a) {
return Math.abs(a);
},
mod: function mod(a, b) {
return +a % +b;
},
},
});
done();
});
// scripts
const webpackConfig = require('./webpack.config')(config);
gulp.task('scripts', (done) => {
webpack(webpackConfig, (err, stats) => {
if (err) {
gutil.log(gutil.colors.red(err()));
}
const result = stats.toJson();
if (result.errors.length) {
result.errors.forEach((error) => {
gutil.log(gutil.colors.red(error));
notifier.notify({
title: 'JS Build Error',
message: error,
});
});
}
done();
});
});
// SASS styles
gulp.task('styles', () =>
gulp.src(config.styles.src)
.pipe(sourcemaps.init())
.pipe(sass({
includePaths: './node_modules',
}).on('error', sass.logError))
.pipe(autoprefixer({
browsers: config.styles.browsers,
}))
.pipe(gulpif(!config.dev, cssnano({ autoprefixer: false })))
.pipe(sourcemaps.write())
.pipe(gulp.dest(config.styles.dest))
.pipe(gulpif(config.dev, reload({ stream: true }))));
// images
gulp.task('images', () =>
gulp.src(config.images.src)
.pipe(imagemin({
progressive: true,
interlaced: true,
}))
.pipe(gulp.dest(config.images.dest)));
// fonts
gulp.task('fonts', () =>
gulp.src(config.fonts.src).pipe(gulp.dest(config.fonts.dest)));
// data
gulp.task('data', () =>
gulp.src(config.data.src));
// server
gulp.task('serve', () => {
browserSync({
server: {
baseDir: config.templates.dest,
},
notify: false,
logPrefix: 'BrowserSync',
});
gulp.task('templates:watch', ['templates'], reload);
gulp.watch(config.templates.watch, ['templates:watch']);
gulp.task('styles:watch', ['styles']);
gulp.watch(config.styles.watch, ['styles:watch']);
gulp.task('scripts:watch', ['scripts'], reload);
gulp.watch(config.scripts.watch, ['scripts:watch']);
gulp.task('images:watch', ['images'], reload);
gulp.watch(config.images.watch, ['images:watch']);
gulp.task('fonts:watch', ['fonts'], reload);
gulp.watch(config.fonts.watch, ['fonts:watch']);
gulp.task('data:watch', ['templates'], reload);
gulp.watch(config.data.watch, ['data:watch']);
});
// default build task
gulp.task('default', ['clean'], () => {
const tasks = [
'templates',
'scripts',
'styles',
'images',
'fonts',
'data',
'postclean',
];
// run build
runSequence(tasks, () => {
if (config.dev) {
gulp.start('serve');
}
});
});