fixes + improvements

This commit is contained in:
Pooya Parsa 2017-06-14 02:39:03 +04:30
parent 37c92e7398
commit c519c3edff
7 changed files with 74 additions and 94 deletions

View File

@ -53,38 +53,43 @@ export default class Builder extends Tapable {
}
// Register lifecycle hooks
if (this.nuxt.options.dev) {
// Don't await for build on dev (faster startup)
this.nuxt.plugin('afterInit', () => {
if (this.nuxt.dev) {
// But don't await for builder on dev (faster startup)
this.build().catch(err => {
// eslint-disable-next-line no-console
console.error(err)
this.build().catch(this.nuxt.errorHandler)
})
} else {
this.nuxt.plugin('init', () => {
// Guess it is build or production
// If build is not called it may be nuxt.start
if (this._buildStatus === STATUS.INITIAL) {
return this.production()
}
})
}
this._buildStatus = STATUS.INITIAL
}
async build () {
// Ensure nuxt initialized
await this.nuxt.init()
// Avoid calling this method multiple times
if (this._buildStatus === STATUS.BUILD_DONE) {
return this
}
// If building
if (this._buildStatus === STATUS.BUILDING) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(this.build())
}, 300)
}, 1000)
})
}
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'
@ -92,11 +97,10 @@ export default class Builder extends Tapable {
if (!fs.existsSync(join(this.options.srcDir, 'pages'))) {
let dir = this.options.srcDir
if (fs.existsSync(join(this.options.srcDir, '..', 'pages'))) {
console.error(`> No 'pages' directory found in ${dir}. Did you mean to run 'nuxt' in the parent ('../') directory?`) // eslint-disable-line no-console
throw new Error(`No \`pages\` directory found in ${dir}. Did you mean to run \`nuxt\` in the parent (\`../\`) directory?`)
} else {
console.error(`> Couldn't find a 'pages' directory in ${dir}. Please create one under the project root`) // eslint-disable-line no-console
throw new Error(`Couldn't find a \`pages\` directory in ${dir}. Please create one under the project root`)
}
process.exit(1)
}
}
@ -124,10 +128,7 @@ export default class Builder extends Tapable {
const bundlePath = join(serverConfig.output.path, 'server-bundle.json')
const manifestPath = join(serverConfig.output.path, 'client-manifest.json')
if (!fs.existsSync(bundlePath) || !fs.existsSync(manifestPath)) {
console.error('[warning] No build files found, trying to build for production') // eslint-disable-line no-console
return this.build().then(() => {
return this.production()
})
throw new Error('No build files found, please run `nuxt build` before launching `nuxt start`')
}
const bundle = fs.readFileSync(bundlePath, 'utf8')
const manifest = fs.readFileSync(manifestPath, 'utf8')

View File

@ -24,9 +24,6 @@ export default class Generator extends Tapable {
let errors = []
let generateRoutes = []
// Wait for nuxt.js to be ready
await this.nuxt.init()
// Set variables
let srcStaticPath = resolve(this.options.srcDir, 'static')
let srcBuiltPath = resolve(this.options.buildDir, 'dist')
@ -55,7 +52,6 @@ export default class Generator extends Tapable {
} catch (e) {
console.error('Could not resolve routes') // eslint-disable-line no-console
console.error(e) // eslint-disable-line no-console
process.exit(1)
throw e // eslint-disable-line no-unreachable
}
}

View File

@ -96,7 +96,7 @@ export default class ModuleContainer extends Tapable {
const originalSrc = moduleOpts.src || moduleOpts
// Resolve module
let module = originalSrc
try {
if (typeof module === 'string') {
// Using ~ or ./ shorthand modules are resolved from project srcDir
if (module.indexOf('~') === 0 || module.indexOf('./') === 0) {
@ -105,19 +105,11 @@ export default class ModuleContainer extends Tapable {
// eslint-disable-next-line no-eval
module = eval('require')(module)
}
} catch (e) /* istanbul ignore next */ {
// eslint-disable-next-line no-console
console.error('[nuxt] Unable to resolve module', module)
// eslint-disable-next-line no-console
console.error(e)
process.exit(0)
}
// Validate module
/* istanbul ignore if */
if (typeof module !== 'function') {
// eslint-disable-next-line no-console
console.error(`[nuxt] Module [${originalSrc}] should export a function`)
process.exit(1)
throw new Error(`[nuxt] Module ${JSON.stringify(originalSrc)} should export a function`)
}
// Module meta
if (!module.meta) {

View File

@ -62,11 +62,12 @@ export default class Nuxt extends Tapable {
this.dir = options.rootDir
this.srcDir = options.srcDir
this.buildDir = options.buildDir
this.dev = options.dev
this.Server = Nuxt.Server
this.Utils = Nuxt.Utils
// eslint-disable-next-line no-console
this._init = this.init().catch(console.error)
this.errorHandler = this.errorHandler.bind(this)
this._init = this.init().catch(this.errorHandler)
this.initialized = false
}
@ -74,14 +75,35 @@ export default class Nuxt extends Tapable {
if (this._init) {
return this._init
}
// Wait for all components to be ready
// Including modules
await this.applyPluginsAsync('beforeInit')
// Including Build
await this.applyPluginsAsync('init')
this.applyPluginsAsync('afterInit')
// Extra jobs
this.applyPluginsAsync('afterInit').catch(this.errorHandler)
this.initialized = true
return this
}
errorHandler () {
// Global error handler
// Silent
if (this.options.errorHandler === false) {
return
}
// Custom eventHandler
if (typeof this.options.errorHandler === 'function') {
return this.options.errorHandler.apply(this, arguments)
}
// Default
// eslint-disable-next-line no-console
console.error.apply(this, arguments)
process.exit(1)
}
async close (callback) {
let promises = []
/* istanbul ignore if */

View File

@ -183,7 +183,7 @@ export default class Renderer extends Tapable {
console.error('Fail when calling nuxt.renderAndGetWindow(url)') // eslint-disable-line no-console
console.error('jsdom module is not installed') // eslint-disable-line no-console
console.error('Please install jsdom with: npm install --save-dev jsdom') // eslint-disable-line no-console
process.exit(1)
throw e
}
}
let options = {

View File

@ -7,29 +7,14 @@ test('Fail with routes() which throw an error', async t => {
rootDir: resolve(__dirname, 'fixtures/basic'),
dev: false,
generate: {
routes: function () {
return new Promise((resolve, reject) => {
reject(new Error('Not today!'))
})
async routes () {
throw new Error('Not today!')
}
}
}
const nuxt = new Nuxt(options)
return new Promise((resolve) => {
var oldExit = process.exit
var oldCE = console.error // eslint-disable-line no-console
var _log = ''
console.error = (s) => { _log += s } // eslint-disable-line no-console
process.exit = (code) => {
process.exit = oldExit
console.error = oldCE // eslint-disable-line no-console
t.is(code, 1)
t.true(_log.includes('Could not resolve routes'))
resolve()
}
nuxt.generate()
return nuxt.generate()
.catch((e) => {
t.true(e.message === 'Not today!')
})
})
})

View File

@ -21,44 +21,28 @@ test.serial('Nuxt.js Instance', async t => {
t.is(nuxt.initialized, true)
})
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', t => {
const nuxt = new Nuxt({
dev: true,
dev: false,
rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages')
})
return new Promise((resolve) => {
let oldExit = process.exit
let oldCE = console.error // eslint-disable-line no-console
let _log = ''
console.error = (s) => { _log += s } // eslint-disable-line no-console
process.exit = (code) => {
process.exit = oldExit
console.error = oldCE // eslint-disable-line no-console
t.is(code, 1)
t.true(_log.includes('No `pages` directory found. Did you mean to run `nuxt` in the parent (`../`) directory?'))
return nuxt.build().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?'))
resolve()
}
nuxt.build()
})
})
test.serial('Fail to build when no pages/ directory', async t => {
test.serial('Fail to build when no pages/ directory', t => {
const nuxt = new Nuxt({
dev: true,
dev: false,
rootDir: resolve(__dirname)
})
return new Promise((resolve) => {
let oldExit = process.exit
let oldCE = console.error // eslint-disable-line no-console
let _log = ''
console.error = (s) => { _log += s } // eslint-disable-line no-console
process.exit = (code) => {
process.exit = oldExit
console.error = oldCE // eslint-disable-line no-console
t.is(code, 1)
t.true(_log.includes('Couldn\'t find a `pages` directory. Please create one under the project root'))
return nuxt.build().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'))
resolve()
}
nuxt.build()
})
})