refactor: remove deprecated enhancement

This commit is contained in:
pengzhanbo 2026-04-02 21:12:59 +08:00
parent 184d1aee76
commit a6cb3820b1
2 changed files with 5 additions and 80 deletions

View File

@ -1,75 +0,0 @@
/**
* Temporary enhancement for VuePress app.
* This enhancement will be removed in the next version of vuepress/core.
*
* VuePress
* vuepress/core
*/
import type { App } from 'vuepress'
import { fs, hash } from 'vuepress/utils'
/**
* Cache structure for writeTemp operations.
* Tracks content hash and writing promises for optimization.
*
* writeTemp
*
*/
interface WriteTempCache {
/** Content hash for change detection / 用于变更检测的内容哈希 */
hash?: string
/** Current writing promise / 当前写入承诺 */
current?: Promise<void>
/** Next writing promise to chain / 要链接的下一个写入承诺 */
next?: () => Promise<void>
}
/**
* Enhance the VuePress app with optimized writeTemp method.
* Implements caching and promise chaining for better performance.
*
* 使 writeTemp VuePress
*
*
* @param app - VuePress application instance / VuePress
*/
export function enhanceApp(app: App): void {
// rewrite writeTemp to cache the writing promise
const cache = new Map<string, WriteTempCache>()
app.writeTemp = async function (file: string, content: string): Promise<string> {
const filePath = app.dir.temp(file)
const contentHash = hash(content)
let item = cache.get(filePath)
if (!item) {
cache.set(filePath, (item = {}))
}
// if content hash is the same as the last one, skip writing
if (item.hash === contentHash) {
return filePath
}
item.hash = contentHash
if (!item.current) {
item.current = (async () => {
await fs.outputFile(filePath, content)
// if there is a next writing promise, chain it with the current one
item.current = item.next?.()
return item.current
})()
}
else {
// if there is a current writing promise, save the next writing promise
item.next = async () => {
await fs.outputFile(filePath, content)
item.next = undefined
item.current = undefined
}
}
await item.current
return filePath
}
}

View File

@ -12,11 +12,10 @@ import {
templateBuildRenderer, templateBuildRenderer,
} from './config/index.js' } from './config/index.js'
import { detectThemeOptions, detectVersions } from './detector/index.js' import { detectThemeOptions, detectVersions } from './detector/index.js'
import { enhanceApp } from './enhance.js'
import { configLoader } from './loadConfig/index.js' import { configLoader } from './loadConfig/index.js'
import { createPages, extendsPageData } from './pages/index.js' import { createPages, extendsPageData } from './pages/index.js'
import { setupPlugins } from './plugins/index.js' import { setupPlugins } from './plugins/index.js'
import { prepareData, watchPrepare } from './prepare/index.js' import { prepareData } from './prepare/index.js'
import { prepareThemeData } from './prepare/prepareThemeData.js' import { prepareThemeData } from './prepare/prepareThemeData.js'
import { perf, resolve, setTranslateLang, templates, THEME_NAME } from './utils/index.js' import { perf, resolve, setTranslateLang, templates, THEME_NAME } from './utils/index.js'
@ -40,8 +39,6 @@ import { perf, resolve, setTranslateLang, templates, THEME_NAME } from './utils/
*/ */
export function plumeTheme(options: ThemeOptions = {}): Theme { export function plumeTheme(options: ThemeOptions = {}): Theme {
return (app) => { return (app) => {
enhanceApp(app)
setTranslateLang(app.options.lang) setTranslateLang(app.options.lang)
perf.init(app.env.isDebug) perf.init(app.env.isDebug)
@ -87,9 +84,12 @@ export function plumeTheme(options: ThemeOptions = {}): Theme {
await prepareData(app) await prepareData(app)
}, },
onPageUpdated: async (app) => {
await prepareData(app)
},
onWatched: async (app, watchers) => { onWatched: async (app, watchers) => {
configLoader.watch(watchers as any) configLoader.watch(watchers as any)
watchPrepare(app, watchers)
watchAutoFrontmatter(app, watchers as any) watchAutoFrontmatter(app, watchers as any)
}, },
} }