Skip to content

Commit

Permalink
fix($core): changing frontmatter always took a long time to refresh page
Browse files Browse the repository at this point in the history
This commit also convert the AppContext into a singleton.
  • Loading branch information
ulivz committed Dec 2, 2018
1 parent 9070496 commit 68add19
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
25 changes: 21 additions & 4 deletions packages/@vuepress/core/lib/prepare/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const PluginAPI = require('../plugin-api/index')
*/

module.exports = class AppContext {
static getInstance (...args) {
if (!AppContext._instance) {
AppContext._instance = new AppContext(...args)
}
return AppContext._instance
}

/**
* Instantiate the app context with a new API
*
Expand All @@ -45,6 +52,16 @@ module.exports = class AppContext {
this.writeTemp = writeTemp

this.vuepressDir = path.resolve(sourceDir, '.vuepress')
}

/**
* Resolve user config and initialize.
*
* @returns {void}
* @api private
*/

resolveConfigAndInitialize () {
this.siteConfig = loadConfig(this.vuepressDir)
if (isFunction(this.siteConfig)) {
this.siteConfig = this.siteConfig(this)
Expand All @@ -56,13 +73,12 @@ module.exports = class AppContext {
this.base = this.siteConfig.base || '/'
this.themeConfig = this.siteConfig.themeConfig || {}

const rawOutDir = cliOptions.dest || this.siteConfig.dest
const rawOutDir = this.cliOptions.dest || this.siteConfig.dest
this.outDir = rawOutDir
? require('path').resolve(this.cwd, rawOutDir)
: require('path').resolve(sourceDir, '.vuepress/dist')

this.pluginAPI = new PluginAPI(this)
: require('path').resolve(this.sourceDir, '.vuepress/dist')
this.pages = [] // Array<Page>
this.pluginAPI = new PluginAPI(this)
this.ClientComputedMixinConstructor = ClientComputedMixin(this.getSiteData())
}

Expand All @@ -74,6 +90,7 @@ module.exports = class AppContext {
*/

async process () {
this.resolveConfigAndInitialize()
this.resolveCacheLoaderOptions()
this.normalizeHeadTagUrls()
await this.resolveTheme()
Expand Down
2 changes: 1 addition & 1 deletion packages/@vuepress/core/lib/prepare/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const AppContext = require('./AppContext')
*/

module.exports = async function prepare (sourceDir, cliOptions, isProd) {
const appContext = new AppContext(sourceDir, cliOptions, isProd)
const appContext = AppContext.getInstance(sourceDir, cliOptions, isProd)
await appContext.process()
return appContext
}

0 comments on commit 68add19

Please sign in to comment.