refactor: nuxt constructor no longer returns a promise

by not returning a promise we can expose .render method
also the old way of using nuxt won't change by 1.x release
This commit is contained in:
Pooya Parsa 2017-05-31 18:51:16 +04:30
parent 9f7722f845
commit c5ca8c64f1
19 changed files with 92 additions and 89 deletions

View File

@ -52,7 +52,7 @@ if (analyzeBuild) {
}
console.log('[nuxt] Building...') // eslint-disable-line no-console
new Nuxt(options).then(nuxt => {
var nuxt = module.exports = new Nuxt(options)
nuxt.build()
.then(() => {
console.log('[nuxt] Building done') // eslint-disable-line no-console
@ -61,4 +61,3 @@ new Nuxt(options).then(nuxt => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
})

View File

@ -39,9 +39,11 @@ if (typeof options.rootDir !== 'string') {
}
options.dev = true // Add hot reloading and watching changes
new Nuxt(options).then(nuxt => {
var server = new nuxt.Server(nuxt)
.listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host)
var nuxt = module.exports = new Nuxt(options)
var port = process.env.PORT || process.env.npm_package_config_nuxt_port
var host = process.env.HOST || process.env.npm_package_config_nuxt_host
var server = nuxt.server = new nuxt.Server(nuxt).listen(port, host)
listenOnConfigChanges(nuxt, server)
nuxt.build()
@ -49,7 +51,6 @@ new Nuxt(options).then(nuxt => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
})
function listenOnConfigChanges(nuxt, server) {
// Listen on nuxt.config.js changes
@ -69,7 +70,7 @@ function listenOnConfigChanges (nuxt, server) {
.then(() => {
nuxt.renderer = null
debug('Rebuilding the app...')
return (new Nuxt(options)).then(nuxt => nuxt.build())
return new Nuxt(options).build()
})
.then((nuxt) => {
server.nuxt = nuxt

View File

@ -20,7 +20,7 @@ if (typeof options.rootDir !== 'string') {
options.dev = false // Force production mode (no webpack middleware called)
console.log('[nuxt] Generating...') // eslint-disable-line no-console
new Nuxt(options).then(nuxt => {
var nuxt = module.exports = new Nuxt(options)
nuxt.generate()
.then(() => {
console.log('[nuxt] Generate done') // eslint-disable-line no-console
@ -29,4 +29,3 @@ new Nuxt(options).then(nuxt => {
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
})

View File

@ -16,10 +16,7 @@ if (typeof options.rootDir !== 'string') {
}
options.dev = false // Force production mode (no webpack middleware called)
new Nuxt(options).then(nuxt => {
new nuxt.Server(nuxt)
.listen(
process.env.PORT || process.env.npm_package_config_nuxt_port,
process.env.HOST || process.env.npm_package_config_nuxt_host
)
})
var nuxt = module.exports = new Nuxt(options)
var port = process.env.PORT || process.env.npm_package_config_nuxt_port
var host = process.env.HOST || process.env.npm_package_config_nuxt_host
var server = nuxt.server = new nuxt.Server(nuxt).listen(port, host)

View File

@ -14,7 +14,7 @@ test.before('Init Nuxt.js', async t => {
try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {}
config.rootDir = rootDir // project folder
config.dev = false // production build
nuxt = await new Nuxt(config)
nuxt = new Nuxt(config)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(4000, 'localhost')

View File

@ -97,8 +97,10 @@ export function options () {
}
export async function build () {
this._nuxtPages = typeof this.createRoutes !== 'function'
// Initialize modules first
await this.module.init()
// Check if pages dir exists and warn if not
this._nuxtPages = typeof this.createRoutes !== 'function'
if (this._nuxtPages) {
if (!fs.existsSync(join(this.srcDir, 'pages'))) {
if (fs.existsSync(join(this.srcDir, '..', 'pages'))) {

View File

@ -4,13 +4,27 @@ import path from 'path'
import fs from 'fs'
import {uniq} from 'lodash'
import hash from 'hash-sum'
import {chainFn} from './utils'
import {chainFn, sequence} from './utils'
const debug = require('debug')('nuxt:module')
class Module {
constructor(nuxt) {
this.nuxt = nuxt
this.options = nuxt.options
this.modules = []
this.initialized = false
}
async init() {
if (this.initialized) {
debug('[nuxt] Modules are already initialized')
return
}
// Install all modules in sequence
await sequence(this.options.modules, this.addModule.bind(this))
// Indicate modules are already initialized
this.initialized = true
}
addVendor(vendor) {
@ -31,8 +45,7 @@ class Module {
const srcPath = path.parse(src)
/* istanbul ignore if */
if (!src || typeof src !== 'string' || !fs.existsSync(src)) {
// eslint-disable-next-line no-console
console.warn('[nuxt] invalid template', template)
debug('[nuxt] invalid template', template)
return
}
// Generate unique and human readable dst filename

View File

@ -121,14 +121,6 @@ class Nuxt {
this.utils = utils
// Add module integration
this.module = new Module(this)
// Install all modules in sequence and then return `this` instance
return utils.sequence(options.modules, this.module.addModule.bind(this.module))
.then(() => this)
.catch(/* istanbul ignore next */ (err) => {
console.error('[nuxt] error while initializing modules') // eslint-disable-line no-console
console.error(err) // eslint-disable-line no-console
process.exit(1)
})
}
close (callback) {

View File

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

View File

@ -14,7 +14,7 @@ test('Fail with routes() which throw an error', async t => {
}
}
}
const nuxt = await new Nuxt(options)
const nuxt = new Nuxt(options)
return new Promise((resolve) => {
var oldExit = process.exit
var oldCE = console.error // eslint-disable-line no-console

View File

@ -17,7 +17,7 @@ test.before('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
nuxt = await new Nuxt(config)
nuxt = new Nuxt(config)
try {
await nuxt.generate() // throw an error (of /validate route)
} catch (err) {}

View File

@ -16,7 +16,7 @@ test.before('Init Nuxt.js', async t => {
rootDir: resolve(__dirname, 'fixtures/basic'),
dev: false
}
nuxt = await new Nuxt(options)
nuxt = new Nuxt(options)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')

View File

@ -13,7 +13,7 @@ test.before('Init Nuxt.js', async t => {
rootDir: resolve(__dirname, 'fixtures/children'),
dev: false
}
nuxt = await new Nuxt(options)
nuxt = new Nuxt(options)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')

View File

@ -6,7 +6,7 @@ const readFile = pify(fs.readFile)
test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
const nuxt = await new Nuxt({
const nuxt = new Nuxt({
rootDir: resolve(__dirname, 'fixtures/dynamic-routes'),
dev: false
})

View File

@ -13,7 +13,7 @@ test.before('Init Nuxt.js', async t => {
rootDir: resolve(__dirname, 'fixtures/error'),
dev: false
}
nuxt = await new Nuxt(options)
nuxt = new Nuxt(options)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')

View File

@ -8,7 +8,7 @@ test('Nuxt.js Class', t => {
})
test('Nuxt.js Instance', async t => {
const nuxt = await new Nuxt()
const nuxt = new Nuxt()
t.is(typeof nuxt, 'object')
t.is(nuxt.dev, true)
t.is(typeof nuxt.build, 'function')
@ -16,7 +16,7 @@ test('Nuxt.js Instance', async t => {
})
test.serial('Fail when build not done and try to render', async t => {
const nuxt = await new Nuxt({
const nuxt = new Nuxt({
dev: false,
rootDir: resolve(__dirname, 'fixtures/empty')
})
@ -37,7 +37,7 @@ test.serial('Fail when build not done and try to render', async t => {
})
test.serial('Fail to build when no pages/ directory but is in the parent', async t => {
const nuxt = await new Nuxt({
const nuxt = new Nuxt({
dev: false,
rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages')
})
@ -58,7 +58,7 @@ test.serial('Fail to build when no pages/ directory but is in the parent', async
})
test.serial('Fail to build when no pages/ directory', async t => {
const nuxt = await new Nuxt({
const nuxt = new Nuxt({
dev: false,
rootDir: resolve(__dirname)
})

View File

@ -17,7 +17,7 @@ test.before('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
nuxt = await new Nuxt(config)
nuxt = new Nuxt(config)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')

View File

@ -5,7 +5,7 @@ let utils
// Init nuxt.js and create server listening on localhost:4000
test.before('Init Nuxt.js', async t => {
const Nuxt = require('../')
let nuxt = await new Nuxt({ dev: false })
let nuxt = new Nuxt({ dev: false })
utils = nuxt.utils
})

View File

@ -15,7 +15,7 @@ test.before('Init Nuxt.js', async t => {
let config = require(resolve(rootDir, 'nuxt.config.js'))
config.rootDir = rootDir
config.dev = false
nuxt = await new Nuxt(config)
nuxt = new Nuxt(config)
await nuxt.build()
server = new nuxt.Server(nuxt)
server.listen(port, 'localhost')
@ -110,5 +110,5 @@ 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
nuxt = await new Nuxt(config)
nuxt = new Nuxt(config)
})