-
Notifications
You must be signed in to change notification settings - Fork 64
/
gulpfile.js
184 lines (155 loc) · 4.24 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
const path = require('path')
const fs = require('fs')
const fsp = require('fs/promises')
const { Transform } = require('stream')
const { pipeline } = require('stream/promises')
const gulp = require('gulp')
const git = require('gulp-git')
const shell = require('gulp-shell')
const postcss = require('gulp-postcss')
const csso = require('postcss-csso')
const pimport = require('postcss-import')
const minmax = require('postcss-media-minmax')
const autoprefixer = require('autoprefixer')
const esbuild = require('gulp-esbuild')
const del = require('del')
const rev = require('gulp-rev')
const revRewrite = require('gulp-rev-rewrite')
const puppeteer = require('puppeteer')
const { contentRepGithub, contentRepFolders } = require(path.join(__dirname, 'config/constants'))
const cloneContent = () => git.clone(contentRepGithub)
const makeLinks = shell.task(`node make-links.js --default`, {
env: {
PATH_TO_CONTENT: path.join(__dirname, 'content'),
PATH: process.env.PATH,
},
})
// Styles
const styles = () => {
return gulp
.src('src/styles/{index.css,index.sc.css,dark-theme.css}')
.pipe(
postcss([
pimport,
minmax,
autoprefixer,
csso({
restructure: false,
}),
]),
)
.pipe(gulp.dest('dist/styles'))
}
// Scripts
const sw = () => {
return gulp
.src('src/sw.js')
.pipe(
esbuild({
target: 'es2015',
minify: true,
}),
)
.pipe(gulp.dest('dist/'))
}
const scripts = () => {
return gulp
.src('src/scripts/index.js')
.pipe(
esbuild({
target: 'es2015',
bundle: true,
minify: true,
plugins: [
{
name: 'node-modules-resolution',
setup(build) {
build.onResolve({ filter: /^\// }, (args) => {
const cwd = process.cwd()
const newPath = args.path.includes(cwd) ? args.path : path.join(cwd, 'node_modules', args.path)
return {
path: newPath,
}
})
},
},
],
}),
)
.pipe(gulp.dest('dist/scripts'))
}
// Clean
const clean = () => {
return del(['dist/styles', 'dist/scripts', 'dist/sw.js'])
}
// Cache
const cacheHash = () => {
return gulp
.src('dist/**/*.{css,js}')
.pipe(rev())
.pipe(gulp.dest('dist'))
.pipe(rev.manifest('rev-manifset.json'))
.pipe(gulp.dest('dist'))
}
const cacheReplace = () => {
return gulp
.src('dist/**/*.{html,css,svg}')
.pipe(
revRewrite({
manifest: fs.readFileSync('dist/rev-manifset.json'),
}),
)
.pipe(gulp.dest('dist'))
}
const cache = gulp.series(cacheHash, cacheReplace)
exports.setupContent = gulp.series(cloneContent, makeLinks)
exports.dropContent = () => del(['content', ...contentRepFolders.map((folder) => `src/${folder}`)])
// Social cards
const socialCards = async () => {
const browser = await puppeteer.launch({ headless: 'new' })
const page = await browser.newPage()
return pipeline(
gulp.src('dist/{a11y,css,html,js,tools,recipes}/**/index.sc.html'),
new Transform({
objectMode: true,
async transform(file, encoding, done) {
const imagePath = file.path.replace('index.sc.html', 'images/covers/')
if (!fs.existsSync(imagePath)) {
await fsp.mkdir(imagePath, { recursive: true })
}
await page.goto('file://' + file.path)
await page.evaluate(() => {
const image = document.querySelector('.social-card__image')
if (image) {
image.setAttribute('src', image.src.replace(/.*images\//, 'images/'))
}
})
await page.setViewport({
width: 503,
height: 273,
deviceScaleFactor: 1,
})
await page.screenshot({
path: path.join(imagePath, 'og.png'),
type: 'png',
clip: {
x: 0,
y: 0,
width: 503,
height: 273,
},
})
done()
},
}),
)
.catch(console.error)
.finally(async () => {
await page.close()
await browser.close()
})
}
// Build social cards
exports.socialCards = socialCards
// Default
exports.default = gulp.series(clean, styles, scripts, sw, cache)