2018-03-16 19:11:24 +00:00
|
|
|
import path from 'path'
|
2018-03-16 19:52:17 +00:00
|
|
|
import _ from 'lodash'
|
2018-03-16 19:11:24 +00:00
|
|
|
import htmlMinifier from 'html-minifier'
|
2018-03-16 16:12:06 +00:00
|
|
|
import Chalk from 'chalk'
|
2018-03-16 19:11:24 +00:00
|
|
|
import fsExtra from 'fs-extra'
|
2018-03-16 17:02:56 +00:00
|
|
|
import { isUrl, promisifyRoute, waitFor, flatRoutes, printWarn, createSpinner } from '../common/utils'
|
2018-01-11 18:20:35 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export default class Generator {
|
2017-10-30 17:41:22 +00:00
|
|
|
constructor(nuxt, builder) {
|
2017-06-11 14:17:36 +00:00
|
|
|
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
|
2018-03-16 19:11:24 +00:00
|
|
|
this.staticRoutes = path.resolve(this.options.srcDir, this.options.dir.static)
|
|
|
|
this.srcBuiltPath = path.resolve(this.options.buildDir, 'dist')
|
|
|
|
this.distPath = path.resolve(this.options.rootDir, this.options.generate.dir)
|
|
|
|
this.distNuxtPath = path.join(
|
2018-01-11 18:20:35 +00:00
|
|
|
this.distPath,
|
|
|
|
isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath
|
|
|
|
)
|
2018-03-13 17:16:12 +00:00
|
|
|
|
2018-03-16 06:26:23 +00:00
|
|
|
this.spinner = createSpinner()
|
2017-05-11 12:06:57 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
async generate({ build = true, init = true } = {}) {
|
2018-03-13 17:16:12 +00:00
|
|
|
this.spinner.start('Initializing generator...')
|
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
await this.initiate({ build, init })
|
|
|
|
|
2018-03-13 17:16:12 +00:00
|
|
|
this.spinner.start('Preparing routes for generate...')
|
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
const routes = await this.initRoutes()
|
|
|
|
|
2018-03-13 17:16:12 +00:00
|
|
|
this.spinner.start('Generating pages...')
|
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
const errors = await this.generateRoutes(routes)
|
2018-03-13 17:16:12 +00:00
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
await this.afterGenerate()
|
|
|
|
|
2018-01-11 18:20:35 +00:00
|
|
|
// Done hook
|
2017-11-23 22:31:54 +00:00
|
|
|
await this.nuxt.callHook('generate:done', this, errors)
|
|
|
|
|
|
|
|
return { errors }
|
|
|
|
}
|
|
|
|
|
|
|
|
async initiate({ build = true, init = true } = {}) {
|
2017-10-30 21:39:08 +00:00
|
|
|
// Wait for nuxt be ready
|
|
|
|
await this.nuxt.ready()
|
|
|
|
|
|
|
|
// Call before hook
|
|
|
|
await this.nuxt.callHook('generate:before', this, this.options.generate)
|
|
|
|
|
2017-08-17 12:43:51 +00:00
|
|
|
if (build) {
|
2017-11-29 17:28:19 +00:00
|
|
|
// Add flag to set process.static
|
|
|
|
this.builder.forGenerate()
|
|
|
|
|
|
|
|
// Start build process
|
2017-06-18 15:44:01 +00:00
|
|
|
await this.builder.build()
|
|
|
|
}
|
|
|
|
|
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-11-23 22:31:54 +00:00
|
|
|
}
|
2017-06-12 20:16:42 +00:00
|
|
|
|
2017-11-24 17:02:53 +00:00
|
|
|
async initRoutes(...args) {
|
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 {
|
2018-01-11 18:20:35 +00:00
|
|
|
generateRoutes = await promisifyRoute(
|
|
|
|
this.options.generate.routes || [],
|
|
|
|
...args
|
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.error('Could not resolve routes') // 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'
|
2018-01-11 18:20:35 +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-10-30 21:39:08 +00:00
|
|
|
// extendRoutes hook
|
|
|
|
await this.nuxt.callHook('generate:extendRoutes', routes)
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
|
|
|
async generateRoutes(routes) {
|
|
|
|
let errors = []
|
|
|
|
|
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
|
2018-01-11 18:20:35 +00:00
|
|
|
await Promise.all(
|
|
|
|
routes
|
|
|
|
.splice(0, this.options.generate.concurrency)
|
|
|
|
.map(async ({ route, payload }) => {
|
|
|
|
await waitFor(n++ * this.options.generate.interval)
|
|
|
|
await this.generateRoute({ route, payload, errors })
|
|
|
|
})
|
|
|
|
)
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
2017-06-12 20:16:42 +00:00
|
|
|
|
2018-01-11 18:20:35 +00:00
|
|
|
// Improve string representation for errors
|
|
|
|
errors.toString = () => this._formatErrors(errors)
|
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
return errors
|
|
|
|
}
|
|
|
|
|
2018-01-11 18:20:35 +00:00
|
|
|
_formatErrors(errors) {
|
|
|
|
return errors
|
|
|
|
.map(({ type, route, error }) => {
|
|
|
|
const isHandled = type === 'handled'
|
|
|
|
const bgColor = isHandled ? 'bgYellow' : 'bgRed'
|
|
|
|
const color = isHandled ? 'yellow' : 'red'
|
|
|
|
|
|
|
|
let line =
|
2018-03-16 17:02:56 +00:00
|
|
|
Chalk.black[bgColor](' GEN ERR ') + Chalk[color](` ${route}\n\n`)
|
2018-01-11 18:20:35 +00:00
|
|
|
|
|
|
|
if (isHandled) {
|
|
|
|
line += Chalk.grey(JSON.stringify(error, undefined, 2) + '\n')
|
|
|
|
} else {
|
2018-03-16 19:11:24 +00:00
|
|
|
line += Chalk.grey(error.stack)
|
2018-01-11 18:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return line
|
|
|
|
})
|
|
|
|
.join('\n')
|
|
|
|
}
|
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
async afterGenerate() {
|
2018-01-27 00:20:03 +00:00
|
|
|
let { fallback } = this.options.generate
|
|
|
|
|
|
|
|
// Disable SPA fallback if value isn't true or a string
|
|
|
|
if (fallback !== true && typeof fallback !== 'string') return
|
|
|
|
|
2018-03-16 19:11:24 +00:00
|
|
|
const fallbackPath = path.join(this.distPath, fallback)
|
2018-01-27 00:20:03 +00:00
|
|
|
|
|
|
|
// Prevent conflicts
|
2018-03-16 19:11:24 +00:00
|
|
|
if (fsExtra.existsSync(fallbackPath)) {
|
2018-01-27 00:20:03 +00:00
|
|
|
printWarn(`SPA fallback was configured, but the configured path (${fallbackPath}) already exists.`)
|
|
|
|
return
|
2017-08-18 16:31:55 +00:00
|
|
|
}
|
2018-01-27 00:20:03 +00:00
|
|
|
|
|
|
|
// Render and write the SPA template to the fallback path
|
|
|
|
const { html } = await this.nuxt.renderRoute('/', { spa: true })
|
2018-03-16 19:11:24 +00:00
|
|
|
await fsExtra.writeFile(fallbackPath, html, 'utf8')
|
2017-05-11 12:06:57 +00:00
|
|
|
}
|
2017-07-04 22:26:01 +00:00
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
async initDist() {
|
2017-07-04 22:26:01 +00:00
|
|
|
// Clean destination folder
|
2018-03-16 19:11:24 +00:00
|
|
|
await fsExtra.remove(this.distPath)
|
2017-11-23 22:31:54 +00:00
|
|
|
|
|
|
|
await this.nuxt.callHook('generate:distRemoved', this)
|
2017-07-04 22:26:01 +00:00
|
|
|
|
|
|
|
// Copy static and built files
|
|
|
|
/* istanbul ignore if */
|
2018-03-16 19:11:24 +00:00
|
|
|
if (fsExtra.existsSync(this.staticRoutes)) {
|
|
|
|
await fsExtra.copy(this.staticRoutes, this.distPath)
|
2017-07-04 22:26:01 +00:00
|
|
|
}
|
2018-03-16 19:11:24 +00:00
|
|
|
await fsExtra.copy(this.srcBuiltPath, this.distNuxtPath)
|
2017-07-04 22:26:01 +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/
|
2018-03-16 19:11:24 +00:00
|
|
|
const nojekyllPath = path.resolve(this.distPath, '.nojekyll')
|
|
|
|
fsExtra.writeFile(nojekyllPath, '')
|
2017-07-04 22:26:01 +00:00
|
|
|
|
2017-08-18 11:44:47 +00:00
|
|
|
// Cleanup SSR related files
|
|
|
|
const extraFiles = [
|
|
|
|
'index.spa.html',
|
|
|
|
'index.ssr.html',
|
|
|
|
'server-bundle.json',
|
|
|
|
'vue-ssr-client-manifest.json'
|
2018-03-16 19:11:24 +00:00
|
|
|
].map(file => path.resolve(this.distNuxtPath, file))
|
2017-08-18 11:44:47 +00:00
|
|
|
|
|
|
|
extraFiles.forEach(file => {
|
2018-03-16 19:11:24 +00:00
|
|
|
if (fsExtra.existsSync(file)) {
|
|
|
|
fsExtra.removeSync(file)
|
2017-08-18 11:44:47 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
await this.nuxt.callHook('generate:distCopied', this)
|
2017-07-04 22:26:01 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
decorateWithPayloads(routes, generateRoutes) {
|
2017-07-04 22:26:01 +00:00
|
|
|
let routeMap = {}
|
|
|
|
// Fill routeMap for known routes
|
2018-01-11 18:20:35 +00:00
|
|
|
routes.forEach(route => {
|
2017-07-04 22:26:01 +00:00
|
|
|
routeMap[route] = {
|
|
|
|
route,
|
|
|
|
payload: null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Fill routeMap with given generate.routes
|
2018-01-11 18:20:35 +00:00
|
|
|
generateRoutes.forEach(route => {
|
2017-10-30 17:41:22 +00:00
|
|
|
// route is either a string or like { route : '/my_route/1', payload: {} }
|
2017-07-04 22:26:01 +00:00
|
|
|
const path = _.isString(route) ? route : route.route
|
|
|
|
routeMap[path] = {
|
|
|
|
route: path,
|
|
|
|
payload: route.payload || null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return _.values(routeMap)
|
|
|
|
}
|
|
|
|
|
2017-10-30 17:41:22 +00:00
|
|
|
async generateRoute({ route, payload = {}, errors = [] }) {
|
2017-07-04 22:26:01 +00:00
|
|
|
let html
|
2017-11-23 22:31:54 +00:00
|
|
|
const pageErrors = []
|
2017-07-04 22:26:01 +00:00
|
|
|
|
|
|
|
try {
|
2018-01-11 18:20:35 +00:00
|
|
|
const res = await this.nuxt.renderer.renderRoute(route, {
|
|
|
|
_generate: true,
|
|
|
|
payload
|
|
|
|
})
|
2017-07-04 22:26:01 +00:00
|
|
|
html = res.html
|
|
|
|
if (res.error) {
|
2017-11-23 22:31:54 +00:00
|
|
|
pageErrors.push({ type: 'handled', route, error: res.error })
|
2017-07-04 22:26:01 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
/* istanbul ignore next */
|
2017-11-23 22:31:54 +00:00
|
|
|
pageErrors.push({ type: 'unhandled', route, error: err })
|
|
|
|
Array.prototype.push.apply(errors, pageErrors)
|
|
|
|
|
2018-01-11 18:20:35 +00:00
|
|
|
await this.nuxt.callHook('generate:routeFailed', {
|
|
|
|
route,
|
|
|
|
errors: pageErrors
|
|
|
|
})
|
2017-11-23 22:31:54 +00:00
|
|
|
|
|
|
|
return false
|
2017-07-04 22:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.options.generate.minify) {
|
|
|
|
try {
|
2018-03-16 19:11:24 +00:00
|
|
|
html = htmlMinifier.minify(html, this.options.generate.minify)
|
2017-07-04 22:26:01 +00:00
|
|
|
} catch (err) /* istanbul ignore next */ {
|
2018-01-11 18:20:35 +00:00
|
|
|
const minifyErr = new Error(
|
|
|
|
`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`
|
|
|
|
)
|
2017-11-23 22:31:54 +00:00
|
|
|
pageErrors.push({ type: 'unhandled', route, error: minifyErr })
|
2017-07-04 22:26:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 19:52:17 +00:00
|
|
|
path = 123
|
|
|
|
|
2018-03-16 19:21:55 +00:00
|
|
|
let _path
|
2017-11-06 07:36:28 +00:00
|
|
|
|
|
|
|
if (this.options.generate.subFolders) {
|
2018-03-16 19:21:55 +00:00
|
|
|
_path = path.join(route, path.sep, 'index.html') // /about -> /about/index.html
|
|
|
|
_path = _path === '/404/index.html' ? '/404.html' : _path // /404 -> /404.html
|
2017-11-06 07:36:28 +00:00
|
|
|
} else {
|
2018-03-16 19:21:55 +00:00
|
|
|
_path = route.length > 1 ? path.join(path.sep, route + '.html') : path.join(path.sep, 'index.html')
|
2017-11-06 07:36:28 +00:00
|
|
|
}
|
2017-10-30 22:14:21 +00:00
|
|
|
|
|
|
|
// Call hook to let user update the path & html
|
2018-03-16 19:21:55 +00:00
|
|
|
const page = { route, path: _path, html }
|
2017-10-30 22:14:21 +00:00
|
|
|
await this.nuxt.callHook('generate:page', page)
|
|
|
|
|
2018-03-16 19:11:24 +00:00
|
|
|
page.path = path.join(this.distPath, page.path)
|
2017-07-04 22:26:01 +00:00
|
|
|
|
|
|
|
// Make sure the sub folders are created
|
2018-03-16 19:11:24 +00:00
|
|
|
await fsExtra.mkdirp(path.dirname(page.path))
|
|
|
|
await fsExtra.writeFile(page.path, page.html, 'utf8')
|
2017-07-04 22:26:01 +00:00
|
|
|
|
2018-01-11 18:20:35 +00:00
|
|
|
await this.nuxt.callHook('generate:routeCreated', {
|
|
|
|
route,
|
|
|
|
path: page.path,
|
|
|
|
errors: pageErrors
|
|
|
|
})
|
2017-11-23 22:31:54 +00:00
|
|
|
|
|
|
|
if (pageErrors.length) {
|
2018-03-13 17:19:39 +00:00
|
|
|
this.spinner.fail('Error generating ' + route)
|
2017-11-23 22:31:54 +00:00
|
|
|
Array.prototype.push.apply(errors, pageErrors)
|
2018-03-13 17:19:39 +00:00
|
|
|
} else {
|
|
|
|
this.spinner.succeed('Generated ' + route)
|
2017-11-23 22:31:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 22:26:01 +00:00
|
|
|
return true
|
|
|
|
}
|
2016-11-10 11:33:52 +00:00
|
|
|
}
|