Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat($core): Improve VuePress build time #2163

Merged
merged 7 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions packages/@vuepress/core/lib/node/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,20 @@ module.exports = class Page {
*/

async enhance (enhancers) {
for (const { name: pluginName, value: enhancer } of enhancers) {
try {
await enhancer(this)
} catch (error) {
console.log(error)
throw new Error(`[${pluginName}] execute extendPageData failed.`)
}
}
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
kefranabg marked this conversation as resolved.
Show resolved Hide resolved
}
}
13 changes: 7 additions & 6 deletions packages/@vuepress/core/lib/node/__tests__/prepare/Page.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
]
Expand All @@ -130,21 +130,22 @@ 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)

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)
})
Expand Down
9 changes: 4 additions & 5 deletions packages/@vuepress/core/lib/node/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From 91 to 97:

Suggested change
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)

Expand Down Expand Up @@ -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')
Expand Down