2016-11-10 11:33:52 +00:00
|
|
|
'use strict'
|
|
|
|
|
2017-01-11 19:14:59 +00:00
|
|
|
import fs from 'fs-extra'
|
|
|
|
import pify from 'pify'
|
|
|
|
import _ from 'lodash'
|
|
|
|
import { resolve, join, dirname, sep } from 'path'
|
2017-04-25 13:00:42 +00:00
|
|
|
import { isUrl, promisifyRoute, waitFor } from './utils'
|
2017-01-11 19:14:59 +00:00
|
|
|
import { minify } from 'html-minifier'
|
2017-03-20 16:52:35 +00:00
|
|
|
const debug = require('debug')('nuxt:generate')
|
2016-11-10 12:24:20 +00:00
|
|
|
const copy = pify(fs.copy)
|
|
|
|
const remove = pify(fs.remove)
|
|
|
|
const writeFile = pify(fs.writeFile)
|
2016-11-10 13:51:40 +00:00
|
|
|
const mkdirp = pify(fs.mkdirp)
|
2016-11-10 11:33:52 +00:00
|
|
|
|
|
|
|
const defaults = {
|
|
|
|
dir: 'dist',
|
2017-03-24 00:25:41 +00:00
|
|
|
routes: [],
|
2017-04-25 13:00:42 +00:00
|
|
|
interval: 0,
|
2017-03-24 00:25:41 +00:00
|
|
|
minify: {
|
|
|
|
collapseBooleanAttributes: true,
|
|
|
|
collapseWhitespace: true,
|
|
|
|
decodeEntities: true,
|
|
|
|
minifyCSS: true,
|
|
|
|
minifyJS: true,
|
|
|
|
processConditionalComments: true,
|
|
|
|
removeAttributeQuotes: false,
|
|
|
|
removeComments: false,
|
|
|
|
removeEmptyAttributes: true,
|
|
|
|
removeOptionalTags: true,
|
|
|
|
removeRedundantAttributes: true,
|
|
|
|
removeScriptTypeAttributes: false,
|
|
|
|
removeStyleLinkTypeAttributes: false,
|
|
|
|
removeTagWhitespace: false,
|
|
|
|
sortAttributes: true,
|
|
|
|
sortClassName: true,
|
|
|
|
trimCustomFragments: true,
|
|
|
|
useShortDoctype: true
|
|
|
|
}
|
2016-11-10 11:33:52 +00:00
|
|
|
}
|
|
|
|
|
2017-05-11 12:06:57 +00:00
|
|
|
export default async function () {
|
2016-12-08 17:07:14 +00:00
|
|
|
const s = Date.now()
|
2017-04-24 20:21:11 +00:00
|
|
|
let errors = []
|
2016-11-10 18:34:59 +00:00
|
|
|
/*
|
2017-06-04 12:08:36 +00:00
|
|
|
** Wait for modules to be initialized
|
|
|
|
*/
|
|
|
|
await this.ready()
|
|
|
|
/*
|
2016-11-10 11:33:52 +00:00
|
|
|
** Set variables
|
|
|
|
*/
|
|
|
|
this.options.generate = _.defaultsDeep(this.options.generate, defaults)
|
2016-12-08 06:45:40 +00:00
|
|
|
var srcStaticPath = resolve(this.srcDir, 'static')
|
2017-05-23 23:52:48 +00:00
|
|
|
var srcBuiltPath = resolve(this.buildDir, 'dist')
|
2016-11-10 11:33:52 +00:00
|
|
|
var distPath = resolve(this.dir, this.options.generate.dir)
|
2017-04-25 09:17:12 +00:00
|
|
|
var distNuxtPath = join(distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath))
|
2017-05-11 12:06:57 +00:00
|
|
|
/*
|
|
|
|
** Launch build process
|
|
|
|
*/
|
2017-05-21 13:26:39 +00:00
|
|
|
await this.build()
|
2017-05-11 12:06:57 +00:00
|
|
|
/*
|
|
|
|
** Clean destination folder
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
await remove(distPath)
|
|
|
|
debug('Destination folder cleaned')
|
|
|
|
} catch (e) {}
|
|
|
|
/*
|
|
|
|
** Copy static and built files
|
|
|
|
*/
|
|
|
|
if (fs.existsSync(srcStaticPath)) {
|
|
|
|
await copy(srcStaticPath, distPath)
|
|
|
|
}
|
|
|
|
await copy(srcBuiltPath, distNuxtPath)
|
|
|
|
debug('Static & build files copied')
|
2017-05-17 14:20:14 +00:00
|
|
|
if (this.options.router.mode !== 'hash') {
|
2017-03-06 19:00:19 +00:00
|
|
|
// Resolve config.generate.routes promises before generating the routes
|
2017-05-17 14:19:22 +00:00
|
|
|
try {
|
|
|
|
var generateRoutes = await promisifyRoute(this.options.generate.routes || [])
|
|
|
|
} catch (e) {
|
|
|
|
console.error('Could not resolve routes') // eslint-disable-line no-console
|
|
|
|
console.error(e) // eslint-disable-line no-console
|
|
|
|
process.exit(1)
|
|
|
|
throw e // eslint-disable-line no-unreachable
|
|
|
|
}
|
2017-05-20 05:09:05 +00:00
|
|
|
}
|
|
|
|
function decorateWithPayloads (routes) {
|
2017-05-30 10:00:31 +00:00
|
|
|
let routeMap = {}
|
|
|
|
// Fill routeMap for known routes
|
|
|
|
routes.forEach((route) => {
|
|
|
|
routeMap[route] = {
|
|
|
|
route,
|
|
|
|
payload: null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Fill routeMap with given generate.routes
|
2017-05-24 09:32:17 +00:00
|
|
|
generateRoutes.forEach((route) => {
|
2017-05-30 10:00:31 +00:00
|
|
|
// 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
|
2017-05-17 14:19:22 +00:00
|
|
|
}
|
|
|
|
})
|
2017-05-20 05:09:05 +00:00
|
|
|
return _.values(routeMap)
|
2017-05-11 12:06:57 +00:00
|
|
|
}
|
|
|
|
/*
|
2017-05-17 14:19:22 +00:00
|
|
|
** Generate only index.html for router.mode = 'hash'
|
2017-05-11 12:06:57 +00:00
|
|
|
*/
|
2017-05-30 10:00:31 +00:00
|
|
|
let routes = (this.options.router.mode === 'hash') ? ['/'] : this.routes
|
|
|
|
routes = decorateWithPayloads(routes)
|
2017-05-20 05:09:05 +00:00
|
|
|
|
2017-05-14 18:21:14 +00:00
|
|
|
while (routes.length) {
|
|
|
|
let n = 0
|
2017-05-24 09:32:17 +00:00
|
|
|
await Promise.all(routes.splice(0, 500).map(async ({route, payload}) => {
|
2017-05-21 13:26:39 +00:00
|
|
|
await waitFor(n++ * this.options.generate.interval)
|
|
|
|
let html
|
2017-05-14 18:21:14 +00:00
|
|
|
try {
|
2017-05-24 10:46:22 +00:00
|
|
|
const res = await this.renderRoute(route, { _generate: true, payload })
|
2017-05-21 13:26:39 +00:00
|
|
|
html = res.html
|
|
|
|
if (res.error) {
|
|
|
|
errors.push({ type: 'handled', route, error: res.error })
|
2017-05-14 18:21:14 +00:00
|
|
|
}
|
2017-05-14 23:17:13 +00:00
|
|
|
} catch (err) {
|
|
|
|
/* istanbul ignore next */
|
2017-05-30 10:00:31 +00:00
|
|
|
return errors.push({ type: 'unhandled', route, error: err })
|
2016-11-24 00:47:11 +00:00
|
|
|
}
|
2017-05-21 13:26:39 +00:00
|
|
|
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 })
|
|
|
|
}
|
2017-05-14 18:21:14 +00:00
|
|
|
}
|
2017-05-21 13:26:39 +00:00
|
|
|
let path = join(route, sep, 'index.html') // /about -> /about/index.html
|
2017-05-14 18:21:14 +00:00
|
|
|
debug('Generate file: ' + path)
|
|
|
|
path = join(distPath, path)
|
|
|
|
// Make sure the sub folders are created
|
|
|
|
await mkdirp(dirname(path))
|
2017-05-21 13:26:39 +00:00
|
|
|
await writeFile(path, html, 'utf8')
|
2017-05-14 18:21:14 +00:00
|
|
|
}))
|
2017-05-11 12:06:57 +00:00
|
|
|
}
|
|
|
|
// 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(distPath, '.nojekyll')
|
|
|
|
writeFile(nojekyllPath, '')
|
|
|
|
const duration = Math.round((Date.now() - s) / 100) / 10
|
|
|
|
debug(`HTML Files generated in ${duration}s`)
|
|
|
|
|
|
|
|
if (errors.length) {
|
|
|
|
const report = errors.map(({ type, route, error }) => {
|
2017-05-14 23:01:41 +00:00
|
|
|
/* istanbul ignore if */
|
2017-05-11 12:06:57 +00:00
|
|
|
if (type === 'unhandled') {
|
|
|
|
return `Route: '${route}'\n${error.stack}`
|
|
|
|
} else {
|
|
|
|
return `Route: '${route}' thrown an error: \n` + JSON.stringify(error)
|
2016-12-08 17:07:14 +00:00
|
|
|
}
|
2016-11-10 11:33:52 +00:00
|
|
|
})
|
2017-05-11 12:06:57 +00:00
|
|
|
console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
|
|
|
|
}
|
2017-06-12 17:36:47 +00:00
|
|
|
return { duration, errors }
|
2016-11-10 11:33:52 +00:00
|
|
|
}
|