2018-03-16 19:11:24 +00:00
|
|
|
import path from 'path'
|
2020-05-07 19:08:01 +00:00
|
|
|
import chalk from 'chalk'
|
2018-03-31 16:22:14 +00:00
|
|
|
import consola from 'consola'
|
2020-11-25 14:19:29 +00:00
|
|
|
import devalue from 'devalue'
|
2018-10-24 16:55:18 +00:00
|
|
|
import fsExtra from 'fs-extra'
|
2020-05-27 14:51:51 +00:00
|
|
|
import defu from 'defu'
|
2018-10-24 16:55:18 +00:00
|
|
|
import htmlMinifier from 'html-minifier'
|
2020-05-07 19:08:01 +00:00
|
|
|
import { parse } from 'node-html-parser'
|
2018-10-17 21:28:25 +00:00
|
|
|
|
2020-12-22 17:07:50 +00:00
|
|
|
import { isFullStatic, flatRoutes, isString, isUrl, promisifyRoute, urlJoin, waitFor, requireModule } from '@nuxt/utils'
|
2018-03-31 16:22:14 +00:00
|
|
|
|
2018-03-16 16:12:06 +00:00
|
|
|
export default class Generator {
|
2019-07-10 10:45:49 +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
|
2020-07-16 15:10:54 +00:00
|
|
|
this.isFullStatic = isFullStatic(this.options)
|
2020-10-15 16:12:06 +00:00
|
|
|
if (this.isFullStatic) {
|
|
|
|
consola.info(`Full static generation activated`)
|
|
|
|
}
|
2018-03-16 19:11:24 +00:00
|
|
|
this.staticRoutes = path.resolve(this.options.srcDir, this.options.dir.static)
|
2018-08-20 16:04:55 +00:00
|
|
|
this.srcBuiltPath = path.resolve(this.options.buildDir, 'dist', 'client')
|
2018-09-14 07:06:44 +00:00
|
|
|
this.distPath = this.options.generate.dir
|
2018-03-16 19:11:24 +00:00
|
|
|
this.distNuxtPath = path.join(
|
2018-01-11 18:20:35 +00:00
|
|
|
this.distPath,
|
|
|
|
isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath
|
|
|
|
)
|
2020-07-16 15:10:54 +00:00
|
|
|
// Payloads for full static
|
|
|
|
if (this.isFullStatic) {
|
2020-11-25 14:19:29 +00:00
|
|
|
const { staticAssets, manifest } = this.options.generate
|
2020-07-16 15:10:54 +00:00
|
|
|
this.staticAssetsDir = path.resolve(this.distNuxtPath, staticAssets.dir, staticAssets.version)
|
2020-12-22 17:15:59 +00:00
|
|
|
this.staticAssetsBase = urlJoin(this.options.app.cdnURL || '/', this.options.generate.staticAssets.versionBase)
|
2020-11-25 14:19:29 +00:00
|
|
|
if (manifest) {
|
|
|
|
this.manifest = defu(manifest, {
|
|
|
|
routes: []
|
|
|
|
})
|
|
|
|
}
|
2020-07-16 15:10:54 +00:00
|
|
|
}
|
2020-05-27 14:51:51 +00:00
|
|
|
|
|
|
|
// Shared payload
|
|
|
|
this._payload = null
|
|
|
|
this.setPayload = (payload) => {
|
|
|
|
this._payload = defu(payload, this._payload)
|
|
|
|
}
|
2017-05-11 12:06:57 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async generate ({ build = true, init = true } = {}) {
|
2018-04-01 20:20:46 +00:00
|
|
|
consola.debug('Initializing generator...')
|
2017-11-23 22:31:54 +00:00
|
|
|
await this.initiate({ build, init })
|
|
|
|
|
2020-05-07 19:08:01 +00:00
|
|
|
consola.debug('Preparing routes for generate...')
|
2017-11-23 22:31:54 +00:00
|
|
|
const routes = await this.initRoutes()
|
|
|
|
|
2020-07-16 15:10:54 +00:00
|
|
|
consola.info('Generating pages' + (this.isFullStatic ? ' with full static mode' : ''))
|
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()
|
|
|
|
|
2020-11-25 14:19:29 +00:00
|
|
|
// Save routes manifest for full static
|
|
|
|
if (this.manifest) {
|
|
|
|
await this.nuxt.callHook('generate:manifest', this.manifest, this)
|
|
|
|
const manifestPath = path.join(this.staticAssetsDir, 'manifest.js')
|
2020-12-11 12:34:13 +00:00
|
|
|
await fsExtra.ensureDir(this.staticAssetsDir)
|
2020-11-25 14:19:29 +00:00
|
|
|
await fsExtra.writeFile(manifestPath, `__NUXT_JSONP__("manifest.js", ${devalue(this.manifest)})`, 'utf-8')
|
|
|
|
consola.success('Static manifest generated')
|
|
|
|
}
|
|
|
|
|
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)
|
2020-05-27 14:51:51 +00:00
|
|
|
await this.nuxt.callHook('export:done', this, { errors })
|
2017-11-23 22:31:54 +00:00
|
|
|
|
|
|
|
return { errors }
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
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)
|
2020-05-27 14:51:51 +00:00
|
|
|
await this.nuxt.callHook('export:before', this)
|
2017-10-30 21:39:08 +00:00
|
|
|
|
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()
|
2020-05-07 19:08:01 +00:00
|
|
|
} else {
|
2020-05-08 14:37:42 +00:00
|
|
|
const hasBuilt = await fsExtra.exists(path.resolve(this.options.buildDir, 'dist', 'server', 'client.manifest.json'))
|
2020-05-07 19:08:01 +00:00
|
|
|
if (!hasBuilt) {
|
|
|
|
throw new Error(
|
2020-07-16 15:10:54 +00:00
|
|
|
`No build files found in ${this.srcBuiltPath}.\nPlease run \`nuxt build\``
|
2020-05-07 19:08:01 +00:00
|
|
|
)
|
|
|
|
}
|
2017-06-18 15:44:01 +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-11-23 22:31:54 +00:00
|
|
|
}
|
2017-06-12 20:16:42 +00:00
|
|
|
|
2019-07-10 10:45:49 +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 = []
|
2020-05-20 15:55:54 +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) {
|
2018-04-01 20:20:46 +00:00
|
|
|
consola.error('Could not resolve routes')
|
2017-06-11 14:17:36 +00:00
|
|
|
throw e // eslint-disable-line no-unreachable
|
2016-11-24 00:47:11 +00:00
|
|
|
}
|
2017-06-11 14:17:36 +00:00
|
|
|
}
|
2020-05-07 19:08:01 +00:00
|
|
|
let routes = []
|
|
|
|
// Generate only index.html for router.mode = 'hash' or client-side apps
|
2020-05-20 15:55:54 +00:00
|
|
|
if (this.options.router.mode === 'hash') {
|
2020-05-07 19:08:01 +00:00
|
|
|
routes = ['/']
|
|
|
|
} else {
|
2020-10-08 14:29:30 +00:00
|
|
|
try {
|
|
|
|
routes = flatRoutes(this.getAppRoutes())
|
|
|
|
} catch (err) {
|
|
|
|
// Case: where we use custom router.js
|
|
|
|
// https://github.com/nuxt-community/router-module/issues/83
|
|
|
|
routes = ['/']
|
|
|
|
}
|
2020-05-07 19:08:01 +00:00
|
|
|
}
|
|
|
|
routes = routes.filter(route => this.shouldGenerateRoute(route))
|
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)
|
2020-05-27 14:51:51 +00:00
|
|
|
await this.nuxt.callHook('export:extendRoutes', { routes })
|
2017-07-03 11:11:40 +00:00
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2020-05-07 19:08:01 +00:00
|
|
|
shouldGenerateRoute (route) {
|
|
|
|
return this.options.generate.exclude.every((regex) => {
|
|
|
|
if (typeof regex === 'string') {
|
|
|
|
return regex !== route
|
|
|
|
}
|
|
|
|
return !regex.test(route)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getBuildConfig () {
|
2020-05-08 11:55:39 +00:00
|
|
|
try {
|
2020-12-22 17:07:50 +00:00
|
|
|
return requireModule(path.join(this.options.buildDir, 'nuxt/config.json'))
|
2020-05-08 11:55:39 +00:00
|
|
|
} catch (err) {
|
|
|
|
return null
|
|
|
|
}
|
2020-05-07 19:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getAppRoutes () {
|
2020-12-22 17:07:50 +00:00
|
|
|
return requireModule(path.join(this.options.buildDir, 'routes.json'))
|
2020-05-07 19:08:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async generateRoutes (routes) {
|
2018-08-08 10:54:05 +00:00
|
|
|
const errors = []
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2020-06-16 15:53:29 +00:00
|
|
|
this.routes = []
|
|
|
|
this.generatedRoutes = new Set()
|
|
|
|
|
|
|
|
routes.forEach(({ route, ...props }) => {
|
|
|
|
route = decodeURI(route)
|
|
|
|
this.routes.push({ route, ...props })
|
|
|
|
// Add routes to the tracked generated routes (for crawler)
|
|
|
|
this.generatedRoutes.add(route)
|
|
|
|
})
|
|
|
|
|
2017-07-04 22:26:01 +00:00
|
|
|
// Start generate process
|
2020-05-07 19:08:01 +00:00
|
|
|
while (this.routes.length) {
|
2017-06-11 14:17:36 +00:00
|
|
|
let n = 0
|
2018-01-11 18:20:35 +00:00
|
|
|
await Promise.all(
|
2020-05-07 19:08:01 +00:00
|
|
|
this.routes
|
2018-01-11 18:20:35 +00:00
|
|
|
.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
|
2018-11-08 09:15:56 +00:00
|
|
|
// TODO: Use consola for more consistency
|
2018-01-11 18:20:35 +00:00
|
|
|
errors.toString = () => this._formatErrors(errors)
|
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
return errors
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
_formatErrors (errors) {
|
2018-01-11 18:20:35 +00:00
|
|
|
return errors
|
|
|
|
.map(({ type, route, error }) => {
|
|
|
|
const isHandled = type === 'handled'
|
|
|
|
const color = isHandled ? 'yellow' : 'red'
|
|
|
|
|
2020-05-07 19:08:01 +00:00
|
|
|
let line = chalk[color](` ${route}\n\n`)
|
2018-01-11 18:20:35 +00:00
|
|
|
|
|
|
|
if (isHandled) {
|
2020-05-07 19:08:01 +00:00
|
|
|
line += chalk.grey(JSON.stringify(error, undefined, 2) + '\n')
|
2018-01-11 18:20:35 +00:00
|
|
|
} else {
|
2020-05-07 19:08:01 +00:00
|
|
|
line += chalk.grey(error.stack || error.message || `${error}`)
|
2018-01-11 18:20:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return line
|
|
|
|
})
|
|
|
|
.join('\n')
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async afterGenerate () {
|
2018-08-08 10:54:05 +00:00
|
|
|
const { fallback } = this.options.generate
|
2018-01-27 00:20:03 +00:00
|
|
|
|
2018-08-23 17:33:02 +00:00
|
|
|
// Disable SPA fallback if value isn't a non-empty string
|
2018-12-09 22:00:48 +00:00
|
|
|
if (typeof fallback !== 'string' || !fallback) {
|
|
|
|
return
|
|
|
|
}
|
2018-01-27 00:20:03 +00:00
|
|
|
|
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
|
2019-01-29 10:16:45 +00:00
|
|
|
if (await fsExtra.exists(fallbackPath)) {
|
2018-04-01 20:20:46 +00:00
|
|
|
consola.warn(`SPA fallback was configured, but the configured path (${fallbackPath}) already exists.`)
|
2018-01-27 00:20:03 +00:00
|
|
|
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
|
2020-05-07 19:08:01 +00:00
|
|
|
let { html } = await this.nuxt.server.renderRoute('/', {
|
|
|
|
spa: true,
|
|
|
|
staticAssetsBase: this.staticAssetsBase
|
|
|
|
})
|
2019-05-23 09:45:22 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
html = this.minifyHtml(html)
|
|
|
|
} catch (error) {
|
2019-11-26 22:42:39 +00:00
|
|
|
consola.warn('HTML minification failed for SPA fallback')
|
2019-05-23 09:45:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 19:11:24 +00:00
|
|
|
await fsExtra.writeFile(fallbackPath, html, 'utf8')
|
2020-05-07 19:08:01 +00:00
|
|
|
consola.success('Client-side fallback created: `' + fallback + '`')
|
2017-05-11 12:06:57 +00:00
|
|
|
}
|
2017-07-04 22:26:01 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async initDist () {
|
2017-07-04 22:26:01 +00:00
|
|
|
// Clean destination folder
|
2020-05-13 14:46:13 +00:00
|
|
|
await fsExtra.emptyDir(this.distPath)
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2020-05-07 19:08:01 +00:00
|
|
|
consola.info(`Generating output directory: ${path.basename(this.distPath)}/`)
|
2017-11-23 22:31:54 +00:00
|
|
|
await this.nuxt.callHook('generate:distRemoved', this)
|
2020-06-18 15:45:06 +00:00
|
|
|
await this.nuxt.callHook('export:distRemoved', this)
|
2017-07-04 22:26:01 +00:00
|
|
|
|
|
|
|
// Copy static and built files
|
2019-01-29 10:16:45 +00:00
|
|
|
if (await fsExtra.exists(this.staticRoutes)) {
|
2018-03-16 19:11:24 +00:00
|
|
|
await fsExtra.copy(this.staticRoutes, this.distPath)
|
2017-07-04 22:26:01 +00:00
|
|
|
}
|
2020-05-07 19:08:01 +00:00
|
|
|
// Copy .nuxt/dist/client/ to dist/_nuxt/
|
2018-03-16 19:11:24 +00:00
|
|
|
await fsExtra.copy(this.srcBuiltPath, this.distNuxtPath)
|
2017-07-04 22:26:01 +00:00
|
|
|
|
2020-05-07 19:08:01 +00:00
|
|
|
if (this.payloadDir) {
|
|
|
|
await fsExtra.ensureDir(this.payloadDir)
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:07:43 +00:00
|
|
|
// Add .nojekyll file to let GitHub Pages add the _nuxt/ folder
|
2017-07-04 22:26:01 +00:00
|
|
|
// 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')
|
2020-11-25 14:19:29 +00:00
|
|
|
await fsExtra.writeFile(nojekyllPath, '')
|
2017-07-04 22:26:01 +00:00
|
|
|
|
2017-11-23 22:31:54 +00:00
|
|
|
await this.nuxt.callHook('generate:distCopied', this)
|
2020-05-27 14:51:51 +00:00
|
|
|
await this.nuxt.callHook('export:distCopied', this)
|
2017-07-04 22:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
decorateWithPayloads (routes, generateRoutes) {
|
2018-08-08 10:54:05 +00:00
|
|
|
const routeMap = {}
|
2017-07-04 22:26:01 +00:00
|
|
|
// Fill routeMap for known routes
|
2018-08-06 00:12:44 +00:00
|
|
|
routes.forEach((route) => {
|
|
|
|
routeMap[route] = { route, payload: null }
|
2017-07-04 22:26:01 +00:00
|
|
|
})
|
|
|
|
// Fill routeMap with given generate.routes
|
2018-08-06 00:12:44 +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: {} }
|
2018-10-12 22:31:19 +00:00
|
|
|
const path = isString(route) ? route : route.route
|
2017-07-04 22:26:01 +00:00
|
|
|
routeMap[path] = {
|
|
|
|
route: path,
|
|
|
|
payload: route.payload || null
|
|
|
|
}
|
|
|
|
})
|
2018-10-12 22:31:19 +00:00
|
|
|
return Object.values(routeMap)
|
2017-07-04 22:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +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
|
|
|
|
2020-11-25 14:24:13 +00:00
|
|
|
if (this.options.router && this.options.router.trailingSlash && route[route.length - 1] !== '/') {
|
|
|
|
route = route + '/'
|
|
|
|
}
|
|
|
|
|
2020-05-27 14:51:51 +00:00
|
|
|
const setPayload = (_payload) => {
|
|
|
|
payload = defu(_payload, payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply shared payload
|
|
|
|
if (this._payload) {
|
|
|
|
payload = defu(payload, this._payload)
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.nuxt.callHook('generate:route', { route, setPayload })
|
|
|
|
await this.nuxt.callHook('export:route', { route, setPayload })
|
|
|
|
|
2017-07-04 22:26:01 +00:00
|
|
|
try {
|
2020-05-07 19:08:01 +00:00
|
|
|
const renderContext = {
|
|
|
|
payload,
|
|
|
|
staticAssetsBase: this.staticAssetsBase
|
|
|
|
}
|
|
|
|
const res = await this.nuxt.server.renderRoute(route, renderContext)
|
|
|
|
html = res.html
|
|
|
|
|
|
|
|
// If crawler activated and called from generateRoutes()
|
|
|
|
if (this.options.generate.crawler && this.options.render.ssr) {
|
2020-05-28 16:26:27 +00:00
|
|
|
const possibleTrailingSlash = this.options.router.trailingSlash ? '/' : ''
|
2020-05-07 19:08:01 +00:00
|
|
|
parse(html).querySelectorAll('a').map((el) => {
|
2020-05-28 16:26:27 +00:00
|
|
|
const sanitizedHref = (el.getAttribute('href') || '')
|
2020-06-10 07:51:29 +00:00
|
|
|
.replace(this.options.router.base, '/')
|
2020-05-28 16:26:27 +00:00
|
|
|
.split('?')[0]
|
|
|
|
.split('#')[0]
|
2020-07-27 09:01:24 +00:00
|
|
|
.replace(/\/+$/, '')
|
2020-05-28 16:26:27 +00:00
|
|
|
.trim()
|
|
|
|
|
2020-11-25 14:19:29 +00:00
|
|
|
const foundRoute = decodeURI(sanitizedHref + possibleTrailingSlash)
|
2020-05-07 19:08:01 +00:00
|
|
|
|
2020-11-25 14:19:29 +00:00
|
|
|
if (foundRoute.startsWith('/') && !foundRoute.startsWith('//') && !path.extname(foundRoute) && this.shouldGenerateRoute(foundRoute) && !this.generatedRoutes.has(foundRoute)) {
|
|
|
|
this.generatedRoutes.add(foundRoute)
|
|
|
|
this.routes.push({ route: foundRoute })
|
2020-05-07 19:08:01 +00:00
|
|
|
}
|
2020-11-14 17:10:53 +00:00
|
|
|
return null
|
2020-05-07 19:08:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save Static Assets
|
|
|
|
if (this.staticAssetsDir && renderContext.staticAssets) {
|
|
|
|
for (const asset of renderContext.staticAssets) {
|
|
|
|
const assetPath = path.join(this.staticAssetsDir, asset.path)
|
|
|
|
await fsExtra.ensureDir(path.dirname(assetPath))
|
|
|
|
await fsExtra.writeFile(assetPath, asset.src, 'utf-8')
|
|
|
|
}
|
2020-11-25 14:19:29 +00:00
|
|
|
// Add route to manifest (only if no error and redirect)
|
|
|
|
if (this.manifest && (!res.error && !res.redirected)) {
|
|
|
|
this.manifest.routes.push(route)
|
|
|
|
}
|
2020-05-07 19:08:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 14:19:29 +00:00
|
|
|
// SPA fallback
|
2017-07-04 22:26:01 +00:00
|
|
|
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) {
|
2017-11-23 22:31:54 +00:00
|
|
|
pageErrors.push({ type: 'unhandled', route, error: err })
|
2019-02-08 16:25:11 +00:00
|
|
|
errors.push(...pageErrors)
|
2017-11-23 22:31:54 +00:00
|
|
|
|
2020-05-27 14:51:51 +00:00
|
|
|
await this.nuxt.callHook('generate:routeFailed', { route, errors: pageErrors })
|
|
|
|
await this.nuxt.callHook('export:routeFailed', { route, errors: pageErrors })
|
2018-09-19 10:39:47 +00:00
|
|
|
consola.error(this._formatErrors(pageErrors))
|
2017-11-23 22:31:54 +00:00
|
|
|
|
|
|
|
return false
|
2017-07-04 22:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-05-23 09:45:22 +00:00
|
|
|
try {
|
|
|
|
html = this.minifyHtml(html)
|
|
|
|
} catch (err) {
|
|
|
|
const minifyErr = new Error(
|
|
|
|
`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`
|
|
|
|
)
|
|
|
|
pageErrors.push({ type: 'unhandled', route, error: minifyErr })
|
2017-07-04 22:26:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 18:50:07 +00:00
|
|
|
let fileName
|
2017-11-06 07:36:28 +00:00
|
|
|
|
|
|
|
if (this.options.generate.subFolders) {
|
2020-12-22 17:07:50 +00:00
|
|
|
fileName = route === '/404'
|
|
|
|
? path.join(path.sep, '404.html') // /404 -> /404.html
|
|
|
|
: path.join(route, path.sep, 'index.html') // /about -> /about/index.html
|
2017-11-06 07:36:28 +00:00
|
|
|
} else {
|
2019-10-08 08:47:13 +00:00
|
|
|
const normalizedRoute = route.replace(/\/$/, '')
|
|
|
|
fileName = route.length > 1 ? path.join(path.sep, normalizedRoute + '.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
|
2020-07-16 15:10:54 +00:00
|
|
|
const page = {
|
|
|
|
route,
|
|
|
|
path: fileName,
|
|
|
|
html,
|
|
|
|
exclude: false,
|
|
|
|
errors: pageErrors
|
|
|
|
}
|
|
|
|
page.page = page // Backward compatibility for export:page hook
|
2017-10-30 22:14:21 +00:00
|
|
|
await this.nuxt.callHook('generate:page', page)
|
|
|
|
|
2020-06-03 19:35:28 +00:00
|
|
|
if (page.exclude) {
|
|
|
|
return false
|
|
|
|
}
|
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
|
|
|
|
2020-05-27 14:51:51 +00:00
|
|
|
await this.nuxt.callHook('generate:routeCreated', { route, path: page.path, errors: pageErrors })
|
|
|
|
await this.nuxt.callHook('export:routeCreated', { route, path: page.path, errors: pageErrors })
|
2017-11-23 22:31:54 +00:00
|
|
|
|
|
|
|
if (pageErrors.length) {
|
2020-06-16 15:53:29 +00:00
|
|
|
consola.error(`Error generating route "${route}": ${pageErrors.map(e => e.error.message).join(', ')}`)
|
2019-02-08 16:25:11 +00:00
|
|
|
errors.push(...pageErrors)
|
2018-03-13 17:19:39 +00:00
|
|
|
} else {
|
2020-06-16 15:53:29 +00:00
|
|
|
consola.success(`Generated route "${route}"`)
|
2017-11-23 22:31:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 22:26:01 +00:00
|
|
|
return true
|
|
|
|
}
|
2019-05-23 09:45:22 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
minifyHtml (html) {
|
2019-05-23 09:45:22 +00:00
|
|
|
let minificationOptions = this.options.build.html.minify
|
|
|
|
|
|
|
|
// Legacy: Override minification options with generate.minify if present
|
|
|
|
// TODO: Remove in Nuxt version 3
|
|
|
|
if (typeof this.options.generate.minify !== 'undefined') {
|
|
|
|
minificationOptions = this.options.generate.minify
|
|
|
|
consola.warn('generate.minify has been deprecated and will be removed in the next major version.' +
|
|
|
|
' Use build.html.minify instead!')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!minificationOptions) {
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
|
|
|
return htmlMinifier.minify(html, minificationOptions)
|
|
|
|
}
|
2016-11-10 11:33:52 +00:00
|
|
|
}
|