diff --git a/README.md b/README.md index 3bd7e73..cbb3434 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ const defaultConfig = { minify: false, concurrency: 4, additionalSitemapUrls: [], + flatStructure: false, } ``` @@ -57,6 +58,7 @@ const defaultConfig = { * `minify` is a boolean or a [html-minifier](https://github.com/kangax/html-minifier) configuration object. * `concurrency` controls how many routes are pre-rendered in parallel. (Default: `4`) * `additionalSitemapUrls` is a list of URLs to include as well to the generated `sitemap.xml`. (Default: `[]`) +* `flatStructure` controls if prep should generate index.html files in route named subfolders or `{routeName}.html` files at top level. (Default: `false`) ## Example `prep.js` diff --git a/src/index.js b/src/index.js index d88f333..d8d1964 100755 --- a/src/index.js +++ b/src/index.js @@ -38,6 +38,7 @@ async function crawlAndWrite (configuration) { minify: false, concurrency: 4, additionalSitemapUrls: [], + flatStructure: false, }, configuration) debug('Config prepared', configuration) @@ -120,20 +121,43 @@ async function prepRoute (route, configuration) { debug('Crawling completed: %s', url) - const filePath = path.join(tmpDir, route) - mkdirp.sync(filePath) + const cleanRoute = route.replace('.html', '') - debug('Directory created: %s', filePath) + let filePath + + if (configuration.flatStructure) { + filePath = tmpDir + mkdirp.sync(filePath) + } else { + filePath = path.join(tmpDir, cleanRoute) + mkdirp.sync(filePath) + + debug('Directory created: %s', filePath) + } if (configuration.minify) { const minifyConfig = configuration.minify === true ? {} : configuration.minify const minifiedContent = minify(content, minifyConfig) - fs.writeFileSync(path.join(filePath, 'index.html'), minifiedContent) + if (configuration.flatStructure) { + fs.writeFileSync(path.join(filePath, `${cleanRoute}.html`), minifiedContent) + } else { + fs.writeFileSync(path.join(filePath, 'index.html'), minifiedContent) + } } else { - fs.writeFileSync(path.join(filePath, 'index.html'), content) + if (configuration.flatStructure) { + fs.writeFileSync(path.join(filePath, `${cleanRoute}.html`), content) + } else { + fs.writeFileSync(path.join(filePath, 'index.html'), content) + } } - const logFileName = `${route}/index.html`.replace(/^\//, '') + let logFileName + + if (configuration.flatStructure) { + logFileName = `${cleanRoute}.html`.replace(/^\//, '') + } else { + logFileName = `${route}/index.html`.replace(/^\//, '') + } console.log(`prep: Rendered ${logFileName}`) }