Skip to content

Commit

Permalink
fix: root index is saved last to preserve template
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Rosenberg committed May 24, 2021
1 parent 9461938 commit 3f2d67a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ async function start(options) {
return url
}

const urlToHtml = saveUrlToHtml(options)
const afterSave = {}
const saveUrlToHtml = createSaveUrlToHtml(options, afterSave)

if (options.inlineDynamicImports)
await inlineScript(options.script)
Expand All @@ -82,7 +83,7 @@ async function start(options) {
queue.push(async () => {
counter++
spinner.text = `Exporting ${counter} of ${urls.length} ${url.path}`
url.children = await urlToHtml(url.path)
url.children = await saveUrlToHtml(url.path)

if (depth < options.depth) {
const newUrls = url.children
Expand All @@ -100,6 +101,8 @@ async function start(options) {

const time = Date.now()
await new Promise((resolve) => { queue.done = () => resolve() })
if (afterSave.saveRootIndex)
await afterSave.saveRootIndex()
spinner.succeed(`Exported ${counter} pages (${urls.length - counter} ignored) from total ${urls.length} pages in ${Date.now() - time} ms`)

if (options.writeSummary)
Expand All @@ -123,14 +126,18 @@ function writeSummary(urls, options) {


/** @param {Options} options */
function saveUrlToHtml(options) {
function createSaveUrlToHtml(options, afterSave) {
const { entrypoint, script, outputDir, forceIndex, eventName, host, ssrOptions, inlineDynamicImports } = options

/** @param {string} url */
return async function urlToHtml(url) {
return async function saveUrlToHtml(url) {
const html = await tossr(entrypoint, script, url, { silent: true, eventName, host, inlineDynamicImports, ...ssrOptions })
const suffix = forceIndex && !url.endsWith('/index') ? '/index' : ''
await outputFile(`${outputDir + url + suffix}.html`, html)
const saveRootIndex = () => outputFile(`${outputDir + url + suffix}.html`, html)
console.log(url)
if (url !== '/index')
await saveRootIndex()
else afterSave.saveRootIndex = saveRootIndex
const dom = parse(html)
return dom.querySelectorAll('a')
.filter(s => s.attributes.href)
Expand Down

0 comments on commit 3f2d67a

Please sign in to comment.