mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 15:22:39 +00:00
runBuild option
This commit is contained in:
parent
3df51f7f45
commit
25d106e2ab
@ -51,6 +51,8 @@ if (typeof options.rootDir !== 'string') {
|
|||||||
}
|
}
|
||||||
// Create production build when calling `nuxt build`
|
// Create production build when calling `nuxt build`
|
||||||
options.dev = false
|
options.dev = false
|
||||||
|
options.runBuild = true // Force doing production build before init
|
||||||
|
|
||||||
// Analyze option
|
// Analyze option
|
||||||
options.build = options.build || {}
|
options.build = options.build || {}
|
||||||
if (argv.analyze) {
|
if (argv.analyze) {
|
||||||
@ -59,7 +61,7 @@ if (argv.analyze) {
|
|||||||
|
|
||||||
console.log('[nuxt] Building...') // eslint-disable-line no-console
|
console.log('[nuxt] Building...') // eslint-disable-line no-console
|
||||||
var nuxt = module.exports = new Nuxt(options)
|
var nuxt = module.exports = new Nuxt(options)
|
||||||
nuxt.build()
|
nuxt.init()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log('[nuxt] Building done') // eslint-disable-line no-console
|
console.log('[nuxt] Building done') // eslint-disable-line no-console
|
||||||
})
|
})
|
||||||
|
@ -47,6 +47,7 @@ if (typeof options.rootDir !== 'string') {
|
|||||||
options.rootDir = rootDir
|
options.rootDir = rootDir
|
||||||
}
|
}
|
||||||
options.dev = false // Force production mode (no webpack middleware called)
|
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
|
console.log('[nuxt] Generating...') // eslint-disable-line no-console
|
||||||
var nuxt = module.exports = new Nuxt(options)
|
var nuxt = module.exports = new Nuxt(options)
|
||||||
|
@ -74,9 +74,6 @@ export default class Builder extends Tapable {
|
|||||||
}
|
}
|
||||||
this._buildStatus = STATUS.BUILDING
|
this._buildStatus = STATUS.BUILDING
|
||||||
|
|
||||||
// Ensure nuxt initialized
|
|
||||||
await this.nuxt.init()
|
|
||||||
|
|
||||||
// Check if pages dir exists and warn if not
|
// Check if pages dir exists and warn if not
|
||||||
this._nuxtPages = typeof this.options.build.createRoutes !== 'function'
|
this._nuxtPages = typeof this.options.build.createRoutes !== 'function'
|
||||||
if (this._nuxtPages) {
|
if (this._nuxtPages) {
|
||||||
|
@ -45,11 +45,17 @@ export default function defaults (_options) {
|
|||||||
options.store = true
|
options.store = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// runBuild can not be enabled for dev === true
|
||||||
|
if (options.dev === true) {
|
||||||
|
options.runBuild = false
|
||||||
|
}
|
||||||
|
|
||||||
return options
|
return options
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
dev: (process.env.NODE_ENV !== 'production'),
|
dev: (process.env.NODE_ENV !== 'production'),
|
||||||
|
runBuild: false,
|
||||||
buildDir: '.nuxt',
|
buildDir: '.nuxt',
|
||||||
build: {
|
build: {
|
||||||
analyze: false,
|
analyze: false,
|
||||||
|
@ -30,8 +30,8 @@ export default class Generator extends Tapable {
|
|||||||
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
|
// Wait for nuxt be ready
|
||||||
await this.nuxt.build()
|
await this.nuxt.init()
|
||||||
|
|
||||||
// Clean destination folder
|
// Clean destination folder
|
||||||
await remove(distPath)
|
await remove(distPath)
|
||||||
|
29
lib/nuxt.js
29
lib/nuxt.js
@ -23,6 +23,18 @@ export default class Nuxt extends Tapable {
|
|||||||
this.renderRoute = this.renderer.renderRoute.bind(this.renderer)
|
this.renderRoute = this.renderer.renderRoute.bind(this.renderer)
|
||||||
this.renderAndGetWindow = this.renderer.renderAndGetWindow.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)
|
this._init = this.init().catch(this.errorHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -31,17 +43,12 @@ export default class Nuxt extends Tapable {
|
|||||||
return this._init
|
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
|
// Wait for all components to be ready
|
||||||
await this.applyPluginsAsync('beforeInit')
|
await this.applyPluginsAsync('beforeInit') // 1- Modules
|
||||||
await this.applyPluginsAsync('init')
|
await this.applyPluginsAsync('init') // 2- Builder
|
||||||
this.initialized = true
|
await this.applyPluginsAsync('afterInit') // 3- Renderer
|
||||||
this.applyPluginsAsync('afterInit').catch(this.errorHandler)
|
|
||||||
|
|
||||||
|
this.initialized = true
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,10 +70,6 @@ export default class Nuxt extends Tapable {
|
|||||||
return this._generator
|
return this._generator
|
||||||
}
|
}
|
||||||
|
|
||||||
build () {
|
|
||||||
return this.builder.build.apply(this.builder, arguments)
|
|
||||||
}
|
|
||||||
|
|
||||||
generate () {
|
generate () {
|
||||||
return this.generator.generate.apply(this.generator, arguments)
|
return this.generator.generate.apply(this.generator, arguments)
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ export default class Renderer extends Tapable {
|
|||||||
this._init = this.init().catch(this.nuxt.errorHandler)
|
this._init = this.init().catch(this.nuxt.errorHandler)
|
||||||
} else {
|
} else {
|
||||||
// Wait for hook
|
// Wait for hook
|
||||||
this.nuxt.plugin('init', () => {
|
this.nuxt.plugin('afterInit', () => {
|
||||||
this._init = this.init()
|
this._init = this.init()
|
||||||
return this._init
|
return this._init
|
||||||
})
|
})
|
||||||
@ -123,8 +123,6 @@ export default class Renderer extends Tapable {
|
|||||||
|
|
||||||
// Promisify renderToString
|
// Promisify renderToString
|
||||||
this.bundleRenderer.renderToString = pify(this.bundleRenderer.renderToString)
|
this.bundleRenderer.renderToString = pify(this.bundleRenderer.renderToString)
|
||||||
|
|
||||||
debug('ready')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async render (req, res) {
|
async render (req, res) {
|
||||||
|
@ -15,7 +15,7 @@ test.before('Init Nuxt.js', async t => {
|
|||||||
dev: true
|
dev: true
|
||||||
}
|
}
|
||||||
nuxt = new Nuxt(options)
|
nuxt = new Nuxt(options)
|
||||||
await nuxt.build()
|
await nuxt.init()
|
||||||
server = new Nuxt.Server(nuxt)
|
server = new Nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
server.listen(port, 'localhost')
|
||||||
})
|
})
|
||||||
|
@ -6,6 +6,7 @@ test('Fail with routes() which throw an error', async t => {
|
|||||||
const options = {
|
const options = {
|
||||||
rootDir: resolve(__dirname, 'fixtures/basic'),
|
rootDir: resolve(__dirname, 'fixtures/basic'),
|
||||||
dev: false,
|
dev: false,
|
||||||
|
runBuild: true,
|
||||||
generate: {
|
generate: {
|
||||||
async routes () {
|
async routes () {
|
||||||
throw new Error('Not today!')
|
throw new Error('Not today!')
|
||||||
|
@ -4,6 +4,7 @@ import http from 'http'
|
|||||||
import serveStatic from 'serve-static'
|
import serveStatic from 'serve-static'
|
||||||
import finalhandler from 'finalhandler'
|
import finalhandler from 'finalhandler'
|
||||||
import rp from 'request-promise-native'
|
import rp from 'request-promise-native'
|
||||||
|
|
||||||
const port = 4002
|
const port = 4002
|
||||||
const url = (route) => 'http://localhost:' + port + route
|
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'))
|
let config = require(resolve(rootDir, 'nuxt.config.js'))
|
||||||
config.rootDir = rootDir
|
config.rootDir = rootDir
|
||||||
config.dev = false
|
config.dev = false
|
||||||
|
config.runBuild = true
|
||||||
nuxt = new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
try {
|
try {
|
||||||
await nuxt.generate() // throw an error (of /validate route)
|
await nuxt.generate() // throw an error (of /validate route)
|
||||||
} catch (err) {}
|
} catch (err) {
|
||||||
|
}
|
||||||
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
|
const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist'))
|
||||||
server = http.createServer((req, res) => {
|
server = http.createServer((req, res) => {
|
||||||
serve(req, res, finalhandler(req, res))
|
serve(req, res, finalhandler(req, res))
|
||||||
|
@ -14,10 +14,11 @@ test.before('Init Nuxt.js', async t => {
|
|||||||
const Nuxt = require('../')
|
const Nuxt = require('../')
|
||||||
const options = {
|
const options = {
|
||||||
rootDir: resolve(__dirname, 'fixtures/basic'),
|
rootDir: resolve(__dirname, 'fixtures/basic'),
|
||||||
dev: false
|
dev: false,
|
||||||
|
runBuild: true
|
||||||
}
|
}
|
||||||
nuxt = new Nuxt(options)
|
nuxt = new Nuxt(options)
|
||||||
await nuxt.build()
|
await nuxt.init()
|
||||||
server = new Nuxt.Server(nuxt)
|
server = new Nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
server.listen(port, 'localhost')
|
||||||
})
|
})
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import test from 'ava'
|
import test from 'ava'
|
||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
|
|
||||||
const port = 4004
|
const port = 4004
|
||||||
// const url = (route) => 'http://localhost:' + port + route
|
// const url = (route) => 'http://localhost:' + port + route
|
||||||
|
|
||||||
@ -11,10 +12,11 @@ test.before('Init Nuxt.js', async t => {
|
|||||||
const Nuxt = require('../')
|
const Nuxt = require('../')
|
||||||
const options = {
|
const options = {
|
||||||
rootDir: resolve(__dirname, 'fixtures/children'),
|
rootDir: resolve(__dirname, 'fixtures/children'),
|
||||||
dev: false
|
dev: false,
|
||||||
|
runBuild: true
|
||||||
}
|
}
|
||||||
nuxt = new Nuxt(options)
|
nuxt = new Nuxt(options)
|
||||||
await nuxt.build()
|
await nuxt.init()
|
||||||
server = new Nuxt.Server(nuxt)
|
server = new Nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
server.listen(port, 'localhost')
|
||||||
})
|
})
|
||||||
|
@ -2,15 +2,17 @@ import test from 'ava'
|
|||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import pify from 'pify'
|
import pify from 'pify'
|
||||||
|
|
||||||
const readFile = pify(fs.readFile)
|
const readFile = pify(fs.readFile)
|
||||||
|
|
||||||
test.before('Init Nuxt.js', async t => {
|
test.before('Init Nuxt.js', async t => {
|
||||||
const Nuxt = require('../')
|
const Nuxt = require('../')
|
||||||
const nuxt = new Nuxt({
|
const nuxt = new Nuxt({
|
||||||
rootDir: resolve(__dirname, 'fixtures/dynamic-routes'),
|
rootDir: resolve(__dirname, 'fixtures/dynamic-routes'),
|
||||||
dev: false
|
dev: false,
|
||||||
|
runBuild: true
|
||||||
})
|
})
|
||||||
await nuxt.build()
|
await nuxt.init()
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Check .nuxt/router.js', t => {
|
test('Check .nuxt/router.js', t => {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import test from 'ava'
|
import test from 'ava'
|
||||||
import { resolve } from 'path'
|
import { resolve } from 'path'
|
||||||
|
|
||||||
const port = 4005
|
const port = 4005
|
||||||
const url = (route) => 'http://localhost:' + port + route
|
const url = (route) => 'http://localhost:' + port + route
|
||||||
|
|
||||||
@ -11,10 +12,11 @@ test.before('Init Nuxt.js', async t => {
|
|||||||
const Nuxt = require('../')
|
const Nuxt = require('../')
|
||||||
const options = {
|
const options = {
|
||||||
rootDir: resolve(__dirname, 'fixtures/error'),
|
rootDir: resolve(__dirname, 'fixtures/error'),
|
||||||
dev: false
|
dev: false,
|
||||||
|
runBuild: true
|
||||||
}
|
}
|
||||||
nuxt = new Nuxt(options)
|
nuxt = new Nuxt(options)
|
||||||
await nuxt.build()
|
await nuxt.init()
|
||||||
server = new Nuxt.Server(nuxt)
|
server = new Nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
server.listen(port, 'localhost')
|
||||||
})
|
})
|
||||||
|
@ -10,6 +10,7 @@ test('Nuxt.js Class', t => {
|
|||||||
test.serial('Nuxt.js Instance', async t => {
|
test.serial('Nuxt.js Instance', async t => {
|
||||||
const nuxt = new Nuxt({
|
const nuxt = new Nuxt({
|
||||||
dev: false,
|
dev: false,
|
||||||
|
runBuild: true,
|
||||||
rootDir: resolve(__dirname, 'fixtures', 'empty')
|
rootDir: resolve(__dirname, 'fixtures', 'empty')
|
||||||
})
|
})
|
||||||
t.is(typeof nuxt, 'object')
|
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.build, 'function')
|
||||||
t.is(typeof nuxt.generate, 'function')
|
t.is(typeof nuxt.generate, 'function')
|
||||||
t.is(typeof nuxt._init.then, 'function')
|
t.is(typeof nuxt._init.then, 'function')
|
||||||
await nuxt.build()
|
await nuxt.init()
|
||||||
t.is(nuxt.initialized, true)
|
t.is(nuxt.initialized, true)
|
||||||
})
|
})
|
||||||
|
|
||||||
test.serial('Fail to build when no pages/ directory but is in the parent', t => {
|
test.serial('Fail to build when no pages/ directory but is in the parent', t => {
|
||||||
const nuxt = new Nuxt({
|
const nuxt = new Nuxt({
|
||||||
dev: false,
|
dev: false,
|
||||||
|
runBuild: true,
|
||||||
rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages')
|
rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages')
|
||||||
})
|
})
|
||||||
return nuxt.build().catch(err => {
|
return nuxt.init().catch(err => {
|
||||||
let s = String(err)
|
let s = String(err)
|
||||||
t.true(s.includes('No `pages` directory found'))
|
t.true(s.includes('No `pages` directory found'))
|
||||||
t.true(s.includes('Did you mean to run `nuxt` in the parent (`../`) directory?'))
|
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 => {
|
test.serial('Fail to build when no pages/ directory', t => {
|
||||||
const nuxt = new Nuxt({
|
const nuxt = new Nuxt({
|
||||||
dev: false,
|
dev: false,
|
||||||
|
runBuild: true,
|
||||||
rootDir: resolve(__dirname)
|
rootDir: resolve(__dirname)
|
||||||
})
|
})
|
||||||
return nuxt.build().catch(err => {
|
return nuxt.init().catch(err => {
|
||||||
let s = String(err)
|
let s = String(err)
|
||||||
t.true(s.includes('Couldn\'t find a `pages` directory'))
|
t.true(s.includes('Couldn\'t find a `pages` directory'))
|
||||||
t.true(s.includes('Please create one under the project root'))
|
t.true(s.includes('Please create one under the project root'))
|
||||||
|
@ -15,8 +15,9 @@ test.before('Init Nuxt.js', async t => {
|
|||||||
let config = require(resolve(rootDir, 'nuxt.config.js'))
|
let config = require(resolve(rootDir, 'nuxt.config.js'))
|
||||||
config.rootDir = rootDir
|
config.rootDir = rootDir
|
||||||
config.dev = false
|
config.dev = false
|
||||||
|
config.runBuild = true
|
||||||
nuxt = new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
await nuxt.build()
|
await nuxt.init()
|
||||||
server = new nuxt.Server(nuxt)
|
server = new nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
server.listen(port, 'localhost')
|
||||||
})
|
})
|
||||||
|
@ -15,8 +15,9 @@ test.before('Init Nuxt.js', async t => {
|
|||||||
let config = require(resolve(rootDir, 'nuxt.config.js'))
|
let config = require(resolve(rootDir, 'nuxt.config.js'))
|
||||||
config.rootDir = rootDir
|
config.rootDir = rootDir
|
||||||
config.dev = false
|
config.dev = false
|
||||||
|
config.runBuild = true
|
||||||
nuxt = new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
await nuxt.build()
|
await nuxt.init()
|
||||||
server = new Nuxt.Server(nuxt)
|
server = new Nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
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'))
|
let config = require(resolve(rootDir, 'nuxt.config.js'))
|
||||||
config.rootDir = rootDir
|
config.rootDir = rootDir
|
||||||
config.dev = false
|
config.dev = false
|
||||||
|
config.runBuild = true
|
||||||
nuxt = new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
await nuxt.init()
|
await nuxt.init()
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user