mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-27 08:02:01 +00:00
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:
parent
9f7722f845
commit
c5ca8c64f1
@ -52,13 +52,12 @@ if (analyzeBuild) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log('[nuxt] Building...') // eslint-disable-line no-console
|
console.log('[nuxt] Building...') // eslint-disable-line no-console
|
||||||
new Nuxt(options).then(nuxt => {
|
var nuxt = module.exports = new Nuxt(options)
|
||||||
nuxt.build()
|
nuxt.build()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log('[nuxt] Building done') // eslint-disable-line no-console
|
console.log('[nuxt] Building done') // eslint-disable-line no-console
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err) // eslint-disable-line no-console
|
console.error(err) // eslint-disable-line no-console
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
51
bin/nuxt-dev
51
bin/nuxt-dev
@ -39,19 +39,20 @@ if (typeof options.rootDir !== 'string') {
|
|||||||
}
|
}
|
||||||
options.dev = true // Add hot reloading and watching changes
|
options.dev = true // Add hot reloading and watching changes
|
||||||
|
|
||||||
new Nuxt(options).then(nuxt => {
|
var nuxt = module.exports = new Nuxt(options)
|
||||||
var server = new nuxt.Server(nuxt)
|
var port = process.env.PORT || process.env.npm_package_config_nuxt_port
|
||||||
.listen(process.env.PORT || process.env.npm_package_config_nuxt_port, process.env.HOST || process.env.npm_package_config_nuxt_host)
|
var host = process.env.HOST || process.env.npm_package_config_nuxt_host
|
||||||
listenOnConfigChanges(nuxt, server)
|
var server = nuxt.server = new nuxt.Server(nuxt).listen(port, host)
|
||||||
|
|
||||||
nuxt.build()
|
listenOnConfigChanges(nuxt, server)
|
||||||
.catch((err) => {
|
|
||||||
console.error(err) // eslint-disable-line no-console
|
|
||||||
process.exit(1)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
function listenOnConfigChanges (nuxt, server) {
|
nuxt.build()
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err) // eslint-disable-line no-console
|
||||||
|
process.exit(1)
|
||||||
|
})
|
||||||
|
|
||||||
|
function listenOnConfigChanges(nuxt, server) {
|
||||||
// Listen on nuxt.config.js changes
|
// Listen on nuxt.config.js changes
|
||||||
var build = _.debounce(() => {
|
var build = _.debounce(() => {
|
||||||
debug('[nuxt.config.js] changed')
|
debug('[nuxt.config.js] changed')
|
||||||
@ -66,20 +67,20 @@ function listenOnConfigChanges (nuxt, server) {
|
|||||||
}
|
}
|
||||||
options.rootDir = rootDir
|
options.rootDir = rootDir
|
||||||
nuxt.close()
|
nuxt.close()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
nuxt.renderer = null
|
nuxt.renderer = null
|
||||||
debug('Rebuilding the app...')
|
debug('Rebuilding the app...')
|
||||||
return (new Nuxt(options)).then(nuxt => nuxt.build())
|
return new Nuxt(options).build()
|
||||||
})
|
})
|
||||||
.then((nuxt) => {
|
.then((nuxt) => {
|
||||||
server.nuxt = nuxt
|
server.nuxt = nuxt
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error('Error while rebuild the app:', error) // eslint-disable-line no-console
|
console.error('Error while rebuild the app:', error) // eslint-disable-line no-console
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
}, 200)
|
}, 200)
|
||||||
var nuxtConfigFile = resolve(rootDir, nuxtConfigFileName)
|
var nuxtConfigFile = resolve(rootDir, nuxtConfigFileName)
|
||||||
chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, { ignoreInitial: true }))
|
chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, {ignoreInitial: true}))
|
||||||
.on('all', build)
|
.on('all', build)
|
||||||
}
|
}
|
||||||
|
@ -20,13 +20,12 @@ if (typeof options.rootDir !== 'string') {
|
|||||||
options.dev = false // Force production mode (no webpack middleware called)
|
options.dev = false // Force production mode (no webpack middleware called)
|
||||||
|
|
||||||
console.log('[nuxt] Generating...') // eslint-disable-line no-console
|
console.log('[nuxt] Generating...') // eslint-disable-line no-console
|
||||||
new Nuxt(options).then(nuxt => {
|
var nuxt = module.exports = new Nuxt(options)
|
||||||
nuxt.generate()
|
nuxt.generate()
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log('[nuxt] Generate done') // eslint-disable-line no-console
|
console.log('[nuxt] Generate done') // eslint-disable-line no-console
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err) // eslint-disable-line no-console
|
console.error(err) // eslint-disable-line no-console
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
@ -16,10 +16,7 @@ if (typeof options.rootDir !== 'string') {
|
|||||||
}
|
}
|
||||||
options.dev = false // Force production mode (no webpack middleware called)
|
options.dev = false // Force production mode (no webpack middleware called)
|
||||||
|
|
||||||
new Nuxt(options).then(nuxt => {
|
var nuxt = module.exports = new Nuxt(options)
|
||||||
new nuxt.Server(nuxt)
|
var port = process.env.PORT || process.env.npm_package_config_nuxt_port
|
||||||
.listen(
|
var host = process.env.HOST || process.env.npm_package_config_nuxt_host
|
||||||
process.env.PORT || process.env.npm_package_config_nuxt_port,
|
var server = nuxt.server = new nuxt.Server(nuxt).listen(port, host)
|
||||||
process.env.HOST || process.env.npm_package_config_nuxt_host
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
@ -14,7 +14,7 @@ test.before('Init Nuxt.js', async t => {
|
|||||||
try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {}
|
try { config = require(resolve(rootDir, 'nuxt.config.js')) } catch (e) {}
|
||||||
config.rootDir = rootDir // project folder
|
config.rootDir = rootDir // project folder
|
||||||
config.dev = false // production build
|
config.dev = false // production build
|
||||||
nuxt = await new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
await nuxt.build()
|
await nuxt.build()
|
||||||
server = new nuxt.Server(nuxt)
|
server = new nuxt.Server(nuxt)
|
||||||
server.listen(4000, 'localhost')
|
server.listen(4000, 'localhost')
|
||||||
|
@ -97,8 +97,10 @@ export function options () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function build () {
|
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
|
// Check if pages dir exists and warn if not
|
||||||
|
this._nuxtPages = typeof this.createRoutes !== 'function'
|
||||||
if (this._nuxtPages) {
|
if (this._nuxtPages) {
|
||||||
if (!fs.existsSync(join(this.srcDir, 'pages'))) {
|
if (!fs.existsSync(join(this.srcDir, 'pages'))) {
|
||||||
if (fs.existsSync(join(this.srcDir, '..', 'pages'))) {
|
if (fs.existsSync(join(this.srcDir, '..', 'pages'))) {
|
||||||
|
@ -4,16 +4,30 @@ import path from 'path'
|
|||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import {uniq} from 'lodash'
|
import {uniq} from 'lodash'
|
||||||
import hash from 'hash-sum'
|
import hash from 'hash-sum'
|
||||||
import {chainFn} from './utils'
|
import {chainFn, sequence} from './utils'
|
||||||
|
|
||||||
|
const debug = require('debug')('nuxt:module')
|
||||||
|
|
||||||
class Module {
|
class Module {
|
||||||
constructor (nuxt) {
|
constructor(nuxt) {
|
||||||
this.nuxt = nuxt
|
this.nuxt = nuxt
|
||||||
this.options = nuxt.options
|
this.options = nuxt.options
|
||||||
this.modules = []
|
this.modules = []
|
||||||
|
this.initialized = false
|
||||||
}
|
}
|
||||||
|
|
||||||
addVendor (vendor) {
|
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) {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (!vendor) {
|
if (!vendor) {
|
||||||
return
|
return
|
||||||
@ -21,7 +35,7 @@ class Module {
|
|||||||
this.options.build.vendor = uniq(this.options.build.vendor.concat(vendor))
|
this.options.build.vendor = uniq(this.options.build.vendor.concat(vendor))
|
||||||
}
|
}
|
||||||
|
|
||||||
addTemplate (template) {
|
addTemplate(template) {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (!template) {
|
if (!template) {
|
||||||
return
|
return
|
||||||
@ -31,8 +45,7 @@ class Module {
|
|||||||
const srcPath = path.parse(src)
|
const srcPath = path.parse(src)
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (!src || typeof src !== 'string' || !fs.existsSync(src)) {
|
if (!src || typeof src !== 'string' || !fs.existsSync(src)) {
|
||||||
// eslint-disable-next-line no-console
|
debug('[nuxt] invalid template', template)
|
||||||
console.warn('[nuxt] invalid template', template)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// Generate unique and human readable dst filename
|
// Generate unique and human readable dst filename
|
||||||
@ -48,7 +61,7 @@ class Module {
|
|||||||
return templateObj
|
return templateObj
|
||||||
}
|
}
|
||||||
|
|
||||||
addPlugin (template) {
|
addPlugin(template) {
|
||||||
const {dst} = this.addTemplate(template)
|
const {dst} = this.addTemplate(template)
|
||||||
// Add to nuxt plugins
|
// Add to nuxt plugins
|
||||||
this.options.plugins.push({
|
this.options.plugins.push({
|
||||||
@ -57,19 +70,19 @@ class Module {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
addServerMiddleware (middleware) {
|
addServerMiddleware(middleware) {
|
||||||
this.options.serverMiddleware.push(middleware)
|
this.options.serverMiddleware.push(middleware)
|
||||||
}
|
}
|
||||||
|
|
||||||
extendBuild (fn) {
|
extendBuild(fn) {
|
||||||
this.options.build.extend = chainFn(this.options.build.extend, fn)
|
this.options.build.extend = chainFn(this.options.build.extend, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
extendRoutes (fn) {
|
extendRoutes(fn) {
|
||||||
this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn)
|
this.options.router.extendRoutes = chainFn(this.options.router.extendRoutes, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
requireModule (moduleOpts) {
|
requireModule(moduleOpts) {
|
||||||
if (this.modules.indexOf(moduleOpts) !== -1 || this.modules.indexOf(moduleOpts.src) !== -1) {
|
if (this.modules.indexOf(moduleOpts) !== -1 || this.modules.indexOf(moduleOpts.src) !== -1) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -77,7 +90,7 @@ class Module {
|
|||||||
return this.addModule(moduleOpts)
|
return this.addModule(moduleOpts)
|
||||||
}
|
}
|
||||||
|
|
||||||
addModule (moduleOpts) {
|
addModule(moduleOpts) {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (!moduleOpts) {
|
if (!moduleOpts) {
|
||||||
return
|
return
|
||||||
|
@ -121,14 +121,6 @@ class Nuxt {
|
|||||||
this.utils = utils
|
this.utils = utils
|
||||||
// Add module integration
|
// Add module integration
|
||||||
this.module = new Module(this)
|
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) {
|
close (callback) {
|
||||||
|
@ -14,7 +14,7 @@ test.before('Init Nuxt.js', async t => {
|
|||||||
rootDir: resolve(__dirname, 'fixtures/basic'),
|
rootDir: resolve(__dirname, 'fixtures/basic'),
|
||||||
dev: true
|
dev: true
|
||||||
}
|
}
|
||||||
nuxt = await new Nuxt(options)
|
nuxt = new Nuxt(options)
|
||||||
await nuxt.build()
|
await nuxt.build()
|
||||||
server = new nuxt.Server(nuxt)
|
server = new nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
server.listen(port, 'localhost')
|
||||||
|
@ -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) => {
|
return new Promise((resolve) => {
|
||||||
var oldExit = process.exit
|
var oldExit = process.exit
|
||||||
var oldCE = console.error // eslint-disable-line no-console
|
var oldCE = console.error // eslint-disable-line no-console
|
||||||
|
@ -17,7 +17,7 @@ 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
|
||||||
nuxt = await 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) {}
|
||||||
|
@ -16,7 +16,7 @@ test.before('Init Nuxt.js', async t => {
|
|||||||
rootDir: resolve(__dirname, 'fixtures/basic'),
|
rootDir: resolve(__dirname, 'fixtures/basic'),
|
||||||
dev: false
|
dev: false
|
||||||
}
|
}
|
||||||
nuxt = await new Nuxt(options)
|
nuxt = new Nuxt(options)
|
||||||
await nuxt.build()
|
await nuxt.build()
|
||||||
server = new nuxt.Server(nuxt)
|
server = new nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
server.listen(port, 'localhost')
|
||||||
|
@ -13,7 +13,7 @@ test.before('Init Nuxt.js', async t => {
|
|||||||
rootDir: resolve(__dirname, 'fixtures/children'),
|
rootDir: resolve(__dirname, 'fixtures/children'),
|
||||||
dev: false
|
dev: false
|
||||||
}
|
}
|
||||||
nuxt = await new Nuxt(options)
|
nuxt = new Nuxt(options)
|
||||||
await nuxt.build()
|
await nuxt.build()
|
||||||
server = new nuxt.Server(nuxt)
|
server = new nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
server.listen(port, 'localhost')
|
||||||
|
@ -6,7 +6,7 @@ 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 = await new Nuxt({
|
const nuxt = new Nuxt({
|
||||||
rootDir: resolve(__dirname, 'fixtures/dynamic-routes'),
|
rootDir: resolve(__dirname, 'fixtures/dynamic-routes'),
|
||||||
dev: false
|
dev: false
|
||||||
})
|
})
|
||||||
|
@ -13,7 +13,7 @@ test.before('Init Nuxt.js', async t => {
|
|||||||
rootDir: resolve(__dirname, 'fixtures/error'),
|
rootDir: resolve(__dirname, 'fixtures/error'),
|
||||||
dev: false
|
dev: false
|
||||||
}
|
}
|
||||||
nuxt = await new Nuxt(options)
|
nuxt = new Nuxt(options)
|
||||||
await nuxt.build()
|
await nuxt.build()
|
||||||
server = new nuxt.Server(nuxt)
|
server = new nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
server.listen(port, 'localhost')
|
||||||
|
@ -8,7 +8,7 @@ test('Nuxt.js Class', t => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test('Nuxt.js Instance', async t => {
|
test('Nuxt.js Instance', async t => {
|
||||||
const nuxt = await new Nuxt()
|
const nuxt = new Nuxt()
|
||||||
t.is(typeof nuxt, 'object')
|
t.is(typeof nuxt, 'object')
|
||||||
t.is(nuxt.dev, true)
|
t.is(nuxt.dev, true)
|
||||||
t.is(typeof nuxt.build, 'function')
|
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 => {
|
test.serial('Fail when build not done and try to render', async t => {
|
||||||
const nuxt = await new Nuxt({
|
const nuxt = new Nuxt({
|
||||||
dev: false,
|
dev: false,
|
||||||
rootDir: resolve(__dirname, 'fixtures/empty')
|
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 => {
|
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,
|
dev: false,
|
||||||
rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages')
|
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 => {
|
test.serial('Fail to build when no pages/ directory', async t => {
|
||||||
const nuxt = await new Nuxt({
|
const nuxt = new Nuxt({
|
||||||
dev: false,
|
dev: false,
|
||||||
rootDir: resolve(__dirname)
|
rootDir: resolve(__dirname)
|
||||||
})
|
})
|
||||||
|
@ -17,7 +17,7 @@ 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
|
||||||
nuxt = await new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
await nuxt.build()
|
await nuxt.build()
|
||||||
server = new nuxt.Server(nuxt)
|
server = new nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
server.listen(port, 'localhost')
|
||||||
|
@ -5,7 +5,7 @@ let utils
|
|||||||
// Init nuxt.js and create server listening on localhost:4000
|
// Init nuxt.js and create server listening on localhost:4000
|
||||||
test.before('Init Nuxt.js', async t => {
|
test.before('Init Nuxt.js', async t => {
|
||||||
const Nuxt = require('../')
|
const Nuxt = require('../')
|
||||||
let nuxt = await new Nuxt({ dev: false })
|
let nuxt = new Nuxt({ dev: false })
|
||||||
utils = nuxt.utils
|
utils = nuxt.utils
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ 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
|
||||||
nuxt = await new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
await nuxt.build()
|
await nuxt.build()
|
||||||
server = new nuxt.Server(nuxt)
|
server = new nuxt.Server(nuxt)
|
||||||
server.listen(port, 'localhost')
|
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'))
|
let config = require(resolve(rootDir, 'nuxt.config.js'))
|
||||||
config.rootDir = rootDir
|
config.rootDir = rootDir
|
||||||
config.dev = false
|
config.dev = false
|
||||||
nuxt = await new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user