From c5ca8c64f18d29b83137399b2aa5f6164f53b3c6 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 31 May 2017 18:51:16 +0430 Subject: [PATCH] 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 --- bin/nuxt-build | 19 +++++------ bin/nuxt-dev | 51 ++++++++++++++-------------- bin/nuxt-generate | 19 +++++------ bin/nuxt-start | 11 +++--- examples/with-ava/test/index.test.js | 2 +- lib/build.js | 4 ++- lib/module.js | 37 +++++++++++++------- lib/nuxt.js | 8 ----- test/basic.dev.test.js | 2 +- test/basic.fail.generate.test.js | 2 +- test/basic.generate.test.js | 2 +- test/basic.test.js | 2 +- test/children.test.js | 2 +- test/dynamic-routes.test.js | 2 +- test/error.test.js | 2 +- test/index.test.js | 8 ++--- test/module.test.js | 2 +- test/utils.test.js | 2 +- test/with-config.test.js | 4 +-- 19 files changed, 92 insertions(+), 89 deletions(-) diff --git a/bin/nuxt-build b/bin/nuxt-build index c85d1b6fed..1af998d2be 100755 --- a/bin/nuxt-build +++ b/bin/nuxt-build @@ -52,13 +52,12 @@ if (analyzeBuild) { } console.log('[nuxt] Building...') // eslint-disable-line no-console -new Nuxt(options).then(nuxt => { - nuxt.build() - .then(() => { - console.log('[nuxt] Building done') // eslint-disable-line no-console - }) - .catch((err) => { - console.error(err) // eslint-disable-line no-console - process.exit(1) - }) -}) +var nuxt = module.exports = new Nuxt(options) +nuxt.build() + .then(() => { + console.log('[nuxt] Building done') // eslint-disable-line no-console + }) + .catch((err) => { + console.error(err) // eslint-disable-line no-console + process.exit(1) + }) diff --git a/bin/nuxt-dev b/bin/nuxt-dev index 351a82a680..7b2a36bda2 100755 --- a/bin/nuxt-dev +++ b/bin/nuxt-dev @@ -39,19 +39,20 @@ 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) - listenOnConfigChanges(nuxt, server) +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) - nuxt.build() - .catch((err) => { - console.error(err) // eslint-disable-line no-console - process.exit(1) - }) -}) +listenOnConfigChanges(nuxt, server) -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 var build = _.debounce(() => { debug('[nuxt.config.js] changed') @@ -66,20 +67,20 @@ function listenOnConfigChanges (nuxt, server) { } options.rootDir = rootDir nuxt.close() - .then(() => { - nuxt.renderer = null - debug('Rebuilding the app...') - return (new Nuxt(options)).then(nuxt => nuxt.build()) - }) - .then((nuxt) => { - server.nuxt = nuxt - }) - .catch((error) => { - console.error('Error while rebuild the app:', error) // eslint-disable-line no-console - process.exit(1) - }) + .then(() => { + nuxt.renderer = null + debug('Rebuilding the app...') + return new Nuxt(options).build() + }) + .then((nuxt) => { + server.nuxt = nuxt + }) + .catch((error) => { + console.error('Error while rebuild the app:', error) // eslint-disable-line no-console + process.exit(1) + }) }, 200) var nuxtConfigFile = resolve(rootDir, nuxtConfigFileName) - chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, { ignoreInitial: true })) - .on('all', build) + chokidar.watch(nuxtConfigFile, Object.assign({}, nuxt.options.watchers.chokidar, {ignoreInitial: true})) + .on('all', build) } diff --git a/bin/nuxt-generate b/bin/nuxt-generate index 8d085b666c..c187810fa1 100755 --- a/bin/nuxt-generate +++ b/bin/nuxt-generate @@ -20,13 +20,12 @@ 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 => { - nuxt.generate() - .then(() => { - console.log('[nuxt] Generate done') // eslint-disable-line no-console - }) - .catch((err) => { - console.error(err) // eslint-disable-line no-console - process.exit(1) - }) -}) +var nuxt = module.exports = new Nuxt(options) +nuxt.generate() + .then(() => { + console.log('[nuxt] Generate done') // eslint-disable-line no-console + }) + .catch((err) => { + console.error(err) // eslint-disable-line no-console + process.exit(1) + }) diff --git a/bin/nuxt-start b/bin/nuxt-start index f39daf9b19..8af26fbaf4 100755 --- a/bin/nuxt-start +++ b/bin/nuxt-start @@ -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) diff --git a/examples/with-ava/test/index.test.js b/examples/with-ava/test/index.test.js index e5249406f1..21c25e9320 100755 --- a/examples/with-ava/test/index.test.js +++ b/examples/with-ava/test/index.test.js @@ -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') diff --git a/lib/build.js b/lib/build.js index f548c28d75..a5793131a3 100644 --- a/lib/build.js +++ b/lib/build.js @@ -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'))) { diff --git a/lib/module.js b/lib/module.js index 4bd726d085..17f73a2edd 100755 --- a/lib/module.js +++ b/lib/module.js @@ -4,16 +4,30 @@ 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) { + constructor(nuxt) { this.nuxt = nuxt this.options = nuxt.options 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 */ if (!vendor) { return @@ -21,7 +35,7 @@ class Module { this.options.build.vendor = uniq(this.options.build.vendor.concat(vendor)) } - addTemplate (template) { + addTemplate(template) { /* istanbul ignore if */ if (!template) { return @@ -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 @@ -48,7 +61,7 @@ class Module { return templateObj } - addPlugin (template) { + addPlugin(template) { const {dst} = this.addTemplate(template) // Add to nuxt plugins this.options.plugins.push({ @@ -57,19 +70,19 @@ class Module { }) } - addServerMiddleware (middleware) { + addServerMiddleware(middleware) { this.options.serverMiddleware.push(middleware) } - extendBuild (fn) { + extendBuild(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) } - requireModule (moduleOpts) { + requireModule(moduleOpts) { if (this.modules.indexOf(moduleOpts) !== -1 || this.modules.indexOf(moduleOpts.src) !== -1) { return false } @@ -77,7 +90,7 @@ class Module { return this.addModule(moduleOpts) } - addModule (moduleOpts) { + addModule(moduleOpts) { /* istanbul ignore if */ if (!moduleOpts) { return diff --git a/lib/nuxt.js b/lib/nuxt.js index 64fb633b1b..c045e483c2 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -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) { diff --git a/test/basic.dev.test.js b/test/basic.dev.test.js index 9d1f02f968..42e60e22fa 100644 --- a/test/basic.dev.test.js +++ b/test/basic.dev.test.js @@ -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') diff --git a/test/basic.fail.generate.test.js b/test/basic.fail.generate.test.js index b96a8b343d..c1c85a30fa 100644 --- a/test/basic.fail.generate.test.js +++ b/test/basic.fail.generate.test.js @@ -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 diff --git a/test/basic.generate.test.js b/test/basic.generate.test.js index 02855fb73a..cc0c160912 100644 --- a/test/basic.generate.test.js +++ b/test/basic.generate.test.js @@ -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) {} diff --git a/test/basic.test.js b/test/basic.test.js index 514ccf39c0..14a63aaa5b 100755 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -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') diff --git a/test/children.test.js b/test/children.test.js index 261b878380..2536ed4e22 100644 --- a/test/children.test.js +++ b/test/children.test.js @@ -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') diff --git a/test/dynamic-routes.test.js b/test/dynamic-routes.test.js index 126d433f88..40bd5446d2 100644 --- a/test/dynamic-routes.test.js +++ b/test/dynamic-routes.test.js @@ -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 }) diff --git a/test/error.test.js b/test/error.test.js index 709b37b347..26fba45f6e 100644 --- a/test/error.test.js +++ b/test/error.test.js @@ -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') diff --git a/test/index.test.js b/test/index.test.js index 40be484e4a..53f05b9afa 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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) }) diff --git a/test/module.test.js b/test/module.test.js index c617228e5c..58e15de574 100755 --- a/test/module.test.js +++ b/test/module.test.js @@ -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') diff --git a/test/utils.test.js b/test/utils.test.js index 052db5a900..3a2aff00ff 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -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 }) diff --git a/test/with-config.test.js b/test/with-config.test.js index 0e40937b32..07c299f245 100644 --- a/test/with-config.test.js +++ b/test/with-config.test.js @@ -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) })