Merge pull request #3 from alexchopin/master

Add nuxt-generate
This commit is contained in:
Sébastien Chopin 2016-11-10 14:27:39 +01:00 committed by GitHub
commit 179a806a36
5 changed files with 101 additions and 1 deletions

View File

@ -8,7 +8,8 @@ const commands = new Set([
defaultCommand,
'init',
'build',
'start'
'start',
'generate'
])
let cmd = process.argv[2]

29
bin/nuxt-generate Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env node
const fs = require('fs')
const Nuxt = require('../')
const { resolve } = require('path')
const rootDir = resolve(process.argv.slice(2)[0] || '.')
const nuxtConfigFile = resolve(rootDir, 'nuxt.config.js')
let options = {}
if (fs.existsSync(nuxtConfigFile)) {
options = require(nuxtConfigFile)
}
if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
}
options.build = true // Enable building
options.dev = false // Force production mode (no webpack middlewares called)
console.log('[nuxt] Generating...')
new Nuxt(options)
.then((nuxt) => nuxt.generate())
.then(() => {
console.log('[nuxt] Generate done')
})
.catch((err) => {
console.error(err)
process.exit()
})

66
lib/generate.js Normal file
View File

@ -0,0 +1,66 @@
'use strict'
const fs = require('fs-extra')
const co = require('co')
const pify = require('pify')
const debug = require('debug')('nuxt:generate')
const _ = require('lodash')
const { resolve, join } = require('path')
const copy = pify(fs.copy)
const remove = pify(fs.remove)
const writeFile = pify(fs.writeFile)
const defaults = {
dir: 'dist',
routeParams: {}
}
module.exports = function () {
/*
** Set variables
*/
this.options.generate = _.defaultsDeep(this.options.generate, defaults)
var srcStaticPath = resolve(this.dir, 'static')
var srcBuiltPath = resolve(this.dir, '.nuxt', 'dist')
var distPath = resolve(this.dir, this.options.generate.dir)
var distNuxtPath = resolve(distPath, '_nuxt')
co(function * () {
/*
** Clean destination folder
*/
try {
yield remove(distPath)
debug('Destination folder cleaned')
} catch (e) {}
/*
** Copy static and built files
*/
yield [
copy(srcStaticPath, distPath),
copy(srcBuiltPath, distNuxtPath)
]
debug('Static & build files copied')
})
.then(() => {
/*
** Generate html files from routes
*/
var promises = []
this.options.routes.forEach((route) => {
var promise = this.renderRoute(route.path)
.then(({ html }) => {
var path = route.path
path += path[path.length - 1] === '/' ? 'index.html' : '.html'
debug('Generate file : ' + path)
path = join(distPath, path)
return writeFile(path, html, 'utf8')
})
console.log(promise)
promises.push(promise)
})
return Promise.all(promises)
})
.then((pages) => {
debug('HTML Files generated')
})
}

View File

@ -8,6 +8,7 @@ const pify = require('pify')
const ansiHTML = require('ansi-html')
const serialize = require('serialize-javascript')
const build = require('./build')
const generate = require('./generate')
const serveStatic = require('serve-static')
const { resolve, join } = require('path')
const { encodeHtml, getContext, setAnsiColors } = require('./utils')
@ -51,6 +52,8 @@ class Nuxt {
this.serveStaticNuxt = pify(serveStatic(resolve(this.dir, '.nuxt', 'dist')))
// Add this.build
this.build = build.bind(this)
// Add this.generate
this.generate = generate.bind(this)
// Launch build and set this.renderer
return co(this.build)
.then(() => {

View File

@ -32,6 +32,7 @@
"es6-promise": "^4.0.5",
"extract-text-webpack-plugin": "2.0.0-beta.4",
"file-loader": "^0.9.0",
"fs-extra": "^1.0.0",
"glob-promise": "^1.0.6",
"hash-sum": "^1.0.2",
"lodash": "^4.16.6",