ulivz / vuepress-plugin-export

Export your VuePress site to a PDF file
MIT License
90 stars 17 forks source link

Timeouts and config.js#base #3

Open sullivanpt opened 5 years ago

sullivanpt commented 5 years ago

This tool is really cool! Thank you. I ran into a few issues using it that were pretty easy to work-around in my local code. I'm not sure yet how to fix them correctly so no PR today.

First issue, the paths sent to puppeteer don't include the base from my .vuepress/config.js.

 const exportPages = pages.map(page => {
    return {
      url: page.path,
      title: page.title,
      location: `http://${host}:${port}<BASE HERE WITHOUT TRAILING SLASH>${page.path}`,
      path: `${tempDir}/${page.key}.pdf`
    }
  })

Second issue, puppeteer tries to navigate before the build is finished; in my case the build is over a minute, and puppeteer times out waiting for the first page to load. I worked around it by inserting a long await on setTimeout for the first loop iteration. I do see vuepress actually logging a message to console when the build is finally done. If the plug in could event on that, it would be cleaner.

   if (I===0) await SOME LONG TIMEOUT

    await browserPage.goto(
      location,
      { waitUntil: 'networkidle2' }
    )

Third issue, pdfbox.jar PDFMerger requires a JDK. The README should point out this dependency.

Fourth issue, it exports all languages and the page assembly order is pretty random. I worked around by assembling/merging myself.

sullivanpt commented 4 years ago

Some updates on these issues.

Regarding the second issue with puppeteer navigating before the build is finished, I found a more elegant solution than the arbitrary timeout by using the webpack compiler done hook as follows.

          const devContext = await dev({
            sourceDir: dir,
            clearScreen: false,
            theme: options.theme || '@vuepress/default'
          })

          await new Promise(resolve => {
            devContext.devProcess.server.compiler.hooks.done.tap('webpack-dev-server', () => {
              console.log('webpack-dev-server compiled')
              resolve()
            })
          })

A new fifth issue, using http://0.0.0.0 as a client browser address fails on Windows. Using displayHost fixes the issue. (See vuejs/vuepress@4d5c50e for details)

  await generatePDF(devContext, {
              port: devContext.devProcess.port,
              host: devContext.devProcess.displayHost,
              base: devContext.base,
              options
            })