generate with child process

This commit is contained in:
ausir 2017-05-22 03:42:42 +08:00
parent 1b0a476f14
commit e49424fb6b

View File

@ -11,6 +11,8 @@ const copy = pify(fs.copy)
const remove = pify(fs.remove)
const writeFile = pify(fs.writeFile)
const mkdirp = pify(fs.mkdirp)
const numWorkers = require('os').cpus().length
const childProcess = require('child_process')
const defaults = {
dir: 'dist',
@ -39,6 +41,7 @@ const defaults = {
}
export default async function () {
const child = process.argv[3]
const s = Date.now()
let errors = []
/*
@ -49,6 +52,7 @@ export default async function () {
var srcBuiltPath = resolve(this.dir, '.nuxt', 'dist')
var distPath = resolve(this.dir, this.options.generate.dir)
var distNuxtPath = join(distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath))
if (!child) {
/*
** Launch build process
*/
@ -68,6 +72,7 @@ export default async function () {
}
await copy(srcBuiltPath, distNuxtPath)
debug('Static & build files copied')
}
if (this.options.router.mode !== 'hash') {
// Resolve config.generate.routes promises before generating the routes
try {
@ -82,6 +87,7 @@ export default async function () {
** Generate html files from routes
*/
generateRoutes.forEach((route) => {
this.routes || (this.routes = [])
if (this.routes.indexOf(route) < 0) {
this.routes.push(route)
}
@ -91,6 +97,26 @@ export default async function () {
** Generate only index.html for router.mode = 'hash'
*/
let routes = (this.options.router.mode === 'hash') ? ['/'] : this.routes
const prmiseArray = []
if (!child) {
const workers = routes.length < numWorkers ? routes.length : numWorkers
for (let i = 0; i < workers; i++) {
prmiseArray.push(new Promise((resolve) => {
childProcess.exec(`node ./bin/nuxt generate . child-${i}`, (err, stdout, stderr) => {
if (err) {
debug(`child-${i} has error ${err}`)
} else {
debug(`child-${i} generate success`)
}
resolve(`child-${i} generate finish`)
})
}))
}
} else {
const start = parseInt(child.split('-')[1])
const quantity = Math.ceil(routes.length / (numWorkers - 1))
routes = routes.splice(start * quantity, quantity)
while (routes.length) {
let n = 0
await Promise.all(routes.splice(0, 500).map(async (route) => {
@ -122,6 +148,8 @@ export default async function () {
await writeFile(path, html, 'utf8')
}))
}
}
await Promise.all(prmiseArray)
// 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')