-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.ts
51 lines (49 loc) · 1.42 KB
/
sitemap.ts
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
import { SitemapStream, streamToPromise } from 'sitemap'
import fs from 'fs/promises'
import { Readable } from 'stream'
type Options = {
outDir: string
hostname: string
links: Array<{
url: string
links?: Array<{
lang: string
url: string
}>
}>
}
export const sitemap = (options: Partial<Options> = {}) => {
return {
name: 'sitemap',
apply: 'build',
closeBundle() {
return streamToPromise(Readable.from(
options.links.map((link) => {
return {
...link, url: options.hostname + link.url
}
})
).pipe(new SitemapStream({ hostname: options.hostname })))
.then((data) =>
fs.writeFile(options.outDir + '/sitemap.xml', data.toString(), { encoding: 'utf-8' })
).then(() =>
fs.writeFile(options.outDir + '/robots.txt', `User-agent: *\nSitemap: https://${options.hostname}/robots.txt`, { encoding: 'utf-8' })
).then(() => void console.log('sitemap.xml and robots.txt generated'))
.catch((error) => { throw new Error(`sitemap/robots could not be generated`)})
},
transformIndexHtml() {
return [
{
tag: 'link',
injectTo: 'head' as 'head' | 'body' | 'head-prepend' | 'body-prepend' | undefined,
attrs: {
rel: 'sitemap',
type: 'application/xml',
title: 'Sitemap',
href: '/sitemap.xml',
},
},
]
}
}
}