-
Notifications
You must be signed in to change notification settings - Fork 127
/
generator.js
102 lines (88 loc) · 3.39 KB
/
generator.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
const path = require('path')
const { gzipSync } = require('zlib')
const fs = require('fs-extra')
const { createSitemap, createSitemapIndex } = require('./builder')
const { createRoutesCache } = require('./cache')
const logger = require('./logger')
const { setDefaultSitemapOptions, setDefaultSitemapIndexOptions } = require('./options')
const { excludeRoutes } = require('./routes')
/**
* Generate a static file for each sitemap or sitemapindex
*
* @param {Object} options
* @param {Object} globalCache
* @param {Nuxt} nuxtInstance
*/
async function generateSitemaps(options, globalCache, nuxtInstance) {
const isSitemapIndex = options && options.sitemaps && Array.isArray(options.sitemaps) && options.sitemaps.length > 0
if (isSitemapIndex) {
await generateSitemapIndex(options, globalCache, nuxtInstance)
} else {
await generateSitemap(options, globalCache, nuxtInstance)
}
}
/**
* Generate a sitemap file
*
* @param {Object} options
* @param {Object} globalCache
* @param {Nuxt} nuxtInstance
*/
async function generateSitemap(options, globalCache, nuxtInstance) {
// Init options
options = setDefaultSitemapOptions(options, nuxtInstance)
// Init cache
const cache = {}
cache.staticRoutes = () => excludeRoutes(options.exclude, globalCache.staticRoutes)
cache.routes = createRoutesCache(cache, options)
// Generate sitemap.xml
const routes = await cache.routes.get('routes')
const base = nuxtInstance.options.router.base
const sitemap = await createSitemap(options, routes, base)
const xmlFilePath = path.join(nuxtInstance.options.generate.dir, options.path)
fs.outputFileSync(xmlFilePath, sitemap.toXML())
logger.success('Generated', getPathname(nuxtInstance.options.generate.dir, xmlFilePath))
// Generate sitemap.xml.gz
if (options.gzip) {
const gzipFilePath = path.join(nuxtInstance.options.generate.dir, options.pathGzip)
fs.outputFileSync(gzipFilePath, sitemap.toGzip())
logger.success('Generated', getPathname(nuxtInstance.options.generate.dir, gzipFilePath))
}
}
/**
* Generate a sitemapindex file
*
* @param {Object} options
* @param {Object} globalCache
* @param {Nuxt} nuxtInstance
*/
async function generateSitemapIndex(options, globalCache, nuxtInstance) {
// Init options
options = setDefaultSitemapIndexOptions(options)
// Generate sitemapindex.xml
const base = nuxtInstance.options.router.base
const xml = createSitemapIndex(options, base)
const xmlFilePath = path.join(nuxtInstance.options.generate.dir, options.path)
fs.outputFileSync(xmlFilePath, xml)
logger.success('Generated', getPathname(nuxtInstance.options.generate.dir, xmlFilePath))
// Generate sitemapindex.xml.gz
if (options.gzip) {
const gzip = gzipSync(xml)
const gzipFilePath = path.join(nuxtInstance.options.generate.dir, options.pathGzip)
fs.outputFileSync(gzipFilePath, gzip)
logger.success('Generated', getPathname(nuxtInstance.options.generate.dir, gzipFilePath))
}
// Generate linked sitemaps
await Promise.all(options.sitemaps.map(sitemapOptions => generateSitemaps(sitemapOptions, globalCache, nuxtInstance)))
}
/**
* Convert a file path to a URL pathname
*
* @param {string} dirPath
* @param {string} filePath
* @returns {string}
*/
function getPathname(dirPath, filePath) {
return [, ...path.relative(dirPath, filePath).split(path.sep)].join('/')
}
module.exports = { generateSitemaps, generateSitemap, generateSitemapIndex }