Skip to content

Commit 68add19

Browse files
committed
fix($core): changing frontmatter always took a long time to refresh page
This commit also convert the AppContext into a singleton.
1 parent 9070496 commit 68add19

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

packages/@vuepress/core/lib/prepare/AppContext.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ const PluginAPI = require('../plugin-api/index')
2323
*/
2424

2525
module.exports = class AppContext {
26+
static getInstance (...args) {
27+
if (!AppContext._instance) {
28+
AppContext._instance = new AppContext(...args)
29+
}
30+
return AppContext._instance
31+
}
32+
2633
/**
2734
* Instantiate the app context with a new API
2835
*
@@ -45,6 +52,16 @@ module.exports = class AppContext {
4552
this.writeTemp = writeTemp
4653

4754
this.vuepressDir = path.resolve(sourceDir, '.vuepress')
55+
}
56+
57+
/**
58+
* Resolve user config and initialize.
59+
*
60+
* @returns {void}
61+
* @api private
62+
*/
63+
64+
resolveConfigAndInitialize () {
4865
this.siteConfig = loadConfig(this.vuepressDir)
4966
if (isFunction(this.siteConfig)) {
5067
this.siteConfig = this.siteConfig(this)
@@ -56,13 +73,12 @@ module.exports = class AppContext {
5673
this.base = this.siteConfig.base || '/'
5774
this.themeConfig = this.siteConfig.themeConfig || {}
5875

59-
const rawOutDir = cliOptions.dest || this.siteConfig.dest
76+
const rawOutDir = this.cliOptions.dest || this.siteConfig.dest
6077
this.outDir = rawOutDir
6178
? require('path').resolve(this.cwd, rawOutDir)
62-
: require('path').resolve(sourceDir, '.vuepress/dist')
63-
64-
this.pluginAPI = new PluginAPI(this)
79+
: require('path').resolve(this.sourceDir, '.vuepress/dist')
6580
this.pages = [] // Array<Page>
81+
this.pluginAPI = new PluginAPI(this)
6682
this.ClientComputedMixinConstructor = ClientComputedMixin(this.getSiteData())
6783
}
6884

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

7692
async process () {
93+
this.resolveConfigAndInitialize()
7794
this.resolveCacheLoaderOptions()
7895
this.normalizeHeadTagUrls()
7996
await this.resolveTheme()

packages/@vuepress/core/lib/prepare/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const AppContext = require('./AppContext')
1111
*/
1212

1313
module.exports = async function prepare (sourceDir, cliOptions, isProd) {
14-
const appContext = new AppContext(sourceDir, cliOptions, isProd)
14+
const appContext = AppContext.getInstance(sourceDir, cliOptions, isProd)
1515
await appContext.process()
1616
return appContext
1717
}

0 commit comments

Comments
 (0)