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 writeFile = pify(fs.writeFile)
const mkdirp = pify(fs.mkdirp) 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 { export default class Generator extends Tapable {
constructor (nuxt) { constructor (nuxt) {
super() super()
@ -48,42 +22,36 @@ export default class Generator extends Tapable {
async generate () { async generate () {
const s = Date.now() const s = Date.now()
let errors = [] let errors = []
/* let generateRoutes = []
** Wait for modules to be initialized
*/ // Wait for nuxt.js to be ready
await this.ready() await this.nuxt._ready
/*
** Set variables // Set variables
*/
this.options.generate = _.defaultsDeep(this.options.generate, defaults)
let srcStaticPath = resolve(this.options.srcDir, 'static') 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 distPath = resolve(this.options.rootDir, this.options.generate.dir)
let distNuxtPath = join(distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath)) let distNuxtPath = join(distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath))
/*
** Launch build process // Launch build process
*/ await this.nuxt.builder.build()
await this.build()
/* // Clean destination folder
** Clean destination folder
*/
try {
await remove(distPath) await remove(distPath)
debug('Destination folder cleaned') debug('Destination folder cleaned')
} catch (e) {
} // Copy static and built files
/*
** Copy static and built files
*/
if (fs.existsSync(srcStaticPath)) { if (fs.existsSync(srcStaticPath)) {
await copy(srcStaticPath, distPath) await copy(srcStaticPath, distPath)
} }
await copy(srcBuiltPath, distNuxtPath) await copy(srcBuiltPath, distNuxtPath)
debug('Static & build files copied') debug('Static & build files copied')
if (this.options.router.mode !== 'hash') {
// Resolve config.generate.routes promises before generating the routes // Resolve config.generate.routes promises before generating the routes
if (this.options.router.mode !== 'hash') {
try { 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) { } catch (e) {
console.error('Could not resolve routes') // eslint-disable-line no-console console.error('Could not resolve routes') // eslint-disable-line no-console
console.error(e) // 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 throw e // eslint-disable-line no-unreachable
} }
} }
function decorateWithPayloads (routes) {
const decorateWithPayloads = (routes) => {
let routeMap = {} let routeMap = {}
// Fill routeMap for known routes // Fill routeMap for known routes
routes.forEach((route) => { routes.forEach((route) => {
@ -112,10 +81,8 @@ export default class Generator extends Tapable {
return _.values(routeMap) return _.values(routeMap)
} }
/* // Generate only index.html for router.mode = 'hash'
** Generate only index.html for router.mode = 'hash' let routes = (this.options.router.mode === 'hash') ? ['/'] : this.nuxt.builder.routes
*/
let routes = (this.options.router.mode === 'hash') ? ['/'] : this.routes
routes = decorateWithPayloads(routes) routes = decorateWithPayloads(routes)
while (routes.length) { while (routes.length) {
@ -124,7 +91,7 @@ export default class Generator extends Tapable {
await waitFor(n++ * this.options.generate.interval) await waitFor(n++ * this.options.generate.interval)
let html let html
try { try {
const res = await this.renderRoute(route, { _generate: true, payload }) const res = await this.nuxt.renderer.renderRoute(route, { _generate: true, payload })
html = res.html html = res.html
if (res.error) { if (res.error) {
errors.push({ type: 'handled', route, error: 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') await writeFile(path, html, 'utf8')
})) }))
} }
// Add .nojekyll file to let Github Pages add the _nuxt/ folder // Add .nojekyll file to let Github Pages add the _nuxt/ folder
// https://help.github.com/articles/files-that-start-with-an-underscore-are-missing/ // https://help.github.com/articles/files-that-start-with-an-underscore-are-missing/
const nojekyllPath = resolve(distPath, '.nojekyll') 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 console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
} }
} }
} }