Update generator

This commit is contained in:
Pooya Parsa 2017-06-13 00:46:42 +04:30
parent a14819ca88
commit 344e4a159e

View File

@ -12,32 +12,6 @@ const remove = pify(fs.remove)
const writeFile = pify(fs.writeFile)
const mkdirp = pify(fs.mkdirp)
const defaults = {
dir: 'dist',
routes: [],
interval: 0,
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
}
}
export default class Generator extends Tapable {
constructor (nuxt) {
super()
@ -48,42 +22,36 @@ export default class Generator extends Tapable {
async generate () {
const s = Date.now()
let errors = []
/*
** Wait for modules to be initialized
*/
await this.ready()
/*
** Set variables
*/
this.options.generate = _.defaultsDeep(this.options.generate, defaults)
let generateRoutes = []
// Wait for nuxt.js to be ready
await this.nuxt._ready
// Set variables
let srcStaticPath = resolve(this.options.srcDir, 'static')
let srcBuiltPath = resolve(this.buildDir, 'dist')
let srcBuiltPath = resolve(this.options.buildDir, 'dist')
let distPath = resolve(this.options.rootDir, this.options.generate.dir)
let distNuxtPath = join(distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath))
/*
** Launch build process
*/
await this.build()
/*
** Clean destination folder
*/
try {
await remove(distPath)
debug('Destination folder cleaned')
} catch (e) {
}
/*
** Copy static and built files
*/
// Launch build process
await this.nuxt.builder.build()
// Clean destination folder
await remove(distPath)
debug('Destination folder cleaned')
// Copy static and built files
if (fs.existsSync(srcStaticPath)) {
await copy(srcStaticPath, distPath)
}
await copy(srcBuiltPath, distNuxtPath)
debug('Static & build files copied')
// Resolve config.generate.routes promises before generating the routes
if (this.options.router.mode !== 'hash') {
// Resolve config.generate.routes promises before generating the routes
try {
let generateRoutes = await promisifyRoute(this.options.generate.routes || [])
console.log('Generating routes') // eslint-disable-line no-console
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
@ -91,7 +59,8 @@ export default class Generator extends Tapable {
throw e // eslint-disable-line no-unreachable
}
}
function decorateWithPayloads (routes) {
const decorateWithPayloads = (routes) => {
let routeMap = {}
// Fill routeMap for known routes
routes.forEach((route) => {
@ -112,10 +81,8 @@ export default class Generator extends Tapable {
return _.values(routeMap)
}
/*
** Generate only index.html for router.mode = 'hash'
*/
let routes = (this.options.router.mode === 'hash') ? ['/'] : this.routes
// Generate only index.html for router.mode = 'hash'
let routes = (this.options.router.mode === 'hash') ? ['/'] : this.nuxt.builder.routes
routes = decorateWithPayloads(routes)
while (routes.length) {
@ -124,7 +91,7 @@ export default class Generator extends Tapable {
await waitFor(n++ * this.options.generate.interval)
let html
try {
const res = await this.renderRoute(route, { _generate: true, payload })
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 })
@ -149,6 +116,7 @@ export default class Generator extends Tapable {
await writeFile(path, html, 'utf8')
}))
}
// 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')
@ -168,6 +136,4 @@ export default class Generator extends Tapable {
console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
}
}
}