mirror of
https://github.com/pengzhanbo/vuepress-theme-plume.git
synced 2026-04-23 10:58:13 +08:00
perf: optimize memory usage during build time (#850)
This commit is contained in:
parent
8daddcac5d
commit
78a2859398
@ -22,9 +22,9 @@ const indexByLocales = new Map<string, MiniSearch<IndexObject>>()
|
||||
const indexCache = new Map<string, IndexObject[]>()
|
||||
|
||||
function getIndexByLocale(locale: string, lang: string, options: SearchIndexOptions['searchOptions']) {
|
||||
const segmenter = new Intl.Segmenter(lang, { granularity: 'word' })
|
||||
let index = indexByLocales.get(locale)
|
||||
if (!index) {
|
||||
const segmenter = new Intl.Segmenter(lang, { granularity: 'word' })
|
||||
index = new MiniSearch<IndexObject>({
|
||||
fields: ['title', 'titles', 'text'],
|
||||
storeFields: ['title', 'titles'],
|
||||
@ -62,6 +62,11 @@ export async function prepareSearchIndex({
|
||||
})
|
||||
await writeTemp(app)
|
||||
|
||||
if (app.env.isBuild) {
|
||||
indexByLocales.clear()
|
||||
indexCache.clear()
|
||||
}
|
||||
|
||||
if (app.env.isDebug) {
|
||||
logger.info(
|
||||
`\n[${colors.green('@vuepress-plume/plugin-search')}] prepare search time spent: ${(performance.now() - start).toFixed(2)}ms`,
|
||||
|
||||
@ -265,7 +265,7 @@ watch(
|
||||
padding: 48px 32px 0;
|
||||
}
|
||||
|
||||
.vp-doc-container:not(.has-sidebar) .container, {
|
||||
.vp-doc-container:not(.has-sidebar) .container {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
max-width: 992px;
|
||||
|
||||
@ -106,7 +106,7 @@ function resolveFromSidebarItems(sidebarItems: NavItemWithLink[], currentPath: s
|
||||
if (index === -1)
|
||||
return null
|
||||
// eslint-disable-next-line no-cond-assign
|
||||
while ((index += offset) >= 0) {
|
||||
while ((index += offset) >= 0 && index < sidebarItems.length) {
|
||||
const targetItem = sidebarItems[index]
|
||||
if (targetItem?.link && !SEPARATOR_RE.test(targetItem.link)) {
|
||||
return targetItem
|
||||
|
||||
@ -26,7 +26,7 @@ async function getMarkdownInfo(relativePath: string, cwd: string): Promise<{
|
||||
const raw = await fs.promises.readFile(filepath, 'utf-8')
|
||||
const { data, content } = matter(raw)
|
||||
return {
|
||||
data,
|
||||
data: data as AutoFrontmatterData,
|
||||
context: {
|
||||
filepath,
|
||||
relativePath,
|
||||
|
||||
@ -30,7 +30,7 @@ export const PRESET: TagsColorsItem[] = [
|
||||
]
|
||||
|
||||
// { index: className }
|
||||
const cache: Record<number, string> = {}
|
||||
let cache: Record<number, string> = {}
|
||||
|
||||
export async function prepareArticleTagColors(app: App): Promise<void> {
|
||||
perf.mark('prepare:tag-colors')
|
||||
@ -39,6 +39,10 @@ export async function prepareArticleTagColors(app: App): Promise<void> {
|
||||
await writeTemp(app, 'internal/articleTagColors.css', css)
|
||||
await writeTemp(app, 'internal/articleTagColors.js', js)
|
||||
|
||||
if (app.env.isBuild) {
|
||||
cache = {}
|
||||
}
|
||||
|
||||
perf.log('prepare:tag-colors')
|
||||
}
|
||||
|
||||
|
||||
@ -9,7 +9,7 @@ export async function prepareCollections(app: App): Promise<void> {
|
||||
|
||||
const { collections: fallback, locales } = getThemeConfig()
|
||||
|
||||
const data: Record<string, ThemeCollectionItem[]> = {}
|
||||
let data: Record<string, ThemeCollectionItem[]> = {}
|
||||
|
||||
for (const [locale, opt] of Object.entries(locales || {})) {
|
||||
let collections = opt.collections
|
||||
@ -32,5 +32,9 @@ export async function prepareCollections(app: App): Promise<void> {
|
||||
const content = resolveContent(app, { name: 'collections', content: data })
|
||||
await writeTemp(app, 'internal/collectionsData.js', content)
|
||||
|
||||
if (app.env.isBuild) {
|
||||
data = {}
|
||||
}
|
||||
|
||||
perf.log('prepare:collections')
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ export async function prepareEncrypt(app: App): Promise<void> {
|
||||
content: resolvedEncrypt,
|
||||
}))
|
||||
|
||||
fsCache?.write([currentHash, resolvedEncrypt])
|
||||
fsCache?.write([currentHash, resolvedEncrypt], app.env.isBuild)
|
||||
|
||||
perf.log('prepare:encrypt')
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ let locate!: ((name: string) => any)
|
||||
|
||||
let fsCache: FsCache<IconDataMap> | null = null
|
||||
// { iconName: { className, content } }
|
||||
const cache: IconDataMap = {}
|
||||
let cache: IconDataMap = {}
|
||||
|
||||
// 旧版本内置图标别名,映射回 simple-icons 集合中的名称
|
||||
const socialFallbacks: Record<string, string> = {
|
||||
@ -135,7 +135,11 @@ export async function prepareIcons(app: App): Promise<void> {
|
||||
})),
|
||||
])
|
||||
|
||||
fsCache?.write(cache)
|
||||
fsCache?.write(cache, app.env.isBuild)
|
||||
|
||||
if (app.env.isBuild) {
|
||||
cache = {}
|
||||
}
|
||||
|
||||
perf.log('prepare:icons:total')
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@ export interface FsCache<T> {
|
||||
hash: string
|
||||
data: T | null
|
||||
read: () => Promise<T | null>
|
||||
write: (data: T) => Promise<void>
|
||||
write: (data: T, clear?: boolean) => Promise<void>
|
||||
}
|
||||
|
||||
const CACHE_BASE = 'markdown'
|
||||
@ -37,7 +37,7 @@ export function createFsCache<T = any>(app: App, name: string): FsCache<T> {
|
||||
}
|
||||
|
||||
let timer: NodeJS.Timeout | null = null
|
||||
const write = async (data: T) => {
|
||||
const write = async (data: T, clear?: boolean) => {
|
||||
const currentHash = hash(data)
|
||||
if (cache.hash && currentHash === cache.hash)
|
||||
return
|
||||
@ -49,6 +49,10 @@ export function createFsCache<T = any>(app: App, name: string): FsCache<T> {
|
||||
timer = setTimeout(async () => {
|
||||
await fs.mkdir(path.dirname(filepath), { recursive: true })
|
||||
await fs.writeFile(filepath, JSON.stringify(cache), 'utf-8')
|
||||
if (clear) {
|
||||
cache.data = null
|
||||
cache.hash = ''
|
||||
}
|
||||
}, 300)
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user