This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
gulpfile.js
237 lines (223 loc) · 5.74 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
const fabAssemble = require('fabricator-assemble');
const browserSync = require('browser-sync');
const csso = require('gulp-csso');
const del = require('del');
const gulp = require('gulp');
const argv = require('minimist')(process.argv.slice(2));
const log = require('fancy-log');
const gulpif = require('gulp-if');
const imagemin = require('gulp-imagemin');
const prefix = require('gulp-autoprefixer');
const rename = require('gulp-rename');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const webpack = require('webpack');
sass.compiler = require('node-sass');
let server = false;
function reload(done) {
if (server) server.reload();
done();
}
// configuration
const config = {
dev: !!argv.dev,
styles: {
browsers: [
'ie 11',
'edge >= 16',
'chrome >= 70',
'firefox >= 63',
'safari >= 11',
'iOS >= 12',
'ChromeAndroid >= 70',
],
fabricator: {
src: 'src/assets/fabricator/styles/fabricator.scss',
dest: 'dist/assets/fabricator/styles',
watch: 'src/assets/fabricator/styles/**/*.scss',
},
toolkit: {
src: 'src/assets/toolkit/styles/toolkit.scss',
dest: 'dist/assets/toolkit/styles',
watch: 'src/assets/toolkit/styles/**/*.scss',
},
},
scripts: {
fabricator: {
src: './src/assets/fabricator/scripts/fabricator.js',
dest: 'dist/assets/fabricator/scripts',
watch: 'src/assets/fabricator/scripts/**/*',
},
toolkit: {
src: './src/assets/toolkit/scripts/toolkit.js',
dest: 'dist/assets/toolkit/scripts',
watch: 'src/assets/toolkit/scripts/**/*',
},
},
images: {
toolkit: {
src: ['src/assets/toolkit/images/**/*', 'src/favicon.ico'],
dest: 'dist/assets/toolkit/images',
watch: 'src/assets/toolkit/images/**/*',
},
},
templates: {
watch: 'src/**/*.{html,md,json,yml}',
},
dest: 'dist',
};
// clean
const clean = () => del([config.dest]);
// styles
function stylesFabricator() {
return gulp
.src(config.styles.fabricator.src)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(prefix(config.styles.browsers))
.pipe(gulpif(!config.dev, csso()))
.pipe(rename('f.css'))
.pipe(sourcemaps.write())
.pipe(gulp.dest(config.styles.fabricator.dest));
}
function stylesToolkit() {
return gulp
.src(config.styles.toolkit.src)
.pipe(gulpif(config.dev, sourcemaps.init()))
.pipe(
sass({
includePaths: './node_modules',
}).on('error', sass.logError)
)
.pipe(prefix(config.styles.browsers))
.pipe(gulpif(!config.dev, csso()))
.pipe(gulpif(config.dev, sourcemaps.write()))
.pipe(gulp.dest(config.styles.toolkit.dest));
}
const styles = gulp.parallel(stylesFabricator, stylesToolkit);
// scripts
const webpackConfig = require('./webpack.config')(config);
function scripts(done) {
webpack(webpackConfig, (err, stats) => {
if (err) {
log.error(err());
}
const result = stats.toJson();
if (result.errors.length) {
result.errors.forEach(error => {
log.error(error);
});
}
done();
});
}
// images
function imgFavicon() {
return gulp.src('src/favicon.ico').pipe(gulp.dest(config.dest));
}
function imgMinification() {
return gulp
.src(config.images.toolkit.src)
.pipe(imagemin())
.pipe(gulp.dest(config.images.toolkit.dest));
}
const images = gulp.series(imgFavicon, imgMinification);
// assembly
function assembler(done) {
fabAssemble({
logErrors: config.dev,
dest: config.dest,
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();
}
// server
function serve(done) {
server = browserSync.create();
server.init({
server: {
baseDir: config.dest,
},
notify: false,
logPrefix: 'FABRICATOR',
});
done();
}
function watch() {
gulp.watch(
config.templates.watch,
{ interval: 500 },
gulp.series(assembler, reload)
);
gulp.watch(
[config.scripts.fabricator.watch, config.scripts.toolkit.watch],
{ interval: 500 },
gulp.series(scripts, reload)
);
gulp.watch(
config.images.toolkit.watch,
{ interval: 500 },
gulp.series(images, reload)
);
gulp.watch(
[config.styles.fabricator.watch, config.styles.toolkit.watch],
{ interval: 500 },
gulp.series(styles, reload)
);
}
// default build task
let tasks = [clean, styles, scripts, images, assembler];
if (config.dev) tasks = tasks.concat([serve, watch]);
gulp.task('default', gulp.series(tasks));