mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
commit
179a806a36
3
bin/nuxt
3
bin/nuxt
@ -8,7 +8,8 @@ const commands = new Set([
|
|||||||
defaultCommand,
|
defaultCommand,
|
||||||
'init',
|
'init',
|
||||||
'build',
|
'build',
|
||||||
'start'
|
'start',
|
||||||
|
'generate'
|
||||||
])
|
])
|
||||||
|
|
||||||
let cmd = process.argv[2]
|
let cmd = process.argv[2]
|
||||||
|
29
bin/nuxt-generate
Executable file
29
bin/nuxt-generate
Executable 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
66
lib/generate.js
Normal 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')
|
||||||
|
})
|
||||||
|
}
|
@ -8,6 +8,7 @@ const pify = require('pify')
|
|||||||
const ansiHTML = require('ansi-html')
|
const ansiHTML = require('ansi-html')
|
||||||
const serialize = require('serialize-javascript')
|
const serialize = require('serialize-javascript')
|
||||||
const build = require('./build')
|
const build = require('./build')
|
||||||
|
const generate = require('./generate')
|
||||||
const serveStatic = require('serve-static')
|
const serveStatic = require('serve-static')
|
||||||
const { resolve, join } = require('path')
|
const { resolve, join } = require('path')
|
||||||
const { encodeHtml, getContext, setAnsiColors } = require('./utils')
|
const { encodeHtml, getContext, setAnsiColors } = require('./utils')
|
||||||
@ -51,6 +52,8 @@ class Nuxt {
|
|||||||
this.serveStaticNuxt = pify(serveStatic(resolve(this.dir, '.nuxt', 'dist')))
|
this.serveStaticNuxt = pify(serveStatic(resolve(this.dir, '.nuxt', 'dist')))
|
||||||
// Add this.build
|
// Add this.build
|
||||||
this.build = build.bind(this)
|
this.build = build.bind(this)
|
||||||
|
// Add this.generate
|
||||||
|
this.generate = generate.bind(this)
|
||||||
// Launch build and set this.renderer
|
// Launch build and set this.renderer
|
||||||
return co(this.build)
|
return co(this.build)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
"es6-promise": "^4.0.5",
|
"es6-promise": "^4.0.5",
|
||||||
"extract-text-webpack-plugin": "2.0.0-beta.4",
|
"extract-text-webpack-plugin": "2.0.0-beta.4",
|
||||||
"file-loader": "^0.9.0",
|
"file-loader": "^0.9.0",
|
||||||
|
"fs-extra": "^1.0.0",
|
||||||
"glob-promise": "^1.0.6",
|
"glob-promise": "^1.0.6",
|
||||||
"hash-sum": "^1.0.2",
|
"hash-sum": "^1.0.2",
|
||||||
"lodash": "^4.16.6",
|
"lodash": "^4.16.6",
|
||||||
|
Loading…
Reference in New Issue
Block a user