@@ -4,14 +4,16 @@ const util = require('util')
44
55const globby = require ( 'globby' )
66const mkdirp = require ( 'mkdirp' )
7- const sm = require ( 'sitemap' )
7+ const { createSitemap } = require ( 'sitemap' )
88
9- const getPaths = async ( { distPath, exclude } ) => {
9+ const ensureTrailingSlash = ( url ) => ( url . endsWith ( '/' ) ? url : `${ url } /` )
10+
11+ const getPaths = async ( { distPath, exclude, cwd } ) => {
1012 const htmlFiles = `${ distPath } /**/**.html`
1113 const excludeFiles = ( exclude || [ ] ) . map ( ( filePath ) => `!${ filePath . replace ( / ^ ! / , '' ) } ` )
1214
1315 const lookup = [ htmlFiles ] . concat ( excludeFiles )
14- const paths = await globby ( lookup )
16+ const paths = await globby ( lookup , { cwd } )
1517 return paths
1618}
1719
@@ -28,34 +30,47 @@ const getUrlFromFile = ({ file, distPath, prettyURLs, trailingSlash }) => {
2830 return prettyUrl
2931 }
3032
31- return prettyUrl . endsWith ( '/' ) ? prettyUrl : ` ${ prettyUrl } /`
33+ return ensureTrailingSlash ( prettyUrl )
3234}
3335
34- const DEFAULT_CHANGE_FREQ = 'weekly'
35- const DEFAULT_PRIORITY = 0.8
36- // 600 sec cache period
37- const DEFAULT_CACHE_TIME = 600000
38-
39- module . exports = async function makeSitemap ( opts = { } ) {
40- const { distPath, fileName, homepage, exclude, prettyURLs, trailingSlash, failBuild } = opts
41- const paths = await getPaths ( { distPath, exclude } )
36+ const getUrlsFromPaths = ( { paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd } ) => {
4237 const urls = paths . map ( ( file ) => {
4338 const url = getUrlFromFile ( { file, distPath, prettyURLs, trailingSlash } )
4439 return {
4540 url,
46- changefreq : opts . changeFreq || DEFAULT_CHANGE_FREQ ,
47- priority : opts . priority || DEFAULT_PRIORITY ,
41+ changefreq : changeFreq ,
42+ priority,
4843 lastmodrealtime : true ,
49- lastmodfile : file ,
44+ lastmodfile : cwd === undefined ? file : path . resolve ( cwd , file ) ,
5045 }
5146 } )
52- const options = {
53- hostname : `${ homepage . replace ( / \/ $ / , '' ) } /` ,
54- cacheTime : DEFAULT_CACHE_TIME ,
55- urls,
56- }
47+ return urls
48+ }
49+
50+ const DEFAULT_CHANGE_FREQ = 'weekly'
51+ const DEFAULT_PRIORITY = 0.8
52+ // 600 sec cache period
53+ const DEFAULT_CACHE_TIME = 600000
54+
55+ module . exports = async function makeSitemap ( opts = { } ) {
56+ const {
57+ distPath,
58+ fileName,
59+ homepage,
60+ exclude,
61+ prettyURLs,
62+ trailingSlash,
63+ failBuild,
64+ cwd,
65+ changeFreq = DEFAULT_CHANGE_FREQ ,
66+ priority = DEFAULT_PRIORITY ,
67+ } = opts
68+
69+ const paths = await getPaths ( { distPath, exclude, cwd } )
70+ const urls = getUrlsFromPaths ( { paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd } )
71+
5772 // Creates a sitemap object given the input configuration with URLs
58- const sitemap = sm . createSitemap ( options )
73+ const sitemap = createSitemap ( { hostname : ensureTrailingSlash ( homepage ) , cacheTime : DEFAULT_CACHE_TIME , urls } )
5974 // Generates XML
6075 try {
6176 await util . promisify ( sitemap . toXML . bind ( sitemap ) ) ( )
0 commit comments