Skip to content

Commit 1d5905c

Browse files
authored
fix: support buildDir=. (#41)
1 parent a38282f commit 1d5905c

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

make_sitemap.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@ const getPaths = async ({ distPath, exclude, cwd }) => {
1717
return paths
1818
}
1919

20-
const getUrlFromFile = ({ file, distPath, prettyURLs, trailingSlash }) => {
21-
const url = file.startsWith(distPath) ? file.replace(distPath, '') : distPath
20+
const normalizeFile = ({ file, distPath }) => {
21+
// handle root distPath
22+
if (distPath === '.') {
23+
return `/${file}`
24+
}
2225

23-
if (!prettyURLs) {
24-
return url
26+
if (file.startsWith(distPath)) {
27+
return file.replace(distPath, '')
2528
}
2629

30+
return distPath
31+
}
32+
33+
const prettifyUrl = ({ url, trailingSlash }) => {
2734
const prettyUrl = url.replace(/\/index\.html$/, '').replace(/\.html$/, '')
2835

2936
if (!trailingSlash) {
@@ -33,6 +40,17 @@ const getUrlFromFile = ({ file, distPath, prettyURLs, trailingSlash }) => {
3340
return ensureTrailingSlash(prettyUrl)
3441
}
3542

43+
const getUrlFromFile = ({ file, distPath, prettyURLs, trailingSlash }) => {
44+
const url = normalizeFile({ file, distPath })
45+
46+
if (!prettyURLs) {
47+
return url
48+
}
49+
50+
const prettyUrl = prettifyUrl({ url, trailingSlash })
51+
return prettyUrl
52+
}
53+
3654
const getUrlsFromPaths = ({ paths, distPath, prettyURLs, trailingSlash, changeFreq, priority, cwd }) => {
3755
const urls = paths.map((file) => {
3856
const url = getUrlFromFile({ file, distPath, prettyURLs, trailingSlash })

tests/sitemap.test.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,27 @@ test.afterEach(async (t) => {
2222

2323
// Test relative and absolute paths
2424
const CONFIGS = [
25-
{ distPath: './fixtures', testNamePostfix: 'relative path', cwd: __dirname },
26-
{ distPath: path.resolve(__dirname, './fixtures'), testNamePostfix: 'absolute path' },
25+
{
26+
distPath: './fixtures',
27+
testNamePostfix: 'relative path',
28+
cwd: __dirname,
29+
excludePath: './fixtures/children/grandchildren/grandchild-two.html',
30+
},
31+
{
32+
distPath: path.resolve(__dirname, './fixtures'),
33+
testNamePostfix: 'absolute path',
34+
excludePath: path.resolve(__dirname, './fixtures/children/grandchildren/grandchild-two.html'),
35+
},
36+
{
37+
distPath: '.',
38+
testNamePostfix: 'root relative path',
39+
cwd: path.join(__dirname, 'fixtures'),
40+
excludePath: './children/grandchildren/grandchild-two.html',
41+
},
2742
]
2843

2944
// eslint-disable-next-line max-lines-per-function
30-
CONFIGS.forEach(({ distPath, testNamePostfix, cwd }) => {
45+
CONFIGS.forEach(({ distPath, testNamePostfix, cwd, excludePath }) => {
3146
test(`Creates Sitemap with all html files - ${testNamePostfix}`, async (t) => {
3247
const { fileName } = t.context
3348
const sitemapData = await makeSitemap({
@@ -143,8 +158,6 @@ CONFIGS.forEach(({ distPath, testNamePostfix, cwd }) => {
143158

144159
test(`Sitemap exclude works correctly - ${testNamePostfix}`, async (t) => {
145160
const { fileName } = t.context
146-
const toExclude = './fixtures/children/grandchildren/grandchild-two.html'
147-
const excludePath = cwd === undefined ? path.resolve(__dirname, toExclude) : toExclude
148161
const sitemapData = await makeSitemap({
149162
homepage: 'https://site.com/',
150163
distPath,

0 commit comments

Comments
 (0)