From 05ace9b429a629ec1ed4336e129a44d988359460 Mon Sep 17 00:00:00 2001 From: Yurk Sha Date: Wed, 12 Feb 2020 22:16:56 +0200 Subject: [PATCH 01/10] filter static routes by component option --- lib/module.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/module.js b/lib/module.js index 5d21f1d..5a5b590 100644 --- a/lib/module.js +++ b/lib/module.js @@ -25,7 +25,7 @@ module.exports = function module(moduleOptions) { // Init static routes nuxtInstance.extendRoutes(routes => { // Create a cache for static routes - globalCache.staticRoutes = getStaticRoutes(routes) + globalCache.staticRoutes = getStaticRoutes.call(nuxtInstance, routes) // On run cmd "build" if (!nuxtInstance.options.dev) { From c8267603765adbe60f0f20ce1b04b814deb52cb6 Mon Sep 17 00:00:00 2001 From: Yurk Sha Date: Wed, 12 Feb 2020 22:20:47 +0200 Subject: [PATCH 02/10] filter static routes by component option --- lib/routes.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/lib/routes.js b/lib/routes.js index 79c3fe8..c1256de 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -23,8 +23,15 @@ function excludeRoutes(patterns, routes) { * @returns {string[]} */ function getStaticRoutes(router) { + const nuxtInstance = this + // Get all static routes and ignore dynamic routes - return flattenRoutes(router).filter(({ url }) => !url.includes(':') && !url.includes('*')) + return flattenRoutes(router) + .filter(({ url }) => !url.includes(':') && !url.includes('*')) + .filter(nuxtInstance.options.sitemap_filter === true ? + _filter(nuxtInstance.options.alias) : + route => route + ) } /** @@ -50,4 +57,57 @@ function flattenRoutes(router, path = '', routes = []) { return routes } + +function _filter(aliases) { + const fs = require('fs') + + const prop = 'sitemap' + const value = true + + const aliasesKeys = Object.keys(aliases || {}).join('|') + const re_aliasReplacer = aliasesKeys && new RegExp(`^(${aliasesKeys})(.)`, 'g') + const aliasReplacer = (_s,alias,char) => aliases[alias] + (char !== '/' && char !== '\\' && '/' || '') + char + const normalizeComponentPath = (pathName) => re_aliasReplacer && pathName && pathName.replace(re_aliasReplacer, aliasReplacer) || pathName + + const extractComponentData = (text, ...exp) => { + return exp + .filter(re => re) + .reduce((out, re) => { + if (out) { + out = out.match(re) + return out && out[1] || void 0 + } + }, text) + } + + const re_0 = /\.vue$/ + const re_1 = /]*>([\s\S]*?)<\/script>/ + const re_2 = /export\s+default\s+({[\s\S]*?})[^}{]*$/ + const re_3 = new RegExp(prop + '\\s*:\\s*([^,\\s}]+)') + + const filterByComponentConfig = (component) => { + if (component) { + if (typeof component === 'string') { + const componentPath = normalizeComponentPath(component) + + if (componentPath) { + try { + return extractComponentData( + fs.readFileSync(componentPath, 'utf8'), + re_0.test(componentPath) && re_1, + re_2, + re_3 + ) === (value + '') + } catch(e) { } + } + } else if (typeof component === 'object') { + return (component.default || component)[prop] === value + } + } + return false + } + + return ({component}) => filterByComponentConfig(component) +} + module.exports = { excludeRoutes, getStaticRoutes } From 5a151b82a6b7b63e6f24104048295427357c0c56 Mon Sep 17 00:00:00 2001 From: Yurk Sha Date: Wed, 12 Feb 2020 22:34:04 +0200 Subject: [PATCH 03/10] Update README.md --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index f0487f6..ff2c96e 100755 --- a/README.md +++ b/README.md @@ -158,6 +158,41 @@ You can combine sitemap and sitemap index configurations. } ``` +### Enable static routes filtering by route component options. (experimental) + +Default route component with property `sitemap: true` will mark corresponding route to be included in sitemap. +Routes without this setting will be ignored. + +```js +// nuxt.config.js + +{ + modules: [ + '@nuxtjs/sitemap' + ], + sitemap_filter: true, + sitemap: { + // custom configuration + } +} +``` + +```js +// route component +const Component = { + sitemap: true, + template: '
Hello!
' +} + +// vue-router config +const router = new VueRouter({ + routes: [ + { path: '/hello', component: Component }, + ] +}) +``` + + ## Sitemap Options ### `routes` (optional) - array | function From 9dbe0bec2eb1e5c1c83f999a5ef2fd83b9bc8cb3 Mon Sep 17 00:00:00 2001 From: Yurk Sha Date: Wed, 12 Feb 2020 22:34:34 +0200 Subject: [PATCH 04/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ff2c96e..4e65261 100755 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ You can combine sitemap and sitemap index configurations. } ``` -### Enable static routes filtering by route component options. (experimental) +### Enable static routes filtering by route component options (experimental) Default route component with property `sitemap: true` will mark corresponding route to be included in sitemap. Routes without this setting will be ignored. From 17dbbb291f3f6866e3ff6268d8fe2f211d79fb92 Mon Sep 17 00:00:00 2001 From: Yurk Sha Date: Wed, 12 Feb 2020 22:41:51 +0200 Subject: [PATCH 05/10] Update README.md --- README.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4e65261..817ae3e 100755 --- a/README.md +++ b/README.md @@ -158,7 +158,7 @@ You can combine sitemap and sitemap index configurations. } ``` -### Enable static routes filtering by route component options (experimental) +### Enable static routes filtering by route component config Default route component with property `sitemap: true` will mark corresponding route to be included in sitemap. Routes without this setting will be ignored. @@ -192,6 +192,26 @@ const router = new VueRouter({ }) ``` +Single File Components are supported (experimental, tested with Nuxt.js). + +```js +// @/components/Hello.vue + + + +// vue-router config +const router = new VueRouter({ + routes: [ + { path: '/hello', component: '@/components/Hello.vue' }, + ] +}) +``` ## Sitemap Options From abda0431aaaacfe802bb5e484adfa04783743936 Mon Sep 17 00:00:00 2001 From: Yurk Sha Date: Wed, 12 Feb 2020 23:06:38 +0200 Subject: [PATCH 06/10] pretify --- lib/routes.js | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/lib/routes.js b/lib/routes.js index c1256de..4afc2d3 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -1,4 +1,5 @@ const { Minimatch } = require('minimatch') +const fs = require('fs') /** * Exclude routes by glob patterns @@ -28,10 +29,7 @@ function getStaticRoutes(router) { // Get all static routes and ignore dynamic routes return flattenRoutes(router) .filter(({ url }) => !url.includes(':') && !url.includes('*')) - .filter(nuxtInstance.options.sitemap_filter === true ? - _filter(nuxtInstance.options.alias) : - route => route - ) + .filter(nuxtInstance.options.sitemap_filter === true ? _filter(nuxtInstance.options.alias) : route => route) } /** @@ -59,15 +57,14 @@ function flattenRoutes(router, path = '', routes = []) { function _filter(aliases) { - const fs = require('fs') - const prop = 'sitemap' const value = true - + const aliasesKeys = Object.keys(aliases || {}).join('|') - const re_aliasReplacer = aliasesKeys && new RegExp(`^(${aliasesKeys})(.)`, 'g') - const aliasReplacer = (_s,alias,char) => aliases[alias] + (char !== '/' && char !== '\\' && '/' || '') + char - const normalizeComponentPath = (pathName) => re_aliasReplacer && pathName && pathName.replace(re_aliasReplacer, aliasReplacer) || pathName + const reAliasReplacer = aliasesKeys && new RegExp(`^(${aliasesKeys})(.)`, 'g') + const aliasReplacer = (_s, alias, char) => aliases[alias] + ((char !== '/' && char !== '\\' && '/') || '') + char + const normalizeComponentPath = (pathName) => + reAliasReplacer && pathName && pathName.replace(reAliasReplacer, aliasReplacer) || pathName const extractComponentData = (text, ...exp) => { return exp @@ -75,17 +72,17 @@ function _filter(aliases) { .reduce((out, re) => { if (out) { out = out.match(re) - return out && out[1] || void 0 + return (out && out[1]) || void 0 } }, text) } - const re_0 = /\.vue$/ - const re_1 = /]*>([\s\S]*?)<\/script>/ - const re_2 = /export\s+default\s+({[\s\S]*?})[^}{]*$/ - const re_3 = new RegExp(prop + '\\s*:\\s*([^,\\s}]+)') + const re0 = /\.vue$/ + const re1 = /]*>([\s\S]*?)<\/script>/ + const re2 = /export\s+default\s+({[\s\S]*?})[^}{]*$/ + const re3 = new RegExp(prop + '\\s*:\\s*([^,\\s}]+)') - const filterByComponentConfig = (component) => { + const filterByComponentConfig = component => { if (component) { if (typeof component === 'string') { const componentPath = normalizeComponentPath(component) @@ -94,11 +91,11 @@ function _filter(aliases) { try { return extractComponentData( fs.readFileSync(componentPath, 'utf8'), - re_0.test(componentPath) && re_1, - re_2, - re_3 - ) === (value + '') - } catch(e) { } + (re0.test(componentPath) && re1), + re2, + re3 + ) === value + '' + } catch (e) { } } } else if (typeof component === 'object') { return (component.default || component)[prop] === value From 76a848530c36284d579cf8421020d2cbc565fa7f Mon Sep 17 00:00:00 2001 From: Yurk Sha Date: Wed, 12 Feb 2020 23:11:25 +0200 Subject: [PATCH 07/10] prettify --- lib/routes.js | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/routes.js b/lib/routes.js index 4afc2d3..97db6aa 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -1,5 +1,5 @@ -const { Minimatch } = require('minimatch') const fs = require('fs') +const { Minimatch } = require('minimatch') /** * Exclude routes by glob patterns @@ -55,16 +55,14 @@ function flattenRoutes(router, path = '', routes = []) { return routes } - function _filter(aliases) { const prop = 'sitemap' const value = true - const aliasesKeys = Object.keys(aliases || {}).join('|') const reAliasReplacer = aliasesKeys && new RegExp(`^(${aliasesKeys})(.)`, 'g') const aliasReplacer = (_s, alias, char) => aliases[alias] + ((char !== '/' && char !== '\\' && '/') || '') + char - const normalizeComponentPath = (pathName) => - reAliasReplacer && pathName && pathName.replace(reAliasReplacer, aliasReplacer) || pathName + const normalizeComponentPath = pathName => + (reAliasReplacer && pathName && pathName.replace(reAliasReplacer, aliasReplacer)) || pathName const extractComponentData = (text, ...exp) => { return exp @@ -89,12 +87,7 @@ function _filter(aliases) { if (componentPath) { try { - return extractComponentData( - fs.readFileSync(componentPath, 'utf8'), - (re0.test(componentPath) && re1), - re2, - re3 - ) === value + '' + return extractComponentData(fs.readFileSync(componentPath, 'utf8'), (re0.test(componentPath) && re1), re2, re3) === value + '' } catch (e) { } } } else if (typeof component === 'object') { @@ -104,7 +97,7 @@ function _filter(aliases) { return false } - return ({component}) => filterByComponentConfig(component) + return ({ component }) => filterByComponentConfig(component) } module.exports = { excludeRoutes, getStaticRoutes } From dd8c8cd46d440eab0a7fff5f08e568b0e8e798ff Mon Sep 17 00:00:00 2001 From: Yurk Sha Date: Wed, 12 Feb 2020 23:14:09 +0200 Subject: [PATCH 08/10] prettify --- lib/routes.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/routes.js b/lib/routes.js index 97db6aa..613830d 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -61,7 +61,7 @@ function _filter(aliases) { const aliasesKeys = Object.keys(aliases || {}).join('|') const reAliasReplacer = aliasesKeys && new RegExp(`^(${aliasesKeys})(.)`, 'g') const aliasReplacer = (_s, alias, char) => aliases[alias] + ((char !== '/' && char !== '\\' && '/') || '') + char - const normalizeComponentPath = pathName => + const normalizeComponentPath = pathName => (reAliasReplacer && pathName && pathName.replace(reAliasReplacer, aliasReplacer)) || pathName const extractComponentData = (text, ...exp) => { @@ -87,7 +87,10 @@ function _filter(aliases) { if (componentPath) { try { - return extractComponentData(fs.readFileSync(componentPath, 'utf8'), (re0.test(componentPath) && re1), re2, re3) === value + '' + return ( + extractComponentData(fs.readFileSync(componentPath, 'utf8'), (re0.test(componentPath) && re1), re2, re3) === + value + '' + ) } catch (e) { } } } else if (typeof component === 'object') { From 0739cf24289b61a223662e6f00a10e917214cd8e Mon Sep 17 00:00:00 2001 From: Yurk Sha Date: Wed, 12 Feb 2020 23:15:38 +0200 Subject: [PATCH 09/10] prettify --- lib/routes.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/routes.js b/lib/routes.js index 613830d..2bcae65 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -88,10 +88,10 @@ function _filter(aliases) { if (componentPath) { try { return ( - extractComponentData(fs.readFileSync(componentPath, 'utf8'), (re0.test(componentPath) && re1), re2, re3) === + extractComponentData(fs.readFileSync(componentPath, 'utf8'), (re0.test(componentPath) && re1), re2, re3) === value + '' ) - } catch (e) { } + } catch (e) {} } } else if (typeof component === 'object') { return (component.default || component)[prop] === value From 75109318ee304b1140fef674c38d6885cdc35b79 Mon Sep 17 00:00:00 2001 From: Yurk Sha Date: Wed, 12 Feb 2020 23:17:14 +0200 Subject: [PATCH 10/10] prettify --- lib/routes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/routes.js b/lib/routes.js index 2bcae65..075d2d3 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -88,7 +88,7 @@ function _filter(aliases) { if (componentPath) { try { return ( - extractComponentData(fs.readFileSync(componentPath, 'utf8'), (re0.test(componentPath) && re1), re2, re3) === + extractComponentData(fs.readFileSync(componentPath, 'utf8'), re0.test(componentPath) && re1, re2, re3) === value + '' ) } catch (e) {}