Webpack plugin to generate a sitemap. Designed to work with static-site-generator-webpack-plugin (although it also works great by itself).
npm install sitemap-webpack-plugin --save-dev
For webpack 4, use version 0.6 or greater. For webpack <= 3, use version 0.5.x.
Add to your webpack config -- see below for examples. The plugin signature is:
new SitemapPlugin(base, paths, options)
base
is the root URL of your site (e.g. 'https://mysite.com')paths
is the array of locations on your site -- this can be the same one you pass tostatic-site-generator-webpack-plugin
. If you want to customize each path in the sitemap, you can also pass an array of objects; objects must have apath
attribute and may have alastMod
,priority
, and/orchangeFreq
attribute. (However, an array of objects will not be directly compatible withstatic-site-generator-webpack-plugin
.)options
is an optional object of configurable options -- see below
fileName
(string) -- defaultsitemap.xml
-- name of the output filelastMod
(boolean) -- defaultfalse
-- whether to include the current date as the<lastmod>
. Can be overridden by path-specificlastMod
.priority
(number) -- defaultnull
-- a<priority>
to be set globally on all locations. Can be overridden by path-specificpriority
.changeFreq
(string) -- defaultnull
-- a<changefreq>
to be set globally on all locations; list of applicable values based on sitemaps.org:always
,hourly
,daily
,weekly
,monthly
,yearly
,never
. Can be overridden by path-specificchangeFreq
.skipGzip
(boolean) -- defaultfalse
-- whether to skip generating a gzipped.xml.gz
sitemap. (By default, both an uncompressed and a compressed sitemap are generated -- the compressed version is generated atsitemap.xml.gz
, or[fileName].gz
if thefileName
configuration option is set.)formatter
(function) -- defaultnull
-- an optional function to format the generated sitemap before it is emitted (for example, if you'd like to pretty-print the XML). The provided function must accept one argument (the unformatted XML) and return the formatted XML as a string. For an example of pretty-printing configuration, see the formatted test.
import SitemapPlugin from 'sitemap-webpack-plugin';
// If you are not using babel, note that the plugin is exported on default so you will need e.g.
const SitemapPlugin = require('sitemap-webpack-plugin').default;
/* basic paths -- directly compatible with static-site-generator-webpack-plugin */
const paths = [
'/foo/',
'/bar/'
];
/* object paths -- more fine-grained but not directly compatible with static-site-generator-webpack-plugin */
/* object paths must have a `path` attribute -- others are optional, and fall back to global config (if any) */
const paths = [
{
path: '/foo/',
lastMod: '2015-01-04',
priority: '0.8',
changeFreq: 'monthly'
}
];
/* you can also convert object paths back into static-site-generator-webpack-plugin compatible paths */
const staticSiteGeneratorCompatiblePaths = paths.map(({path}) => path);
export default {
/* snip */
/* basic (output defaults to sitemap.xml) */
plugins: [
new SitemapPlugin('https://mysite.com', paths)
]
/* with custom output filename */
plugins: [
new SitemapPlugin('https://mysite.com', paths, {
fileName: 'map.xml'
})
]
/* skip generating a gzipped version of the sitemap */
plugins: [
new SitemapPlugin('https://mysite.com', paths, {
skipGzip: true
})
]
/* with global options */
plugins: [
new SitemapPlugin('https://mysite.com', paths, {
fileName: 'map.xml',
lastMod: true,
changeFreq: 'monthly',
priority: '0.4'
})
]
};
- Fork it (https://github.com/schneidmaster/sitemap-webpack-plugin/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request