forked from GoogleChrome/web.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
142 lines (128 loc) · 4.49 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
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const gulp = require('gulp');
const sassTask = require('./gulp-tasks/sass.js');
const mozjpeg = require('imagemin-mozjpeg');
const pngquant = require('imagemin-pngquant');
const imagemin = require('gulp-imagemin');
const rename = require('gulp-rename');
const through2 = require('through2');
const {defaultLocale} = require('./src/site/_data/site');
/* eslint-disable max-len */
const assetTypes =
'jpg,jpeg,png,svg,gif,webp,webm,mp4,mov,ogg,wav,mp3,txt,yaml';
/* eslint-enable max-len */
const isProd = process.env.ELEVENTY_ENV === 'prod';
const compressImagesTransform = (pngQuality, jpegQuality) => {
if (!isProd) {
// This is the identity transform, which does nothing and just passes
// the images through directly.
return through2.obj();
}
return imagemin([
pngquant({quality: [pngQuality, pngQuality]}),
mozjpeg({quality: jpegQuality * 100}),
]);
};
// These are global images used in CSS and base HTML, such as author profiles.
// We don't compress the PNGs here as they are trivially small (site icons).
gulp.task('copy-global-images', () => {
return gulp
.src(['./src/images/**/*'])
.pipe(compressImagesTransform(1.0, 0.8))
.pipe(gulp.dest('./dist/images'));
});
// These are misc top-level assets.
gulp.task('copy-misc', () => {
return gulp.src(['./src/misc/**/*']).pipe(gulp.dest('./dist'));
});
// Images and any other assets in the content directory that should be copied
// over along with the posts themselves.
// Because we use permalinks to strip the parent directories from our posts
// we need to also strip them from the content paths.
gulp.task('copy-content-assets', () => {
return (
gulp
.src([`./src/site/content/**/*.{${assetTypes}}`])
.pipe(compressImagesTransform(0.8, 0.8))
// This makes the images show up in the same spot as the permalinked posts
// they belong to.
.pipe(
rename((assetPath) => {
const defaultLocaleRegExp = new RegExp(`^${defaultLocale}/`);
const parts = assetPath.dirname.split('/');
assetPath.dirname = assetPath.dirname.replace(
defaultLocaleRegExp,
'',
);
// Landing pages should keep their assets.
// e.g. en/vitals, en/about
if (parts.length <= 2) {
return;
}
// Let images pass through if they're special, i.e. part of the
// handbook or newsletter. We don't treat these URLs like the
// rest of the site and they're allowed to nest.
const subdir = parts[1];
const imagePassThroughs = ['handbook', 'newsletter'];
if (imagePassThroughs.includes(subdir)) {
return;
}
// Some assets are nested under directories which aren't part of
// their url. For example, we have /en/blog/some-post/foo.jpg.
// For these assets we need to remove the /blog/ directory so they
// can live at /some-post/foo.jpg since that's what we'll actually
// serve in production.
// e.g. en/blog/foo/bar.jpg -> /foo/bar.jpg
parts.splice(1, 1);
assetPath.dirname = parts.join('/').replace(defaultLocaleRegExp, '');
}),
)
.pipe(gulp.dest('./dist/'))
);
});
gulp.task('copy-fonts', () => {
return gulp.src('src/fonts/**/*').pipe(gulp.dest('dist/fonts/'));
});
gulp.task('sass', sassTask);
gulp.task(
'build',
gulp.parallel(
'copy-global-images',
'copy-misc',
'copy-content-assets',
'copy-fonts',
sassTask,
),
);
gulp.task('watch', () => {
gulp.watch(
'./src/images/**/*',
{ignoreInitial: true},
gulp.series('copy-global-images'),
);
gulp.watch(
'./src/misc/**/*',
{ignoreInitial: true},
gulp.series('copy-misc'),
);
gulp.watch(
'./src/site/content/**/*',
{ignoreInitial: true},
gulp.series('copy-content-assets'),
);
gulp.watch('./src/styles/**/*.scss', {ignoreInitial: true}, sassTask);
});