2017-06-18 15:44:01 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import { copy, remove, writeFile, mkdirp } from 'fs-extra'
|
2017-01-11 19:14:59 +00:00
|
|
|
import _ from 'lodash'
|
|
|
|
import { resolve, join, dirname, sep } from 'path'
|
|
|
|
import { minify } from 'html-minifier'
|
2017-06-13 17:58:04 +00:00
|
|
|
import Tapable from 'tappable'
|
2017-06-16 12:42:45 +00:00
|
|
|
import { isUrl, promisifyRoute, waitFor, flatRoutes } from 'utils'
|
2017-06-18 09:35:00 +00:00
|
|
|
import Debug from 'debug'
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-06-18 09:35:00 +00:00
|
|
|
const debug = Debug('nuxt:generate')
|
2016-11-10 11:33:52 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
export default class Generator extends Tapable {
|
2017-06-18 15:44:01 +00:00
|
|
|
constructor (nuxt, builder) {
|
2017-06-11 14:17:36 +00:00
|
|
|
super()
|
|
|
|
this.nuxt = nuxt
|
|
|
|
this.options = nuxt.options
|
2017-06-18 15:44:01 +00:00
|
|
|
this.builder = builder
|
2017-07-03 11:11:40 +00:00
|
|
|
|
|
|
|
// Set variables
|
|
|
|
this.generateRoutes = resolve(this.options.srcDir, 'static')
|
|
|
|
this.srcBuiltPath = resolve(this.options.buildDir, 'dist')
|
|
|
|
this.distPath = resolve(this.options.rootDir, this.options.generate.dir)
|
|
|
|
this.distNuxtPath = join(this.distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath))
|
2017-05-11 12:06:57 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-07-04 22:26:01 +00:00
|
|
|
async generate ({ build = true, init = true } = {}) {
|
2017-06-11 14:17:36 +00:00
|
|
|
const s = Date.now()
|
|
|
|
let errors = []
|
2017-06-12 20:16:42 +00:00
|
|
|
|
2017-06-14 18:51:14 +00:00
|
|
|
// Wait for nuxt be ready
|
2017-06-15 14:59:26 +00:00
|
|
|
await this.nuxt.ready()
|
2017-06-12 20:16:42 +00:00
|
|
|
|
2017-06-18 15:44:01 +00:00
|
|
|
// Start build process
|
2017-07-04 22:26:01 +00:00
|
|
|
if (this.builder && build) {
|
2017-06-18 15:44:01 +00:00
|
|
|
await this.builder.build()
|
|
|
|
}
|
|
|
|
|
2017-07-17 19:26:41 +00:00
|
|
|
await this.nuxt.applyPluginsAsync('generate', this)
|
2017-06-12 20:16:42 +00:00
|
|
|
|
2017-07-04 22:26:01 +00:00
|
|
|
// Initialize dist directory
|
|
|
|
if (init) {
|
|
|
|
await this.initDist()
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
2017-06-12 20:16:42 +00:00
|
|
|
|
|
|
|
// Resolve config.generate.routes promises before generating the routes
|
2017-07-04 22:26:01 +00:00
|
|
|
let generateRoutes = []
|
2017-06-11 14:17:36 +00:00
|
|
|
if (this.options.router.mode !== 'hash') {
|
2017-05-14 18:21:14 +00:00
|
|
|
try {
|
2017-06-12 20:16:42 +00:00
|
|
|
console.log('Generating routes') // eslint-disable-line no-console
|
|
|
|
generateRoutes = await promisifyRoute(this.options.generate.routes || [])
|
2017-07-03 11:11:40 +00:00
|
|
|
await this.applyPluginsAsync('generateRoutes', {generator: this, generateRoutes})
|
2017-06-11 14:17:36 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error('Could not resolve routes') // eslint-disable-line no-console
|
|
|
|
console.error(e) // eslint-disable-line no-console
|
|
|
|
throw e // eslint-disable-line no-unreachable
|
2016-11-24 00:47:11 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
2017-06-12 20:16:42 +00:00
|
|
|
|
|
|
|
// Generate only index.html for router.mode = 'hash'
|
2017-06-14 16:13:43 +00:00
|
|
|
let routes = (this.options.router.mode === 'hash') ? ['/'] : flatRoutes(this.options.router.routes)
|
2017-07-04 22:26:01 +00:00
|
|
|
routes = this.decorateWithPayloads(routes, generateRoutes)
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-07-03 11:11:40 +00:00
|
|
|
await this.applyPluginsAsync('generate', {generator: this, routes})
|
|
|
|
|
2017-07-04 22:26:01 +00:00
|
|
|
// Start generate process
|
2017-06-11 14:17:36 +00:00
|
|
|
while (routes.length) {
|
|
|
|
let n = 0
|
2017-07-09 10:00:08 +00:00
|
|
|
await Promise.all(routes.splice(0, this.options.generate.concurrency).map(async ({ route, payload }) => {
|
2017-06-11 14:17:36 +00:00
|
|
|
await waitFor(n++ * this.options.generate.interval)
|
2017-07-04 22:26:01 +00:00
|
|
|
await this.generateRoute({route, payload, errors})
|
2017-06-11 14:17:36 +00:00
|
|
|
}))
|
|
|
|
}
|
2017-06-12 20:16:42 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
const duration = Math.round((Date.now() - s) / 100) / 10
|
|
|
|
debug(`HTML Files generated in ${duration}s`)
|
2017-05-11 12:06:57 +00:00
|
|
|
|
2017-06-11 14:17:36 +00:00
|
|
|
if (errors.length) {
|
|
|
|
const report = errors.map(({ type, route, error }) => {
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (type === 'unhandled') {
|
|
|
|
return `Route: '${route}'\n${error.stack}`
|
|
|
|
} else {
|
|
|
|
return `Route: '${route}' thrown an error: \n` + JSON.stringify(error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
|
|
|
|
}
|
2017-06-12 20:40:44 +00:00
|
|
|
|
2017-07-03 11:11:40 +00:00
|
|
|
await this.applyPluginsAsync('generated', this)
|
|
|
|
|
2017-06-12 20:40:44 +00:00
|
|
|
return { duration, errors }
|
2017-05-11 12:06:57 +00:00
|
|
|
}
|
2017-07-04 22:26:01 +00:00
|
|
|
|
|
|
|
async initDist () {
|
|
|
|
// Clean destination folder
|
|
|
|
await remove(this.distPath)
|
|
|
|
debug('Destination folder cleaned')
|
|
|
|
|
|
|
|
// Copy static and built files
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (fs.existsSync(this.generateRoutes)) {
|
|
|
|
await copy(this.generateRoutes, this.distPath)
|
|
|
|
}
|
|
|
|
await copy(this.srcBuiltPath, this.distNuxtPath)
|
|
|
|
|
|
|
|
// Add .nojekyll file to let Github Pages add the _nuxt/ folder
|
|
|
|
// https://help.github.com/articles/files-that-start-with-an-underscore-are-missing/
|
|
|
|
const nojekyllPath = resolve(this.distPath, '.nojekyll')
|
|
|
|
writeFile(nojekyllPath, '')
|
|
|
|
|
|
|
|
debug('Static & build files copied')
|
|
|
|
}
|
|
|
|
|
|
|
|
decorateWithPayloads (routes, generateRoutes) {
|
|
|
|
let routeMap = {}
|
|
|
|
// Fill routeMap for known routes
|
|
|
|
routes.forEach((route) => {
|
|
|
|
routeMap[route] = {
|
|
|
|
route,
|
|
|
|
payload: null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Fill routeMap with given generate.routes
|
|
|
|
generateRoutes.forEach((route) => {
|
|
|
|
// route is either a string or like {route : "/my_route/1"}
|
|
|
|
const path = _.isString(route) ? route : route.route
|
|
|
|
routeMap[path] = {
|
|
|
|
route: path,
|
|
|
|
payload: route.payload || null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return _.values(routeMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
async generateRoute ({route, payload = {}, errors = []}) {
|
|
|
|
let html
|
|
|
|
|
|
|
|
try {
|
|
|
|
const res = await this.nuxt.renderer.renderRoute(route, { _generate: true, payload })
|
|
|
|
html = res.html
|
|
|
|
if (res.error) {
|
|
|
|
errors.push({ type: 'handled', route, error: res.error })
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
/* istanbul ignore next */
|
|
|
|
return errors.push({ type: 'unhandled', route, error: err })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.generate.minify) {
|
|
|
|
try {
|
|
|
|
html = minify(html, this.options.generate.minify)
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
const minifyErr = new Error(`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`)
|
|
|
|
errors.push({ type: 'unhandled', route, error: minifyErr })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let path = join(route, sep, 'index.html') // /about -> /about/index.html
|
|
|
|
path = (path === '/404/index.html') ? '/404.html' : path // /404 -> /404.html
|
|
|
|
debug('Generate file: ' + path)
|
|
|
|
path = join(this.distPath, path)
|
|
|
|
|
|
|
|
// Make sure the sub folders are created
|
|
|
|
await mkdirp(dirname(path))
|
|
|
|
await writeFile(path, html, 'utf8')
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
2016-11-10 11:33:52 +00:00
|
|
|
}
|