From 25d106e2ababd9ac6d38943ab8ad1ba3466477da Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 14 Jun 2017 23:21:14 +0430 Subject: [PATCH] runBuild option --- bin/nuxt-build | 4 +- bin/nuxt-generate | 1 + lib/builder.js | 3 - lib/defaults.js | 6 ++ lib/generator.js | 4 +- lib/nuxt.js | 29 +++---- lib/renderer.js | 4 +- test/basic.dev.test.js | 2 +- test/basic.fail.generate.test.js | 1 + test/basic.generate.test.js | 5 +- test/basic.test.js | 5 +- test/children.test.js | 6 +- test/dynamic-routes.test.js | 130 ++++++++++++++++--------------- test/error.test.js | 6 +- test/index.test.js | 9 ++- test/module.test.js | 3 +- test/with-config.test.js | 4 +- 17 files changed, 123 insertions(+), 99 deletions(-) diff --git a/bin/nuxt-build b/bin/nuxt-build index 53b125765e..8c4fffd038 100755 --- a/bin/nuxt-build +++ b/bin/nuxt-build @@ -51,6 +51,8 @@ if (typeof options.rootDir !== 'string') { } // Create production build when calling `nuxt build` options.dev = false +options.runBuild = true // Force doing production build before init + // Analyze option options.build = options.build || {} if (argv.analyze) { @@ -59,7 +61,7 @@ if (argv.analyze) { console.log('[nuxt] Building...') // eslint-disable-line no-console var nuxt = module.exports = new Nuxt(options) -nuxt.build() +nuxt.init() .then(() => { console.log('[nuxt] Building done') // eslint-disable-line no-console }) diff --git a/bin/nuxt-generate b/bin/nuxt-generate index b39f8de6e6..e9607b174a 100755 --- a/bin/nuxt-generate +++ b/bin/nuxt-generate @@ -47,6 +47,7 @@ if (typeof options.rootDir !== 'string') { options.rootDir = rootDir } 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 var nuxt = module.exports = new Nuxt(options) diff --git a/lib/builder.js b/lib/builder.js index 45b779a5e7..160cb58518 100644 --- a/lib/builder.js +++ b/lib/builder.js @@ -74,9 +74,6 @@ export default class Builder extends Tapable { } 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' if (this._nuxtPages) { diff --git a/lib/defaults.js b/lib/defaults.js index d34475e5e0..1a2195cc73 100755 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -45,11 +45,17 @@ export default function defaults (_options) { options.store = true } + // runBuild can not be enabled for dev === true + if (options.dev === true) { + options.runBuild = false + } + return options } const defaultOptions = { dev: (process.env.NODE_ENV !== 'production'), + runBuild: false, buildDir: '.nuxt', build: { analyze: false, diff --git a/lib/generator.js b/lib/generator.js index df91c8f03c..3cc54b6d93 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -30,8 +30,8 @@ export default class Generator extends Tapable { let distPath = resolve(this.options.rootDir, this.options.generate.dir) let distNuxtPath = join(distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath)) - // Launch build process - await this.nuxt.build() + // Wait for nuxt be ready + await this.nuxt.init() // Clean destination folder await remove(distPath) diff --git a/lib/nuxt.js b/lib/nuxt.js index 415909c329..38cf9769b7 100644 --- a/lib/nuxt.js +++ b/lib/nuxt.js @@ -23,6 +23,18 @@ export default class Nuxt extends Tapable { this.renderRoute = this.renderer.renderRoute.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) } @@ -31,17 +43,12 @@ export default class Nuxt extends Tapable { 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 - await this.applyPluginsAsync('beforeInit') - await this.applyPluginsAsync('init') - this.initialized = true - this.applyPluginsAsync('afterInit').catch(this.errorHandler) + await this.applyPluginsAsync('beforeInit') // 1- Modules + await this.applyPluginsAsync('init') // 2- Builder + await this.applyPluginsAsync('afterInit') // 3- Renderer + this.initialized = true return this } @@ -63,10 +70,6 @@ export default class Nuxt extends Tapable { return this._generator } - build () { - return this.builder.build.apply(this.builder, arguments) - } - generate () { return this.generator.generate.apply(this.generator, arguments) } diff --git a/lib/renderer.js b/lib/renderer.js index f73604ce86..f51fb396df 100644 --- a/lib/renderer.js +++ b/lib/renderer.js @@ -44,7 +44,7 @@ export default class Renderer extends Tapable { this._init = this.init().catch(this.nuxt.errorHandler) } else { // Wait for hook - this.nuxt.plugin('init', () => { + this.nuxt.plugin('afterInit', () => { this._init = this.init() return this._init }) @@ -123,8 +123,6 @@ export default class Renderer extends Tapable { // Promisify renderToString this.bundleRenderer.renderToString = pify(this.bundleRenderer.renderToString) - - debug('ready') } async render (req, res) { diff --git a/test/basic.dev.test.js b/test/basic.dev.test.js index 58ed448ad4..108bd5c35d 100644 --- a/test/basic.dev.test.js +++ b/test/basic.dev.test.js @@ -15,7 +15,7 @@ test.before('Init Nuxt.js', async t => { dev: true } nuxt = new Nuxt(options) - await nuxt.build() + await nuxt.init() 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 996dc142fd..b8861e8e0a 100644 --- a/test/basic.fail.generate.test.js +++ b/test/basic.fail.generate.test.js @@ -6,6 +6,7 @@ test('Fail with routes() which throw an error', async t => { const options = { rootDir: resolve(__dirname, 'fixtures/basic'), dev: false, + runBuild: true, generate: { async routes () { throw new Error('Not today!') diff --git a/test/basic.generate.test.js b/test/basic.generate.test.js index cc0c160912..cd270c13ac 100644 --- a/test/basic.generate.test.js +++ b/test/basic.generate.test.js @@ -4,6 +4,7 @@ import http from 'http' import serveStatic from 'serve-static' import finalhandler from 'finalhandler' import rp from 'request-promise-native' + const port = 4002 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')) config.rootDir = rootDir config.dev = false + config.runBuild = true nuxt = new Nuxt(config) try { await nuxt.generate() // throw an error (of /validate route) - } catch (err) {} + } catch (err) { + } const serve = serveStatic(resolve(__dirname, 'fixtures/basic/dist')) server = http.createServer((req, res) => { serve(req, res, finalhandler(req, res)) diff --git a/test/basic.test.js b/test/basic.test.js index 2b246363cf..71372e0636 100755 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -14,10 +14,11 @@ test.before('Init Nuxt.js', async t => { const Nuxt = require('../') const options = { rootDir: resolve(__dirname, 'fixtures/basic'), - dev: false + dev: false, + runBuild: true } nuxt = new Nuxt(options) - await nuxt.build() + await nuxt.init() server = new Nuxt.Server(nuxt) server.listen(port, 'localhost') }) diff --git a/test/children.test.js b/test/children.test.js index d79f5a768c..5bae366e0b 100644 --- a/test/children.test.js +++ b/test/children.test.js @@ -1,5 +1,6 @@ import test from 'ava' import { resolve } from 'path' + const port = 4004 // const url = (route) => 'http://localhost:' + port + route @@ -11,10 +12,11 @@ test.before('Init Nuxt.js', async t => { const Nuxt = require('../') const options = { rootDir: resolve(__dirname, 'fixtures/children'), - dev: false + dev: false, + runBuild: true } nuxt = new Nuxt(options) - await nuxt.build() + await nuxt.init() server = new Nuxt.Server(nuxt) server.listen(port, 'localhost') }) diff --git a/test/dynamic-routes.test.js b/test/dynamic-routes.test.js index 40bd5446d2..ebd1760d44 100644 --- a/test/dynamic-routes.test.js +++ b/test/dynamic-routes.test.js @@ -2,79 +2,81 @@ import test from 'ava' import { resolve } from 'path' import fs from 'fs' import pify from 'pify' + const readFile = pify(fs.readFile) test.before('Init Nuxt.js', async t => { const Nuxt = require('../') const nuxt = new Nuxt({ rootDir: resolve(__dirname, 'fixtures/dynamic-routes'), - dev: false + dev: false, + runBuild: true }) - await nuxt.build() + await nuxt.init() }) test('Check .nuxt/router.js', t => { return readFile(resolve(__dirname, './fixtures/dynamic-routes/.nuxt/router.js'), 'utf-8') - .then((routerFile) => { - routerFile = routerFile - .slice(routerFile.indexOf('routes: [')) - .replace('routes: [', '[') - .replace(/ _[0-9A-z]+,/g, ' "",') - routerFile = routerFile.substr(routerFile.indexOf('['), routerFile.lastIndexOf(']') + 1) - let routes = eval('( ' + routerFile + ')') // eslint-disable-line no-eval - // pages/index.vue - t.is(routes[0].path, '/') - t.is(routes[0].name, 'index') - // pages/test/index.vue - t.is(routes[1].path, '/test') - t.is(routes[1].name, 'test') - // pages/posts.vue - t.is(routes[2].path, '/posts') - t.is(routes[2].name, 'posts') - t.is(routes[2].children.length, 1) - // pages/posts/_id.vue - t.is(routes[2].children[0].path, ':id?') - t.is(routes[2].children[0].name, 'posts-id') - // pages/parent.vue - t.is(routes[3].path, '/parent') - t.falsy(routes[3].name) // parent route has no name - // pages/parent/*.vue - t.is(routes[3].children.length, 3) // parent has 3 children - t.deepEqual(routes[3].children.map((r) => r.path), ['', 'teub', 'child']) - t.deepEqual(routes[3].children.map((r) => r.name), ['parent', 'parent-teub', 'parent-child']) - // pages/test/projects/index.vue - t.is(routes[4].path, '/test/projects') - t.is(routes[4].name, 'test-projects') - // pages/test/users.vue - t.is(routes[5].path, '/test/users') - t.falsy(routes[5].name) // parent route has no name - // pages/test/users/*.vue - t.is(routes[5].children.length, 5) // parent has 5 children - t.deepEqual(routes[5].children.map((r) => r.path), ['', 'projects', 'projects/:category', ':id', ':index/teub']) - t.deepEqual(routes[5].children.map((r) => r.name), ['test-users', 'test-users-projects', 'test-users-projects-category', 'test-users-id', 'test-users-index-teub']) - // pages/test/songs/toto.vue - t.is(routes[6].path, '/test/songs/toto') - t.is(routes[6].name, 'test-songs-toto') - // pages/test/songs/_id.vue - t.is(routes[7].path, '/test/songs/:id?') - t.is(routes[7].name, 'test-songs-id') - // pages/test/projects/_category.vue - t.is(routes[8].path, '/test/projects/:category') - t.is(routes[8].name, 'test-projects-category') - // pages/users/_id.vue - t.is(routes[9].path, '/users/:id?') - t.is(routes[9].name, 'users-id') - // pages/test/_.vue - t.is(routes[10].path, '/test/*') - t.is(routes[10].name, 'test-all') - // pages/_slug.vue - t.is(routes[11].path, '/:slug') - t.is(routes[11].name, 'slug') - // pages/_key/_id.vue - t.is(routes[12].path, '/:key/:id?') - t.is(routes[12].name, 'key-id') - // pages/_.vue - t.is(routes[13].path, '/*') - t.is(routes[13].name, 'all') - }) + .then((routerFile) => { + routerFile = routerFile + .slice(routerFile.indexOf('routes: [')) + .replace('routes: [', '[') + .replace(/ _[0-9A-z]+,/g, ' "",') + routerFile = routerFile.substr(routerFile.indexOf('['), routerFile.lastIndexOf(']') + 1) + let routes = eval('( ' + routerFile + ')') // eslint-disable-line no-eval + // pages/index.vue + t.is(routes[0].path, '/') + t.is(routes[0].name, 'index') + // pages/test/index.vue + t.is(routes[1].path, '/test') + t.is(routes[1].name, 'test') + // pages/posts.vue + t.is(routes[2].path, '/posts') + t.is(routes[2].name, 'posts') + t.is(routes[2].children.length, 1) + // pages/posts/_id.vue + t.is(routes[2].children[0].path, ':id?') + t.is(routes[2].children[0].name, 'posts-id') + // pages/parent.vue + t.is(routes[3].path, '/parent') + t.falsy(routes[3].name) // parent route has no name + // pages/parent/*.vue + t.is(routes[3].children.length, 3) // parent has 3 children + t.deepEqual(routes[3].children.map((r) => r.path), ['', 'teub', 'child']) + t.deepEqual(routes[3].children.map((r) => r.name), ['parent', 'parent-teub', 'parent-child']) + // pages/test/projects/index.vue + t.is(routes[4].path, '/test/projects') + t.is(routes[4].name, 'test-projects') + // pages/test/users.vue + t.is(routes[5].path, '/test/users') + t.falsy(routes[5].name) // parent route has no name + // pages/test/users/*.vue + t.is(routes[5].children.length, 5) // parent has 5 children + t.deepEqual(routes[5].children.map((r) => r.path), ['', 'projects', 'projects/:category', ':id', ':index/teub']) + t.deepEqual(routes[5].children.map((r) => r.name), ['test-users', 'test-users-projects', 'test-users-projects-category', 'test-users-id', 'test-users-index-teub']) + // pages/test/songs/toto.vue + t.is(routes[6].path, '/test/songs/toto') + t.is(routes[6].name, 'test-songs-toto') + // pages/test/songs/_id.vue + t.is(routes[7].path, '/test/songs/:id?') + t.is(routes[7].name, 'test-songs-id') + // pages/test/projects/_category.vue + t.is(routes[8].path, '/test/projects/:category') + t.is(routes[8].name, 'test-projects-category') + // pages/users/_id.vue + t.is(routes[9].path, '/users/:id?') + t.is(routes[9].name, 'users-id') + // pages/test/_.vue + t.is(routes[10].path, '/test/*') + t.is(routes[10].name, 'test-all') + // pages/_slug.vue + t.is(routes[11].path, '/:slug') + t.is(routes[11].name, 'slug') + // pages/_key/_id.vue + t.is(routes[12].path, '/:key/:id?') + t.is(routes[12].name, 'key-id') + // pages/_.vue + t.is(routes[13].path, '/*') + t.is(routes[13].name, 'all') + }) }) diff --git a/test/error.test.js b/test/error.test.js index c11446ba1a..ddd3fa4ada 100644 --- a/test/error.test.js +++ b/test/error.test.js @@ -1,5 +1,6 @@ import test from 'ava' import { resolve } from 'path' + const port = 4005 const url = (route) => 'http://localhost:' + port + route @@ -11,10 +12,11 @@ test.before('Init Nuxt.js', async t => { const Nuxt = require('../') const options = { rootDir: resolve(__dirname, 'fixtures/error'), - dev: false + dev: false, + runBuild: true } nuxt = new Nuxt(options) - await nuxt.build() + await nuxt.init() server = new Nuxt.Server(nuxt) server.listen(port, 'localhost') }) diff --git a/test/index.test.js b/test/index.test.js index cafaa08158..b54b304715 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -10,6 +10,7 @@ test('Nuxt.js Class', t => { test.serial('Nuxt.js Instance', async t => { const nuxt = new Nuxt({ dev: false, + runBuild: true, rootDir: resolve(__dirname, 'fixtures', 'empty') }) 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.generate, 'function') t.is(typeof nuxt._init.then, 'function') - await nuxt.build() + await nuxt.init() t.is(nuxt.initialized, true) }) test.serial('Fail to build when no pages/ directory but is in the parent', t => { const nuxt = new Nuxt({ dev: false, + runBuild: true, rootDir: resolve(__dirname, 'fixtures', 'empty', 'pages') }) - return nuxt.build().catch(err => { + return nuxt.init().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?')) @@ -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 => { const nuxt = new Nuxt({ dev: false, + runBuild: true, rootDir: resolve(__dirname) }) - return nuxt.build().catch(err => { + return nuxt.init().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')) diff --git a/test/module.test.js b/test/module.test.js index 14b79171c8..646408e92d 100755 --- a/test/module.test.js +++ b/test/module.test.js @@ -15,8 +15,9 @@ test.before('Init Nuxt.js', async t => { let config = require(resolve(rootDir, 'nuxt.config.js')) config.rootDir = rootDir config.dev = false + config.runBuild = true nuxt = new Nuxt(config) - await nuxt.build() + await nuxt.init() server = new nuxt.Server(nuxt) server.listen(port, 'localhost') }) diff --git a/test/with-config.test.js b/test/with-config.test.js index 0cab513c7c..78668a2688 100644 --- a/test/with-config.test.js +++ b/test/with-config.test.js @@ -15,8 +15,9 @@ test.before('Init Nuxt.js', async t => { let config = require(resolve(rootDir, 'nuxt.config.js')) config.rootDir = rootDir config.dev = false + config.runBuild = true nuxt = new Nuxt(config) - await nuxt.build() + await nuxt.init() server = new Nuxt.Server(nuxt) 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')) config.rootDir = rootDir config.dev = false + config.runBuild = true nuxt = new Nuxt(config) await nuxt.init() })