runBuild option

This commit is contained in:
Pooya Parsa 2017-06-14 23:21:14 +04:30
parent 3df51f7f45
commit 25d106e2ab
17 changed files with 123 additions and 99 deletions

View File

@ -51,6 +51,8 @@ if (typeof options.rootDir !== 'string') {
}
// Create production build when calling `nuxt build`
options.dev = false
options.runBuild = true // Force doing production build before init
// Analyze option
options.build = options.build || {}
if (argv.analyze) {
@ -59,7 +61,7 @@ if (argv.analyze) {
console.log('[nuxt] Building...') // eslint-disable-line no-console
var nuxt = module.exports = new Nuxt(options)
nuxt.build()
nuxt.init()
.then(() => {
console.log('[nuxt] Building done') // eslint-disable-line no-console
})

View File

@ -47,6 +47,7 @@ if (typeof options.rootDir !== 'string') {
options.rootDir = rootDir
}
options.dev = false // Force production mode (no webpack middleware called)
options.runBuild = true // Force doing production build before init
console.log('[nuxt] Generating...') // eslint-disable-line no-console
var nuxt = module.exports = new Nuxt(options)

View File

@ -74,9 +74,6 @@ export default class Builder extends Tapable {
}
this._buildStatus = STATUS.BUILDING
// Ensure nuxt initialized
await this.nuxt.init()
// Check if pages dir exists and warn if not
this._nuxtPages = typeof this.options.build.createRoutes !== 'function'
if (this._nuxtPages) {

View File

@ -45,11 +45,17 @@ export default function defaults (_options) {
options.store = true
}
// runBuild can not be enabled for dev === true
if (options.dev === true) {
options.runBuild = false
}
return options
}
const defaultOptions = {
dev: (process.env.NODE_ENV !== 'production'),
runBuild: false,
buildDir: '.nuxt',
build: {
analyze: false,

View File

@ -30,8 +30,8 @@ export default class Generator extends Tapable {
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.nuxt.build()
// Wait for nuxt be ready
await this.nuxt.init()
// Clean destination folder
await remove(distPath)

View File

@ -23,6 +23,18 @@ export default class Nuxt extends Tapable {
this.renderRoute = this.renderer.renderRoute.bind(this.renderer)
this.renderAndGetWindow = this.renderer.renderAndGetWindow.bind(this.renderer)
// Builder is lazy loaded, so register plugin here
this.plugin('init', async () => {
// Call to build on dev
if (this.options.dev) {
this.builder.build().catch(this.errorHandler)
}
// If explicitly runBuild required
if (this.options.runBuild) {
await this.builder.build()
}
})
this._init = this.init().catch(this.errorHandler)
}
@ -31,17 +43,12 @@ export default class Nuxt extends Tapable {
return this._init
}
// Call to build on dev
if (this.options.dev) {
this.builder.build().catch(this.errorHandler)
}
// Wait for all components to be ready
await this.applyPluginsAsync('beforeInit')
await this.applyPluginsAsync('init')
this.initialized = true
this.applyPluginsAsync('afterInit').catch(this.errorHandler)
await this.applyPluginsAsync('beforeInit') // 1- Modules
await this.applyPluginsAsync('init') // 2- Builder
await this.applyPluginsAsync('afterInit') // 3- Renderer
this.initialized = true
return this
}
@ -63,10 +70,6 @@ export default class Nuxt extends Tapable {
return this._generator
}
build () {
return this.builder.build.apply(this.builder, arguments)
}
generate () {
return this.generator.generate.apply(this.generator, arguments)
}

View File

@ -44,7 +44,7 @@ export default class Renderer extends Tapable {
this._init = this.init().catch(this.nuxt.errorHandler)
} else {
// Wait for hook
this.nuxt.plugin('init', () => {
this.nuxt.plugin('afterInit', () => {
this._init = this.init()
return this._init
})
@ -123,8 +123,6 @@ export default class Renderer extends Tapable {
// Promisify renderToString
this.bundleRenderer.renderToString = pify(this.bundleRenderer.renderToString)
debug('ready')
}
async render (req, res) {

View File

@ -15,7 +15,7 @@ test.before('Init Nuxt.js', async t => {
dev: true
}
nuxt = new Nuxt(options)
await nuxt.build()
await nuxt.init()
server = new Nuxt.Server(nuxt)
server.listen(port, 'localhost')
})

View File

@ -6,6 +6,7 @@ test('Fail with routes() which throw an error', async t => {
const options = {
rootDir: resolve(__dirname, 'fixtures/basic'),
dev: false,
runBuild: true,
generate: {
async routes () {
throw new Error('Not today!')

View File

@ -4,6 +4,7 @@ import http from 'http'
import serveStatic from 'serve-static'
import finalhandler from 'finalhandler'
import rp from 'request-promise-native'
const port = 4002
const url = (route) => 'http://localhost:' + port + route
@ -17,10 +18,12 @@ test.before('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
config.runBuild = true
nuxt = new Nuxt(config)
try {
await nuxt.generate() // throw an error (of /validate route)
} catch (err) {}
} catch (err) {
}
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
server = http.createServer((req, res) => {
serve(req, res, finalhandler(req, res))

View File

@ -14,10 +14,11 @@ test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
const options = {
rootDir: resolve(__dirname, 'fixtures/basic'),
dev: false
dev: false,
runBuild: true
}
nuxt = new Nuxt(options)
await nuxt.build()
await nuxt.init()
server = new Nuxt.Server(nuxt)
server.listen(port, 'localhost')
})

View File

@ -1,5 +1,6 @@
import test from 'ava'
import { resolve } from 'path'
const port = 4004
// const url = (route) => 'http://localhost:' + port + route
@ -11,10 +12,11 @@ test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
const options = {
rootDir: resolve(__dirname, 'fixtures/children'),
dev: false
dev: false,
runBuild: true
}
nuxt = new Nuxt(options)
await nuxt.build()
await nuxt.init()
server = new Nuxt.Server(nuxt)
server.listen(port, 'localhost')
})

View File

@ -2,79 +2,81 @@ import test from 'ava'
import { resolve } from 'path'
import fs from 'fs'
import pify from 'pify'
const readFile = pify(fs.readFile)
test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
const nuxt = new Nuxt({
rootDir: resolve(__dirname, 'fixtures/dynamic-routes'),
dev: false
dev: false,
runBuild: true
})
await nuxt.build()
await nuxt.init()
})
test('Check .nuxt/router.js', t => {
return readFile(resolve(__dirname, './fixtures/dynamic-routes/.nuxt/router.js'), 'utf-8')
.then((routerFile) => {
routerFile = routerFile
.slice(routerFile.indexOf('routes: ['))
.replace('routes: [', '[')
.replace(/ _[0-9A-z]+,/g, ' "",')
routerFile = routerFile.substr(routerFile.indexOf('['), routerFile.lastIndexOf(']') + 1)
let routes = eval('( ' + routerFile + ')') // eslint-disable-line no-eval
// pages/index.vue
t.is(routes[0].path, '/')
t.is(routes[0].name, 'index')
// pages/test/index.vue
t.is(routes[1].path, '/test')
t.is(routes[1].name, 'test')
// pages/posts.vue
t.is(routes[2].path, '/posts')
t.is(routes[2].name, 'posts')
t.is(routes[2].children.length, 1)
// pages/posts/_id.vue
t.is(routes[2].children[0].path, ':id?')
t.is(routes[2].children[0].name, 'posts-id')
// pages/parent.vue
t.is(routes[3].path, '/parent')
t.falsy(routes[3].name) // parent route has no name
// pages/parent/*.vue
t.is(routes[3].children.length, 3) // parent has 3 children
t.deepEqual(routes[3].children.map((r) => r.path), ['', 'teub', 'child'])
t.deepEqual(routes[3].children.map((r) => r.name), ['parent', 'parent-teub', 'parent-child'])
// pages/test/projects/index.vue
t.is(routes[4].path, '/test/projects')
t.is(routes[4].name, 'test-projects')
// pages/test/users.vue
t.is(routes[5].path, '/test/users')
t.falsy(routes[5].name) // parent route has no name
// pages/test/users/*.vue
t.is(routes[5].children.length, 5) // parent has 5 children
t.deepEqual(routes[5].children.map((r) => r.path), ['', 'projects', 'projects/:category', ':id', ':index/teub'])
t.deepEqual(routes[5].children.map((r) => r.name), ['test-users', 'test-users-projects', 'test-users-projects-category', 'test-users-id', 'test-users-index-teub'])
// pages/test/songs/toto.vue
t.is(routes[6].path, '/test/songs/toto')
t.is(routes[6].name, 'test-songs-toto')
// pages/test/songs/_id.vue
t.is(routes[7].path, '/test/songs/:id?')
t.is(routes[7].name, 'test-songs-id')
// pages/test/projects/_category.vue
t.is(routes[8].path, '/test/projects/:category')
t.is(routes[8].name, 'test-projects-category')
// pages/users/_id.vue
t.is(routes[9].path, '/users/:id?')
t.is(routes[9].name, 'users-id')
// pages/test/_.vue
t.is(routes[10].path, '/test/*')
t.is(routes[10].name, 'test-all')
// pages/_slug.vue
t.is(routes[11].path, '/:slug')
t.is(routes[11].name, 'slug')
// pages/_key/_id.vue
t.is(routes[12].path, '/:key/:id?')
t.is(routes[12].name, 'key-id')
// pages/_.vue
t.is(routes[13].path, '/*')
t.is(routes[13].name, 'all')
})
.then((routerFile) => {
routerFile = routerFile
.slice(routerFile.indexOf('routes: ['))
.replace('routes: [', '[')
.replace(/ _[0-9A-z]+,/g, ' "",')
routerFile = routerFile.substr(routerFile.indexOf('['), routerFile.lastIndexOf(']') + 1)
let routes = eval('( ' + routerFile + ')') // eslint-disable-line no-eval
// pages/index.vue
t.is(routes[0].path, '/')
t.is(routes[0].name, 'index')
// pages/test/index.vue
t.is(routes[1].path, '/test')
t.is(routes[1].name, 'test')
// pages/posts.vue
t.is(routes[2].path, '/posts')
t.is(routes[2].name, 'posts')
t.is(routes[2].children.length, 1)
// pages/posts/_id.vue
t.is(routes[2].children[0].path, ':id?')
t.is(routes[2].children[0].name, 'posts-id')
// pages/parent.vue
t.is(routes[3].path, '/parent')
t.falsy(routes[3].name) // parent route has no name
// pages/parent/*.vue
t.is(routes[3].children.length, 3) // parent has 3 children
t.deepEqual(routes[3].children.map((r) => r.path), ['', 'teub', 'child'])
t.deepEqual(routes[3].children.map((r) => r.name), ['parent', 'parent-teub', 'parent-child'])
// pages/test/projects/index.vue
t.is(routes[4].path, '/test/projects')
t.is(routes[4].name, 'test-projects')
// pages/test/users.vue
t.is(routes[5].path, '/test/users')
t.falsy(routes[5].name) // parent route has no name
// pages/test/users/*.vue
t.is(routes[5].children.length, 5) // parent has 5 children
t.deepEqual(routes[5].children.map((r) => r.path), ['', 'projects', 'projects/:category', ':id', ':index/teub'])
t.deepEqual(routes[5].children.map((r) => r.name), ['test-users', 'test-users-projects', 'test-users-projects-category', 'test-users-id', 'test-users-index-teub'])
// pages/test/songs/toto.vue
t.is(routes[6].path, '/test/songs/toto')
t.is(routes[6].name, 'test-songs-toto')
// pages/test/songs/_id.vue
t.is(routes[7].path, '/test/songs/:id?')
t.is(routes[7].name, 'test-songs-id')
// pages/test/projects/_category.vue
t.is(routes[8].path, '/test/projects/:category')
t.is(routes[8].name, 'test-projects-category')
// pages/users/_id.vue
t.is(routes[9].path, '/users/:id?')
t.is(routes[9].name, 'users-id')
// pages/test/_.vue
t.is(routes[10].path, '/test/*')
t.is(routes[10].name, 'test-all')
// pages/_slug.vue
t.is(routes[11].path, '/:slug')
t.is(routes[11].name, 'slug')
// pages/_key/_id.vue
t.is(routes[12].path, '/:key/:id?')
t.is(routes[12].name, 'key-id')
// pages/_.vue
t.is(routes[13].path, '/*')
t.is(routes[13].name, 'all')
})
})

View File

@ -1,5 +1,6 @@
import test from 'ava'
import { resolve } from 'path'
const port = 4005
const url = (route) => 'http://localhost:' + port + route
@ -11,10 +12,11 @@ test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
const options = {
rootDir: resolve(__dirname, 'fixtures/error'),
dev: false
dev: false,
runBuild: true
}
nuxt = new Nuxt(options)
await nuxt.build()
await nuxt.init()
server = new Nuxt.Server(nuxt)
server.listen(port, 'localhost')
})

View File

@ -10,6 +10,7 @@ test('Nuxt.js Class', t => {
test.serial('Nuxt.js Instance', async t => {
const nuxt = new Nuxt({
dev: false,
runBuild: true,
rootDir: resolve(__dirname, 'fixtures', 'empty')
})
t.is(typeof nuxt, 'object')
@ -17,16 +18,17 @@ test.serial('Nuxt.js Instance', async t => {
t.is(typeof nuxt.build, 'function')
t.is(typeof nuxt.generate, 'function')
t.is(typeof nuxt._init.then, 'function')
await nuxt.build()
await nuxt.init()
t.is(nuxt.initialized, true)
})
test.serial('Fail to build when no pages/ directory but is in the parent', t => {
const nuxt = new Nuxt({
dev: false,
runBuild: true,
rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages')
})
return nuxt.build().catch(err => {
return nuxt.init().catch(err => {
let s = String(err)
t.true(s.includes('No `pages` directory found'))
t.true(s.includes('Did you mean to run `nuxt` in the parent (`../`) directory?'))
@ -37,9 +39,10 @@ test.serial('Fail to build when no pages/ directory but is in the parent', t =>
test.serial('Fail to build when no pages/ directory', t => {
const nuxt = new Nuxt({
dev: false,
runBuild: true,
rootDir: resolve(__dirname)
})
return nuxt.build().catch(err => {
return nuxt.init().catch(err => {
let s = String(err)
t.true(s.includes('Couldn\'t find a `pages` directory'))
t.true(s.includes('Please create one under the project root'))

View File

@ -15,8 +15,9 @@ test.before('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
config.runBuild = true
nuxt = new Nuxt(config)
await nuxt.build()
await nuxt.init()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')
})

View File

@ -15,8 +15,9 @@ test.before('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
config.runBuild = true
nuxt = new Nuxt(config)
await nuxt.build()
await nuxt.init()
server = new Nuxt.Server(nuxt)
server.listen(port, 'localhost')
})
@ -110,6 +111,7 @@ test.after('Should be able to start Nuxt with build done', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
config.runBuild = true
nuxt = new Nuxt(config)
await nuxt.init()
})