@@ -12,6 +12,7 @@ import { globby } from 'globby'
12
12
import fs from 'node:fs/promises'
13
13
import path from 'node:path'
14
14
import { fileURLToPath } from 'node:url'
15
+ import pupa from 'pupa'
15
16
import { SitemapStream , streamToPromise } from 'sitemap'
16
17
import tsconfigpaths from 'vite-tsconfig-paths'
17
18
import {
@@ -59,32 +60,34 @@ const index: string = pkg.name.replace(/.+\//, '')
59
60
const config : UserConfig = defineConfig ( {
60
61
appearance : 'dark' ,
61
62
/**
62
- * Performs post-build tasks.
63
+ * Performs postbuild tasks.
63
64
*
64
65
* This includes:
65
66
*
66
- * - Building and writing `sitemap.xml`
67
+ * 1. Writing `sitemap.xml` to `config.outDir`
68
+ * 2. Writing `robots.txt` to `config.outDir`
67
69
*
68
70
* @async
69
71
*
70
72
* @param {SiteConfig } config - Site configuration
71
73
* @param {string } config.outDir - Absolute path to output directory
74
+ * @param {string } config.root - Absolute path to project directory
72
75
* @return {Promise<void> } Nothing when complete
73
76
*/
74
- async buildEnd ( { outDir } : SiteConfig ) : Promise < void > {
77
+ async buildEnd ( { outDir, root } : SiteConfig ) : Promise < void > {
75
78
/**
76
- * Sitemap stream .
79
+ * Sitemap routes .
77
80
*
78
- * @var {SitemapStream} stream
81
+ * @const {[string, CheerioAPI][]} routes
79
82
*/
80
- let stream : SitemapStream = new SitemapStream ( { hostname : HOSTNAME } )
83
+ const routes : [ string , CheerioAPI ] [ ] = [ ]
81
84
82
85
/**
83
- * Sitemap routes .
86
+ * Sitemap stream .
84
87
*
85
- * @const {[string, CheerioAPI][]} routes
88
+ * @const {SitemapStream} stream
86
89
*/
87
- const routes : [ string , CheerioAPI ] [ ] = [ ]
90
+ const stream : SitemapStream = new SitemapStream ( { hostname : HOSTNAME } )
88
91
89
92
// get sitemap routes
90
93
for ( const route of await globby ( '**.html' , { cwd : outDir } ) ) {
@@ -105,11 +108,24 @@ const config: UserConfig = defineConfig({
105
108
} )
106
109
}
107
110
108
- // end stream
109
- stream = stream . end ( )
110
-
111
111
// write sitemap.xml
112
- await fs . writeFile ( `${ outDir } /sitemap.xml` , await streamToPromise ( stream ) )
112
+ await fs . writeFile (
113
+ path . resolve ( outDir , 'sitemap.xml' ) ,
114
+ await streamToPromise ( stream . end ( ) )
115
+ )
116
+
117
+ /**
118
+ * `robots.txt` template file path.
119
+ *
120
+ * @const {string} robots
121
+ */
122
+ const robots : string = path . resolve ( root , '.vitepress/templates/robots.txt' )
123
+
124
+ // write robots.txt
125
+ await fs . writeFile (
126
+ path . resolve ( outDir , 'robots.txt' ) ,
127
+ pupa ( await fs . readFile ( robots , 'utf8' ) , { HOSTNAME } )
128
+ )
113
129
114
130
return void 0
115
131
} ,
0 commit comments