jantimon / html-webpack-plugin

Simplifies creation of HTML files to serve your webpack bundles
MIT License
10.71k stars 1.31k forks source link

Request sitemap.txt generation #1823

Closed dimaslanjaka closed 9 months ago

dimaslanjaka commented 1 year ago

Is your feature request related to a problem? Please describe. I want print generated html into sitemap.txt

Describe the solution you'd like

may this feature can generate using below formula:

// in global scope
const sitemaps = [] as string[];

// in html generator
const basePath = htmlWebpackPluginOption.publicPath; // useful for webpack deployment for subfolder
const baseUrl = htmlWebpackPluginOption.baseUrl; // create new option baseUrl
const filename = htmlWebpackPluginOption.filename; // the file name
const resultUrl = `${baseUrl}/${basePath}/${filename}`; // http://example.com/basepath/filename.html
sitemaps.push(resultUrl.replace(/([^:]\/)\/+/g, "$1")); // push url with fixed doubled slashed in url pathname

// in final generator
fs.writeFileSync(path.join(webpackOption.output.path, 'sitemap.txt'), sitemaps.join('\n')); // will generate for example in dist/sitemap.txt

in webpack.config.js like:

// this will generate `http://example.net/www/index.html` in `dist/sitemap.txt`
new HtmlWebpackPlugin({
  baseUrl: 'http://example.net',
  filename: 'index.html', // create index.html
  template: path.resolve('src', 'main.html'), // source html layout
  publicPath: 'www', // base directory from root domain
  minify: process.env.NODE_ENV !== 'development', // minify on production
});

the result in dist/sitemap.txt

http://domain.com/path/to/file.html
http://domain.com/path/to/file2.html
http://domain.com/path/to/file3.html
http://domain.com/path/to/file4.html
dimaslanjaka commented 1 year ago

@jantimon review pls

alexander-akait commented 10 months ago

Can you describe usecase for this?

dimaslanjaka commented 9 months ago

Can you describe usecase for this?

for seo when generate static site using webpack. So I don't need to create a sitemap anymore, because it has been generated by the Webpack HTML plugin.

alexander-akait commented 9 months ago

Sorry, it is out of scope html-webpack-plugin, you can use https://github.com/schneidmaster/sitemap-webpack-plugin, that is why we have the plugin API, feel free to feedback

dimaslanjaka commented 9 months ago

Sorry, it is out of scope html-webpack-plugin, you can use https://github.com/schneidmaster/sitemap-webpack-plugin, that is why we have the plugin API, feel free to feedback

That plugin does not support sitemap.txt, and must also manage each URL object. It cannot be automated when creating html like this 'webpack html plugin'. It's the same as if I have to create a manual URL list if I want to add a new endpoint :(