-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
384 lines (351 loc) · 9.22 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import fs from 'node:fs';
import gulp from 'gulp';
const { series, parallel, task } = gulp;
import gulpTwig from 'gulp-twig';
import gulpSass from 'gulp-sass';
import nodeSass from 'node-sass';
import browserSync from 'browser-sync';
import sourcemaps from 'gulp-sourcemaps';
import plumber from 'gulp-plumber';
import data from 'gulp-data';
import notify from 'gulp-notify';
import prettify from 'gulp-prettify';
import imagemin, { mozjpeg, optipng, svgo } from 'gulp-imagemin';
import replace from 'gulp-replace';
import yaml from 'js-yaml';
import { deleteAsync } from 'del';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import gulpIf from 'gulp-if';
import size from 'gulp-size';
import rename from 'gulp-rename';
import postcss from 'gulp-postcss';
import autoprefixer from 'autoprefixer';
import mqPacker from 'css-mqpacker';
import sortCSSMq from 'sort-css-media-queries';
import cssnano from 'cssnano';
import _ from 'lodash';
import dotenv from 'dotenv';
import svgSprite from 'gulp-svg-sprite';
import gulpSvgo from 'gulp-svgo';
import dom from 'gulp-dom';
import gulpStylelint from '@ronilaukkarinen/gulp-stylelint';
import webpack from 'webpack-stream';
import config from './webpack.config.js';
const args = yargs(hideBin(process.argv))
const sass = gulpSass(nodeSass);
dotenv.config();
const PROD = process.env.NODE_ENV === 'production';
const TEST = process.env.CI;
const THEME_DIR = process.env.THEME_DIR || args.argv.themeDir || '';
const paths = {
html: {
src: ['./src/templates/*.twig', '!./src/templates/*layout.twig'],
dest: './dist'
},
styles: {
src: './src/scss/**/*.scss',
dest: './dist/css/'
},
scripts: {
src: './src/js/index.ts',
dest: './dist/js/'
},
images: {
// ignore sub folders in production build since demo images may be too big.
src: `./src/images/${PROD ? '*' : '**/*'}.{png,jpg,svg}`,
dest: './dist/images/'
}
};
/**
* Change output paths if --themeDir is passed.
*
* This is so frontend can be iterated on and continuously ouput
* to a Drupal theme (helpful for frontend changes on dev server).
*
* Use `gulp dev` in conjunction with this so browser sync server is not started.
*/
if (THEME_DIR !== '') {
console.info('outputing into', THEME_DIR);
paths.styles.dest = THEME_DIR + '/css/';
paths.scripts.dest = THEME_DIR + '/js/';
paths.images.dest = THEME_DIR + '/images/';
}
const onError = function (err) {
notify.onError({
title: 'Gulp error in ' + err.plugin,
message: err.toString()
})(err);
this.emit('end');
};
const clean = () =>
deleteAsync([
'./dist/**/*.html',
'./dist/**/*.js',
'./dist/**/*.css',
'./dist/images/*'
]);
const serve = () =>
browserSync.init({
files: './dist/**/*',
notify: false,
server: {
baseDir: './dist'
},
directory: true,
ghostMode: false
});
const copyIcons = () =>
gulp
.src('./dist/icons/sprites/symbol/svg/sprite.symbol.svg')
.pipe(rename('icons.twig'))
.pipe(gulp.dest('./src/templates/partials'));
const lintStyles = () => {
return gulp.src(paths.styles.src).pipe(
gulpStylelint({
failAfterError: false,
reporters: [{ formatter: 'string', console: true }]
})
);
};
const styles = () => {
const plugins = [autoprefixer()];
if (PROD) {
plugins.push(
cssnano(),
mqPacker({
sort: sortCSSMq
})
);
}
return gulp
.src(paths.styles.src)
.pipe(gulpIf(!PROD, sourcemaps.init({ loadMaps: true })))
.pipe(
plumber({
errorHandler: onError
})
)
.pipe(sass())
.pipe(postcss(plugins))
.pipe(gulpIf(!PROD, sourcemaps.write('./')))
.pipe(size({ showFiles: true }))
.pipe(gulp.dest(paths.styles.dest));
};
const scripts = () =>
gulp
.src(paths.scripts.src)
.pipe(webpack(config))
.on('error', function handleError() {
// we want the build to fail in production builds due to TS errors...
if (PROD) return;
// ...but recover from errors in development.
// https://github.com/shama/webpack-stream/issues/34#issuecomment-171644957
this.emit('end');
})
.pipe(gulp.dest(paths.scripts.dest));
const html = () =>
gulp
.src(paths.html.src)
.pipe(
plumber({
errorHandler: onError
})
)
.pipe(
data(function () {
const ymlData = yaml.load(
fs.readFileSync('./src/data/data.yml', 'utf8')
);
const imageStyles = yaml.load(
fs.readFileSync('./src/data/image_styles.yml', 'utf8')
);
return {
...ymlData,
...imageStyles,
env: {
test: TEST,
production: PROD,
// store if this is vercel, so we can change templates for deploy previews
vercel: Boolean(process.env.VERCEL_URL)
}
};
})
)
.pipe(
gulpTwig({
base: './src/templates',
filters: [
{
name: 'exists',
func: (value, args) => {
if (!value) {
console.log(args);
throw new Error('value is falsy');
}
return value;
}
},
{
name: 'groupBy',
func: (items, field) => {
const grouped = _.groupBy(items, field[0]);
const groupArr = Object.keys(grouped).map((key) => ({
group: key,
items: grouped[key]
}));
return groupArr;
}
},
{
name: 'cast_to_array',
func: (items) => {
return Object.keys(items).map((key) => {
return {
...items[key],
key
};
});
}
}
]
})
)
.pipe(prettify())
.pipe(gulp.dest(paths.html.dest));
const images = () =>
gulp
.src(paths.images.src, { encoding: false })
.pipe(
imagemin([
mozjpeg({ progressive: true }),
optipng({ optimizationLevel: 5 }),
svgo({ cleanupIDs: false })
])
)
.pipe(gulp.dest(paths.images.dest));
const replaceImagePaths = () => {
const imagesDir = args.argv.imagesDir || '/images/';
return gulp
.src('./dist/css/*.css')
.pipe(replace('/images/', imagesDir))
.pipe(gulp.dest('./dist/css'));
};
const copyDeps = () => {
// NOTE: Chart.bundle.min.js includes Momentjs but so far we are not using time axis
// http://www.chartjs.org/docs/latest/getting-started/installation.html#bundled-build
return gulp
.src([
'./node_modules/chart.js/dist/Chart.min.js',
'./node_modules/iframe-resizer/js/iframeResizer.min.js',
'./node_modules/iframe-resizer/js/iframeResizer.contentWindow.min.js'
])
.pipe(gulp.dest('./dist/js'));
};
const copyMeta = () => {
return gulp.src(['./composer.json']).pipe(gulp.dest('./dist/'));
};
const deployDist = () => {
if (!THEME_DIR) {
return console.error('No `--themeDir` argument passed');
}
return gulp
.src(
[
'./dist/css/main.css',
'./dist/js/*',
'./dist/images/*'
],
{
base: './dist'
}
)
.pipe(gulp.dest(THEME_DIR));
};
const watch = () => {
gulp.watch('./src/templates/**/*.twig', html);
gulp.watch(paths.styles.src, gulp.parallel(styles, lintStyles));
gulp.watch(paths.images.src, images);
gulp.watch('./src/data/*.yml', html);
};
const reportFilesizes = () =>
gulp
.src('./dist/**/*.{css,js}')
.pipe(
size({
showFiles: true,
showTotal: false
})
)
.pipe(
size({
showFiles: true,
brotli: true,
showTotal: false
})
);
const minifySvgs = (src) =>
gulp
.src(src)
.pipe(
dom(function () {
const svg = this.querySelector('svg');
svg.setAttribute('fill-rule', 'evenodd');
return this.querySelector('body').innerHTML;
}, false)
)
.pipe(
gulpSvgo({
plugins: [
{ removeTitle: true },
{ removeXMLNS: true },
{ removeAttrs: { attrs: '(stroke)' } }
]
})
);
// clean up and minify svgs
const cleanAndCopyIcons = () =>
minifySvgs('./src/icons/*.svg').pipe(gulp.dest('./dist/icons/svg'));
// create svg sprite
const buildIconSprite = () =>
minifySvgs('./src/icons/*.svg')
.pipe(
svgSprite({
svg: {
xmlDeclaration: false
},
shape: {
id: {
generator: 'icon-%s'
}
},
mode: {
symbol: {
// Activate the defs mode
bust: false, // Cache busting
example: true // Build a page
}
}
})
)
.pipe(gulp.dest('./dist/icons/sprites'));
task('scripts', scripts);
task('styles', styles);
task('cleanAndCopyIcons', cleanAndCopyIcons);
task('replaceImagePaths', replaceImagePaths);
task('lintStyles', lintStyles);
task('copyDeps', copyDeps);
task(
'build',
series(
clean,
copyDeps,
copyMeta,
parallel(html, images, lintStyles, styles, scripts),
reportFilesizes
)
);
task('icons', series(buildIconSprite, copyIcons));
task('dev', parallel('build', watch, serve));
task('devSaw', parallel('build', watch));
task('deploy', series(replaceImagePaths, deployDist));