From b5ca6b7975e696704c9995aad6d78d31b5bcf03e Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 17 Jul 2017 23:56:41 +0430 Subject: [PATCH] feat: enhance tapables usage --- lib/builder/builder.js | 5 +---- lib/builder/generator.js | 5 +---- lib/core/module.js | 8 ++++---- lib/core/nuxt.js | 10 +++++----- lib/core/renderer.js | 22 ++-------------------- 5 files changed, 13 insertions(+), 37 deletions(-) diff --git a/lib/builder/builder.js b/lib/builder/builder.js index 34e7b24dc6..f847f4ff12 100644 --- a/lib/builder/builder.js +++ b/lib/builder/builder.js @@ -43,9 +43,6 @@ export default class Builder extends Tapable { // Helper to resolve build paths this.relativeToBuild = (...args) => relativeTo(this.options.buildDir, ...args) - // Call builder plugin on parent nuxt to notify all modules of builder existence - this.nuxt.applyPluginsAsync('builder', this).catch(this.nuxt.errorHandler) - this._buildStatus = STATUS.INITIAL } @@ -77,7 +74,7 @@ export default class Builder extends Tapable { // Wait for nuxt ready await this.nuxt.ready() - await this.applyPluginsAsync('build', this) + await this.nuxt.applyPluginsAsync('build', this) // Check if pages dir exists and warn if not this._nuxtPages = typeof this.options.build.createRoutes !== 'function' diff --git a/lib/builder/generator.js b/lib/builder/generator.js index 1019afafd6..ed58526cea 100644 --- a/lib/builder/generator.js +++ b/lib/builder/generator.js @@ -21,9 +21,6 @@ export default class Generator extends Tapable { this.srcBuiltPath = resolve(this.options.buildDir, 'dist') this.distPath = resolve(this.options.rootDir, this.options.generate.dir) this.distNuxtPath = join(this.distPath, (isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath)) - - // Call generator plugin on parent nuxt to notify all modules of generator existence - this.nuxt.applyPluginsAsync('generator', this).catch(this.nuxt.errorHandler) } async generate ({ build = true, init = true } = {}) { @@ -38,7 +35,7 @@ export default class Generator extends Tapable { await this.builder.build() } - await this.applyPluginsAsync('beforeGenerate', this) + await this.nuxt.applyPluginsAsync('generate', this) // Initialize dist directory if (init) { diff --git a/lib/core/module.js b/lib/core/module.js index 8876fab3b0..63f835fc6c 100755 --- a/lib/core/module.js +++ b/lib/core/module.js @@ -14,11 +14,11 @@ export default class ModuleContainer extends Tapable { this.nuxt = nuxt this.options = nuxt.options this.requiredModules = [] + } - this.nuxt.plugin('beforeInit', async () => { - await sequence(this.options.modules, this.addModule.bind(this)) - await this.applyPluginsAsync('ready', this) - }) + async _ready () { + await sequence(this.options.modules, this.addModule.bind(this)) + await this.nuxt.applyPluginsAsync('module', this) } addVendor (vendor) { diff --git a/lib/core/nuxt.js b/lib/core/nuxt.js index 7293e60552..b0ac429e5f 100644 --- a/lib/core/nuxt.js +++ b/lib/core/nuxt.js @@ -28,7 +28,7 @@ export default class Nuxt extends Tapable { this.renderRoute = this.renderer.renderRoute.bind(this.renderer) this.renderAndGetWindow = this.renderer.renderAndGetWindow.bind(this.renderer) - this._ready = this.ready() + this._ready = this.ready().catch(this.errorHandler) } async ready () { @@ -36,10 +36,9 @@ export default class Nuxt extends Tapable { return this._ready } - // Wait for all components to be ready - await this.applyPluginsAsync('beforeInit') // 1- Modules - await this.applyPluginsAsync('init') // 2- Builder - await this.applyPluginsAsync('afterInit') // 3- Renderer + await this.moduleContainer._ready() + await this.applyPluginsAsync('ready') + await this.renderer._ready() this.initialized = true return this @@ -73,6 +72,7 @@ export default class Nuxt extends Tapable { resolve() }) + // Add server.destroy(cb) method enableDestroy(server) }) diff --git a/lib/core/renderer.js b/lib/core/renderer.js index 8de1e42d87..f9cd1353bf 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -50,27 +50,9 @@ export default class Renderer extends Tapable { // Bind middleware to this context this.nuxtMiddleware = this.nuxtMiddleware.bind(this) this.errorMiddleware = this.errorMiddleware.bind(this) - - // Initialize - /* istanbul ignore if */ - if (nuxt.initialized) { - // If nuxt already initialized - this._ready = this.ready().catch(this.nuxt.errorHandler) - } else { - // Wait for hook - this.nuxt.plugin('afterInit', () => { - this._ready = this.ready() - return this._ready - }) - } } - async ready () { - /* istanbul ignore if */ - if (this._ready) { - return this._ready - } - + async _ready () { // Setup all middleWare await this.setupMiddleware() @@ -85,7 +67,7 @@ export default class Renderer extends Tapable { await this.loadResources() } - return this + await this.nuxt.applyPluginsAsync('renderer', this) } async loadResources (_fs = fs) {