Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds the flatStructure option to the prep renderer #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const defaultConfig = {
minify: false,
concurrency: 4,
additionalSitemapUrls: [],
flatStructure: false,
}
```

Expand All @@ -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`

Expand Down
36 changes: 30 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ async function crawlAndWrite (configuration) {
minify: false,
concurrency: 4,
additionalSitemapUrls: [],
flatStructure: false,
}, configuration)

debug('Config prepared', configuration)
Expand Down Expand Up @@ -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}`)
}

Expand Down