nextjs-sitemap-generator
Advanced tools
Comparing version 0.4.2 to 0.5.0
203
core.js
@@ -1,18 +0,11 @@ | ||
const fs = require("fs"); | ||
const dateFns = require("date-fns"); | ||
const path = require("path"); | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const fs_1 = __importDefault(require("fs")); | ||
const date_fns_1 = require("date-fns"); | ||
const path_1 = __importDefault(require("path")); | ||
class SiteMapper { | ||
constructor({ | ||
alternateUrls, | ||
baseUrl, | ||
ignoreIndexFiles, | ||
ignoredPaths, | ||
pagesDirectory, | ||
sitemapPath, | ||
targetDirectory, | ||
nextConfigPath, | ||
ignoredExtensions, | ||
pagesConfig | ||
}) { | ||
constructor({ alternateUrls, baseUrl, ignoreIndexFiles, ignoredPaths, pagesDirectory, targetDirectory, nextConfigPath, ignoredExtensions, pagesConfig }) { | ||
this.pagesConfig = pagesConfig || {}; | ||
@@ -25,3 +18,2 @@ this.alternatesUrls = alternateUrls || {}; | ||
this.pagesdirectory = pagesDirectory; | ||
this.sitemapPath = sitemapPath; | ||
this.targetDirectory = targetDirectory; | ||
@@ -32,7 +24,5 @@ this.nextConfigPath = nextConfigPath; | ||
`; | ||
if (this.nextConfigPath) { | ||
this.nextConfig = require(nextConfigPath); | ||
if (typeof this.nextConfig === "function") { | ||
if (typeof this.nextConfig === 'function') { | ||
this.nextConfig = this.nextConfig([], {}); | ||
@@ -42,80 +32,74 @@ } | ||
} | ||
preLaunch() { | ||
fs.writeFileSync( | ||
path.resolve(this.targetDirectory, "./sitemap.xml"), | ||
this.sitemap, | ||
{ | ||
flag: "w" | ||
} | ||
); | ||
fs_1.default.writeFileSync(path_1.default.resolve(this.targetDirectory, './sitemap.xml'), this.sitemap, { | ||
flag: 'w' | ||
}); | ||
} | ||
finish() { | ||
fs.writeFileSync( | ||
path.resolve(this.targetDirectory, "./sitemap.xml"), | ||
"</urlset>", | ||
{ | ||
flag: "as" | ||
} | ||
); | ||
fs_1.default.writeFileSync(path_1.default.resolve(this.targetDirectory, './sitemap.xml'), '</urlset>', { | ||
flag: 'as' | ||
}); | ||
} | ||
/** | ||
* | ||
*/ | ||
isReservedPage(site) { | ||
let isReserved = false; | ||
if (site.charAt(0) === '_' || site.charAt(0) === '.') | ||
isReserved = true; | ||
return isReserved; | ||
} | ||
isIgnoredPath(site) { | ||
let toIgnore = false; | ||
for (const ignoredPath of this.ignoredPaths) { | ||
if (site.includes(ignoredPath)) | ||
toIgnore = true; | ||
} | ||
return toIgnore; | ||
} | ||
isIgnoredExtension(fileExtension) { | ||
let toIgnoreExtension = false; | ||
for (const extensionToIgnore of this.ignoredExtensions) { | ||
if (extensionToIgnore === fileExtension) | ||
toIgnoreExtension = true; | ||
} | ||
return toIgnoreExtension; | ||
} | ||
mergePath(basePath, currentPage) { | ||
let newBasePath = basePath; | ||
if (!basePath && !currentPage) | ||
return ''; | ||
if (!newBasePath) { | ||
newBasePath = '/'; | ||
} | ||
else if (currentPage) { | ||
newBasePath += '/'; | ||
} | ||
return newBasePath + currentPage; | ||
} | ||
buildPathMap(dir) { | ||
var pathMap = {}; | ||
const {exportTrailingSlash} = this.nextConfig || {}; | ||
let data = fs.readdirSync(dir); | ||
for (let site of data) { | ||
let pathMap = {}; | ||
const data = fs_1.default.readdirSync(dir); | ||
for (const site of data) { | ||
// Filter directories | ||
if (site[0] === "_" || site[0] === ".") continue; | ||
if (this.isReservedPage(site)) | ||
continue; | ||
let toIgnore = false; | ||
for (let path of this.ignoredPaths) { | ||
if (site.includes(path)) toIgnore = true; | ||
} | ||
if (toIgnore) continue; | ||
// Handle recursive paths | ||
if (fs.lstatSync(dir + path.sep + site).isDirectory()) { | ||
toIgnore = this.isIgnoredPath(site); | ||
if (toIgnore) | ||
continue; | ||
const nextPath = dir + path_1.default.sep + site; | ||
if (fs_1.default.lstatSync(nextPath).isDirectory()) { | ||
pathMap = { | ||
...pathMap, | ||
...this.buildPathMap(dir + path.sep + site) | ||
...this.buildPathMap(dir + path_1.default.sep + site) | ||
}; | ||
continue; | ||
} | ||
// Is file | ||
let fileExtension = site.split(".").pop(); | ||
//Ignoring file extension by user config | ||
let toIgnoreExtension = false; | ||
for (let extensionToIgnore of this.ignoredExtensions) { | ||
if (extensionToIgnore === fileExtension) toIgnoreExtension = true; | ||
} | ||
if (toIgnoreExtension) continue; | ||
// | ||
let fileNameWithoutExtension = site.substring( | ||
0, | ||
site.length - (fileExtension.length + 1) | ||
); | ||
fileNameWithoutExtension = | ||
this.ignoreIndexFiles && fileNameWithoutExtension === "index" | ||
? "" | ||
: fileNameWithoutExtension; | ||
let newDir = dir.replace(this.pagesdirectory, "").replace(/\\/g, "/"); | ||
if (this.ignoreIndexFiles && newDir === "/index") { | ||
newDir = ""; | ||
} | ||
let pagePath = [newDir, fileNameWithoutExtension] | ||
.filter(val => exportTrailingSlash || !!val) | ||
.join("/"); | ||
const fileExtension = site.split('.').pop(); | ||
if (this.isIgnoredExtension(fileExtension)) | ||
continue; | ||
let fileNameWithoutExtension = site.substring(0, site.length - (fileExtension.length + 1)); | ||
fileNameWithoutExtension = this.ignoreIndexFiles && fileNameWithoutExtension === 'index' ? '' : fileNameWithoutExtension; | ||
let newDir = dir.replace(this.pagesdirectory, '').replace(/\\/g, '/'); | ||
if (newDir === '/index') | ||
newDir = ''; | ||
const pagePath = this.mergePath(newDir, fileNameWithoutExtension); | ||
pathMap[pagePath] = { | ||
@@ -125,42 +109,35 @@ page: pagePath | ||
} | ||
return pathMap; | ||
} | ||
async sitemapMapper(dir) { | ||
var pathMap = this.buildPathMap(dir); | ||
let pathMap = this.buildPathMap(dir); | ||
const exportPathMap = this.nextConfig && this.nextConfig.exportPathMap; | ||
if (exportPathMap) { | ||
try { | ||
pathMap = await exportPathMap(pathMap, {}); | ||
} catch (err) { | ||
} | ||
catch (err) { | ||
console.log(err); | ||
} | ||
} | ||
const paths = Object.keys(pathMap); | ||
const date = dateFns.format(new Date(), "YYYY-MM-DD"); | ||
for (var i = 0, len = paths.length; i < len; i++) { | ||
let pagePath = paths[i]; | ||
let alternates = ""; | ||
let priority = ""; | ||
let changefreq = ""; | ||
for (let langSite in this.alternatesUrls) { | ||
const date = date_fns_1.format(new Date(), 'yyyy-MM-dd'); | ||
for (let i = 0, len = paths.length; i < len; i++) { | ||
const pagePath = paths[i]; | ||
let alternates = ''; | ||
let priority = ''; | ||
let changefreq = ''; | ||
for (const langSite in this.alternatesUrls) { | ||
alternates += `<xhtml:link rel="alternate" hreflang="${langSite}" href="${this.alternatesUrls[langSite]}${pagePath}" />`; | ||
} | ||
if (this.pagesConfig && this.pagesConfig[pagePath.toLowerCase()]) { | ||
let pageConfig = this.pagesConfig[pagePath]; | ||
const pageConfig = this.pagesConfig[pagePath]; | ||
priority = pageConfig.priority | ||
? `<priority>${pageConfig.priority}</priority>` | ||
: ""; | ||
: ''; | ||
changefreq = pageConfig.changefreq | ||
? `<changefreq>${pageConfig.changefreq}</changefreq>` | ||
: ""; | ||
: ''; | ||
} | ||
let xmlObject = `<url><loc>${this.baseUrl}${pagePath}</loc> | ||
const xmlObject = `<url><loc>${this.baseUrl}${pagePath}</loc> | ||
${alternates} | ||
@@ -171,12 +148,8 @@ ${priority} | ||
</url>`; | ||
fs.writeFileSync( | ||
path.resolve(this.targetDirectory, "./sitemap.xml"), | ||
xmlObject, | ||
{flag: "as"} | ||
); | ||
fs_1.default.writeFileSync(path_1.default.resolve(this.targetDirectory, './sitemap.xml'), xmlObject, { | ||
flag: 'as' | ||
}); | ||
} | ||
} | ||
} | ||
module.exports = SiteMapper; | ||
exports.default = SiteMapper; |
26
index.js
@@ -1,13 +0,15 @@ | ||
const Core = require("./core.js"); | ||
module.exports = async function(config) { | ||
if (!config) { | ||
throw new Error("Config is mandatory"); | ||
} | ||
let coreMapper = new Core(config); | ||
coreMapper.preLaunch(); | ||
await coreMapper.sitemapMapper(config.pagesDirectory); | ||
coreMapper.finish(); | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const core_1 = __importDefault(require("./core")); | ||
module.exports = async function (config) { | ||
if (!config) { | ||
throw new Error('Config is mandatory'); | ||
} | ||
const coreMapper = new core_1.default(config); | ||
coreMapper.preLaunch(); | ||
await coreMapper.sitemapMapper(config.pagesDirectory); | ||
coreMapper.finish(); | ||
}; |
{ | ||
"name": "nextjs-sitemap-generator", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "Generate sitemap.xml from nextjs pages", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "yarn jest && tsc" | ||
}, | ||
@@ -18,5 +19,29 @@ "keywords": [ | ||
"dependencies": { | ||
"date-fns": "^1.30.1" | ||
"date-fns": "^2.9.0" | ||
}, | ||
"homepage": "https://github.com/IlusionDev/nextjs-sitemap-generator" | ||
} | ||
"husky": { | ||
"hooks": { | ||
"pre-push": "yarn jest" | ||
} | ||
}, | ||
"homepage": "https://github.com/IlusionDev/nextjs-sitemap-generator", | ||
"devDependencies": { | ||
"@types/jest": "^24.0.25", | ||
"@types/node": "^13.1.6", | ||
"@typescript-eslint/eslint-plugin": "^2.15.0", | ||
"@typescript-eslint/parser": "^2.15.0", | ||
"eslint": "^6.8.0", | ||
"eslint-config-airbnb-base": "^14.0.0", | ||
"eslint-config-standard": "^14.1.0", | ||
"eslint-plugin-import": "^2.20.0", | ||
"eslint-plugin-jest": "^23.4.0", | ||
"eslint-plugin-node": "^11.0.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"husky": "^4.0.6", | ||
"jest": "^24.9.0", | ||
"prettier": "^1.19.1", | ||
"ts-jest": "^24.3.0", | ||
"typescript": "^3.7.4" | ||
} | ||
} |
@@ -34,3 +34,3 @@ ![npmv1](https://img.shields.io/npm/v/nextjs-sitemap-generator.svg) | ||
targetDirectory : 'static/', | ||
nextConfigPath: __dirname + "\\next.config.js" | ||
nextConfigPath: __dirname + "\\next.config.js", | ||
ignoredExtensions: [ | ||
@@ -37,0 +37,0 @@ 'png', |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
230368
36
580
17
1
+ Added@babel/runtime@7.24.6(transitive)
+ Addeddate-fns@2.30.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
- Removeddate-fns@1.30.1(transitive)
Updateddate-fns@^2.9.0