From 9dce83575a8e798ee29fa8ddedacfdfbbb3e7ac6 Mon Sep 17 00:00:00 2001 From: Franck Date: Sun, 2 Feb 2020 11:36:22 +0100 Subject: [PATCH 1/7] feat: Improve rendering process with page parallel rendering --- packages/@vuepress/core/lib/node/build/index.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/@vuepress/core/lib/node/build/index.js b/packages/@vuepress/core/lib/node/build/index.js index 912eca8c23..c31a651bb3 100644 --- a/packages/@vuepress/core/lib/node/build/index.js +++ b/packages/@vuepress/core/lib/node/build/index.js @@ -89,11 +89,13 @@ module.exports = class Build extends EventEmitter { // render pages logger.wait('Rendering static HTML...') - const pagePaths = [] + const pagePathsPromises = [] for (const page of this.context.pages) { - pagePaths.push(await this.renderPage(page)) + pagePathsPromises.push(this.renderPage(page)) } + const pagePaths = await Promise.all(pagePathsPromises) + readline.clearLine(process.stdout, 0) readline.cursorTo(process.stdout, 0) @@ -134,9 +136,6 @@ module.exports = class Build extends EventEmitter { async renderPage (page) { const pagePath = decodeURIComponent(page.path) - readline.clearLine(process.stdout, 0) - readline.cursorTo(process.stdout, 0) - process.stdout.write(`Rendering page: ${pagePath}`) // #565 Avoid duplicate description meta at SSR. const meta = (page.frontmatter && page.frontmatter.meta || []).filter(item => item.name !== 'description') From cbe1af0b674d209beedb91c7a4692effc3118d5a Mon Sep 17 00:00:00 2001 From: Franck Date: Sun, 9 Feb 2020 10:38:27 +0100 Subject: [PATCH 2/7] feat: Run extendPageData functions in parallel --- packages/@vuepress/core/lib/node/Page.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/@vuepress/core/lib/node/Page.js b/packages/@vuepress/core/lib/node/Page.js index 99cb6d4eaa..da68957790 100644 --- a/packages/@vuepress/core/lib/node/Page.js +++ b/packages/@vuepress/core/lib/node/Page.js @@ -286,13 +286,17 @@ module.exports = class Page { */ async enhance (enhancers) { + const enhancerPromises = [] + for (const { name: pluginName, value: enhancer } of enhancers) { try { - await enhancer(this) + enhancerPromises.push(enhancer(this)) } catch (error) { console.log(error) throw new Error(`[${pluginName}] execute extendPageData failed.`) } } + + return Promise.all(enhancerPromises) } } From 59b20bb6914bde3aea5bafe30aea350e4fcec024 Mon Sep 17 00:00:00 2001 From: Franck Date: Sun, 9 Feb 2020 11:54:59 +0100 Subject: [PATCH 3/7] fix: Update Page.js tests --- packages/@vuepress/core/lib/node/Page.js | 19 ++++++++++++------- .../lib/node/__tests__/prepare/Page.spec.js | 13 +++++++------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/packages/@vuepress/core/lib/node/Page.js b/packages/@vuepress/core/lib/node/Page.js index da68957790..210d0e943f 100644 --- a/packages/@vuepress/core/lib/node/Page.js +++ b/packages/@vuepress/core/lib/node/Page.js @@ -289,14 +289,19 @@ module.exports = class Page { const enhancerPromises = [] for (const { name: pluginName, value: enhancer } of enhancers) { - try { - enhancerPromises.push(enhancer(this)) - } catch (error) { - console.log(error) - throw new Error(`[${pluginName}] execute extendPageData failed.`) - } + const enhancerResult = enhancer(this) + const promise = enhancerResult instanceof Promise ? enhancerResult : Promise.resolve(enhancerResult) + + enhancerPromises.push(promise.catch(e => { + console.log(e) + return new Error(`[${pluginName}] execute extendPageData failed.`) + })) } - return Promise.all(enhancerPromises) + const results = await Promise.all(enhancerPromises) + const enhancerError = results.find(result => result instanceof Error) + if (enhancerError) throw enhancerError + + return results } } diff --git a/packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js b/packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js index 7843b14f2b..e176f6dd5f 100644 --- a/packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js +++ b/packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js @@ -111,11 +111,11 @@ describe('Page', () => { page = new Page({ path: '/' }, app) enhancers = [ { - pluginName: 'foo', + name: 'foo', value: jest.fn() }, { - pluginName: 'foo', + name: 'bar', value: jest.fn() } ] @@ -130,7 +130,7 @@ describe('Page', () => { test('should loop over sync and async enhancers', async () => { const mixedEnhancers = [...enhancers, { - pluginName: 'blog', + name: 'blog', value: jest.fn().mockResolvedValue({}) }] await page.enhance(mixedEnhancers) @@ -138,13 +138,14 @@ describe('Page', () => { return mixedEnhancers.map(enhancer => expect(enhancer.value).toHaveBeenCalled()) }) - test('should log when enhancing when failing', async () => { + test('should log and throw an error when enhancing fails', async () => { const error = { errorMessage: 'this is an error message' } + const pluginName = 'error-plugin' await expect(page.enhance([{ - pluginName: 'error-plugin', + name: pluginName, value: jest.fn().mockRejectedValue(error) - }])).rejects.toThrow() + }])).rejects.toThrowError(`[${pluginName}] execute extendPageData failed.`) expect(console.log).toHaveBeenCalledWith(error) }) From 8522e27d3992951e479ad1eeb8ea031686eaf989 Mon Sep 17 00:00:00 2001 From: Franck Date: Fri, 28 Feb 2020 10:39:19 +0100 Subject: [PATCH 4/7] :recycle: refactoring enhance method --- packages/@vuepress/core/lib/node/Page.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/@vuepress/core/lib/node/Page.js b/packages/@vuepress/core/lib/node/Page.js index 210d0e943f..1bda87bf2c 100644 --- a/packages/@vuepress/core/lib/node/Page.js +++ b/packages/@vuepress/core/lib/node/Page.js @@ -286,17 +286,15 @@ module.exports = class Page { */ async enhance (enhancers) { - const enhancerPromises = [] - - for (const { name: pluginName, value: enhancer } of enhancers) { + const enhancerPromises = enhancers.map(({ value: enhancer, name: pluginName }) => { const enhancerResult = enhancer(this) const promise = enhancerResult instanceof Promise ? enhancerResult : Promise.resolve(enhancerResult) - enhancerPromises.push(promise.catch(e => { + promise.catch(e => { console.log(e) return new Error(`[${pluginName}] execute extendPageData failed.`) - })) - } + }) + }) const results = await Promise.all(enhancerPromises) const enhancerError = results.find(result => result instanceof Error) From 169cc05d9e1b4cb8a7b7e90bf95f800f7c530865 Mon Sep 17 00:00:00 2001 From: Franck Date: Fri, 28 Feb 2020 10:59:08 +0100 Subject: [PATCH 5/7] fix: fix tests --- packages/@vuepress/core/lib/node/Page.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@vuepress/core/lib/node/Page.js b/packages/@vuepress/core/lib/node/Page.js index 1bda87bf2c..54da049b68 100644 --- a/packages/@vuepress/core/lib/node/Page.js +++ b/packages/@vuepress/core/lib/node/Page.js @@ -290,7 +290,7 @@ module.exports = class Page { const enhancerResult = enhancer(this) const promise = enhancerResult instanceof Promise ? enhancerResult : Promise.resolve(enhancerResult) - promise.catch(e => { + return promise.catch(e => { console.log(e) return new Error(`[${pluginName}] execute extendPageData failed.`) }) From 9d02c1c28c33e9b8d5e3b0e4e0341b0f881c87bc Mon Sep 17 00:00:00 2001 From: Franck Abgrall Date: Tue, 10 Mar 2020 15:20:39 +0100 Subject: [PATCH 6/7] Update packages/@vuepress/core/lib/node/Page.js Co-Authored-By: meteorlxy --- packages/@vuepress/core/lib/node/Page.js | 27 +++++++++++------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/@vuepress/core/lib/node/Page.js b/packages/@vuepress/core/lib/node/Page.js index 54da049b68..07f50b4b5c 100644 --- a/packages/@vuepress/core/lib/node/Page.js +++ b/packages/@vuepress/core/lib/node/Page.js @@ -286,20 +286,17 @@ module.exports = class Page { */ async enhance (enhancers) { - const enhancerPromises = enhancers.map(({ value: enhancer, name: pluginName }) => { - const enhancerResult = enhancer(this) - const promise = enhancerResult instanceof Promise ? enhancerResult : Promise.resolve(enhancerResult) - - return promise.catch(e => { - console.log(e) - return new Error(`[${pluginName}] execute extendPageData failed.`) - }) - }) - - const results = await Promise.all(enhancerPromises) - const enhancerError = results.find(result => result instanceof Error) - if (enhancerError) throw enhancerError - - return results + return Promise.all( + enhancers.map( + async ({ value: enhancer, name: pluginName }) => { + try { + await enhancer(this) + } catch (error) { + console.log(error) + throw new Error(`[${pluginName}] execute extendPageData failed.`) + } + } + ) + ) } } From fec099f43d1377bc27b72de3fb28d8be252bb84c Mon Sep 17 00:00:00 2001 From: Franck Date: Tue, 10 Mar 2020 15:25:26 +0100 Subject: [PATCH 7/7] refactor: Code review update --- packages/@vuepress/core/lib/node/build/index.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/@vuepress/core/lib/node/build/index.js b/packages/@vuepress/core/lib/node/build/index.js index c31a651bb3..271af8843e 100644 --- a/packages/@vuepress/core/lib/node/build/index.js +++ b/packages/@vuepress/core/lib/node/build/index.js @@ -89,12 +89,9 @@ module.exports = class Build extends EventEmitter { // render pages logger.wait('Rendering static HTML...') - const pagePathsPromises = [] - for (const page of this.context.pages) { - pagePathsPromises.push(this.renderPage(page)) - } - - const pagePaths = await Promise.all(pagePathsPromises) + const pagePaths = await Promise.all( + this.context.pages.map(page => this.renderPage(page)) + ) readline.clearLine(process.stdout, 0) readline.cursorTo(process.stdout, 0)