Skip to content

Commit

Permalink
strip changes from vuejs#3385
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyx1998 committed Jan 8, 2024
1 parent 5493202 commit 1dd35b2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 110 deletions.
2 changes: 0 additions & 2 deletions package.json
Expand Up @@ -138,7 +138,6 @@
"@types/debug": "^4.1.12",
"@types/escape-html": "^1.0.4",
"@types/fs-extra": "^11.0.4",
"@types/humanize-duration": "^3.27.3",
"@types/jsdom": "^21.1.6",
"@types/lodash.template": "^4.5.3",
"@types/mark.js": "^8.11.12",
Expand All @@ -164,7 +163,6 @@
"fs-extra": "^11.2.0",
"get-port": "^7.0.0",
"gray-matter": "^4.0.3",
"humanize-duration": "^3.31.0",
"lint-staged": "^15.2.0",
"lodash.template": "^4.5.0",
"lru-cache": "^10.1.0",
Expand Down
14 changes: 0 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 7 additions & 12 deletions src/node/build/build.ts
Expand Up @@ -74,16 +74,16 @@ export async function build(
}

try {
const { clientResult, serverResult, pageToHashMap } = await task(
'building client + server bundles',
() => bundle(siteConfig, buildOptions)
const { clientResult, serverResult, pageToHashMap } = await bundle(
siteConfig,
buildOptions
)

if (process.env.BUNDLE_ONLY) {
return
}

await task('rendering pages', async (updateProgress) => {
await task('rendering pages', async () => {
const renderEntry =
pathToFileURL(path.join(siteConfig.tempDir, 'app.js')).toString() +
'?t=' +
Expand Down Expand Up @@ -165,14 +165,9 @@ export async function build(
}

const pages = ['404.md', ...siteConfig.pages]
let count_done = 0
await pMap(
pages,
(page) => task(page).then(updateProgress(++count_done, pages.length)),
{
concurrency: siteConfig.concurrency
}
)
await pMap(pages, task, {
concurrency: siteConfig.concurrency
})
})

// emit page hash map for the case where a user session is open
Expand Down
11 changes: 4 additions & 7 deletions src/node/plugins/localSearchPlugin.ts
Expand Up @@ -17,7 +17,6 @@ import {
type Awaitable
} from '../shared'
import { processIncludes } from '../utils/processIncludes'
import { updateCurrentTask, clearLine } from '../utils/task'
import type { PageSplitSection } from '../../../types/local-search'

const debug = _debug('vitepress:local-search')
Expand Down Expand Up @@ -176,12 +175,10 @@ export async function localSearchPlugin(
}

async function indexLocale(locale: string) {
let numIndexed = 0
const update = () =>
updateCurrentTask(++numIndexed, files.length, `🔍️ indexing ${locale}`)
const files = [...filesByLocale.get(locale)!]
const task = (f: string) => indexFile(f, parallel).then(update)
await pMap(files, task, { concurrency: siteConfig.concurrency })
await pMap(files, (f) => indexFile(f, parallel), {
concurrency: siteConfig.concurrency
})
}

const parallel = shouldUseParallel(siteConfig, 'local-search')
Expand Down Expand Up @@ -264,7 +261,7 @@ async function* splitPageIntoSections(
// Skip duplicate id, content will be treated as normal text
if (existingIdSet.has(id)) {
console.error(
`${clearLine}⚠️ Duplicate heading id "${id}" in ${pageName}`
`\x1b[2K\r ⚠️ Duplicate heading id "${id}" in ${pageName}`
)
continue
}
Expand Down
76 changes: 7 additions & 69 deletions src/node/utils/task.ts
@@ -1,80 +1,18 @@
import ora from 'ora'
import humanizeDuration from 'humanize-duration'
import c from 'picocolors'
import { workerMeta } from '../worker'

export const okMark = c.green('✓')
export const failMark = c.red('✖')
export const clearLine = '\x1b[2K\r'

export type UpdateHandle = (
done?: number,
total?: number,
subtask?: string
) => any

let updateHandle: UpdateHandle | null = null

export const updateCurrentTask: UpdateHandle = (...args) => {
if (workerMeta) workerMeta.updateCurrentTask(...args)
else if (updateHandle) updateHandle(...args)
else if (!process.stderr.isTTY) {
return
} else if (args.length === 0) {
process.stderr.write(clearLine)
} else {
const name = args[2] || 'unknown task'
process.stderr.write(
`${clearLine}${name} [${args.slice(0, 2).join(' / ')}]`
)
}
}

export async function task<T>(
taskName: string,
task: (update: UpdateHandle) => Promise<T>
): Promise<T> {
if (workerMeta) {
let retVal: T
await workerMeta.task(taskName, async (handle: UpdateHandle) => {
retVal = await task(handle)
})
return retVal!
}
export const okMark = '\x1b[32m✓\x1b[0m'
export const failMark = '\x1b[31m✖\x1b[0m'

export async function task(taskName: string, task: () => Promise<void>) {
const spinner = ora({ discardStdin: false })
spinner.start(taskName + '...')

updateHandle = (done, total, subtask) => {
const taskFullName = subtask ? `${taskName} - ${subtask}` : taskName
if (done === undefined) {
spinner.text = taskFullName + '...'
} else if (total === undefined) {
spinner.text = `${taskFullName} [ ${done} ]`
} else {
// match length to display them in same width
const _total = `${total}`
const _done = `${done}`.padStart(_total.length, ' ')
spinner.text = `${taskFullName} [ ${_done} / ${_total} ]`
}
}

const timeStart = performance.now()
let success = true

try {
return await task(updateHandle)
await task()
} catch (e) {
success = false
spinner.stopAndPersist({ symbol: failMark })
throw e
} finally {
updateHandle = null
const timeEnd = performance.now()
const duration = humanizeDuration(timeEnd - timeStart, {
maxDecimalPoints: 2
})
const text = `${taskName} - ${duration}`
const symbol = success ? okMark : failMark
spinner.stopAndPersist({ symbol, text })
}

spinner.stopAndPersist({ symbol: okMark })
}
7 changes: 1 addition & 6 deletions src/node/worker.ts
Expand Up @@ -3,7 +3,6 @@ import RPCContext, {
deferPromise,
type RPCContextOptions
} from 'rpc-magic-proxy'
import { task, updateCurrentTask } from './utils/task'
import c from 'picocolors'
import Queue from './utils/queue'
import _debug from 'debug'
Expand Down Expand Up @@ -83,9 +82,7 @@ export async function launchWorkers(numWorkers: number, context: Object) {
workerId,
dispatchWork,
// Save some RPC overhead when debugger is not active
debug: debug.enabled ? debug : null,
task,
updateCurrentTask
debug: debug.enabled ? debug : null
},
initWorkerHooks,
getNextTask,
Expand Down Expand Up @@ -137,8 +134,6 @@ export let workerMeta: {
workerId: string
dispatchWork: typeof dispatchWork
debug: typeof debug
task: typeof task
updateCurrentTask: typeof updateCurrentTask
} | null = null

const registry: Map<string, { main: Function; init?: Function }> = new Map()
Expand Down

0 comments on commit 1dd35b2

Please sign in to comment.