Skip to content

Commit 406fa67

Browse files
committed
chore: improve logging
1 parent 99aa8c2 commit 406fa67

File tree

7 files changed

+66
-33
lines changed

7 files changed

+66
-33
lines changed

packages/bundler-vite/src/build/build.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import type { CreateVueAppFunction } from '@vuepress/client'
22
import type { App, Bundler } from '@vuepress/core'
3-
import { chalk, fs, importFile, ora, withSpinner } from '@vuepress/utils'
3+
import { chalk, debug, fs, importFile, withSpinner } from '@vuepress/utils'
44
import type { OutputAsset, OutputChunk, RollupOutput } from 'rollup'
55
import { build as viteBuild } from 'vite'
66
import { resolveViteConfig } from '../resolveViteConfig.js'
77
import type { ViteBundlerOptions } from '../types.js'
88
import { renderPage } from './renderPage.js'
99

10+
const log = debug('vuepress:bundler-vite/build')
11+
1012
export const build = async (
1113
options: ViteBundlerOptions,
1214
app: App
@@ -15,6 +17,7 @@ export const build = async (
1517
await app.pluginApi.hooks.extendsBundlerOptions.process(options, app)
1618

1719
// vite compile
20+
log('compiling start')
1821
let clientOutput!: RollupOutput
1922
let serverOutput!: RollupOutput
2023
await withSpinner('Compiling with vite')(async () => {
@@ -37,9 +40,10 @@ export const build = async (
3740
viteBuild(serverConfig) as Promise<RollupOutput>,
3841
])
3942
})
43+
log('compiling finish')
4044

4145
// render pages
42-
await withSpinner('Rendering pages')(async () => {
46+
await withSpinner(`Rendering ${app.pages.length} pages`)(async (spinner) => {
4347
// load ssr template file
4448
const ssrTemplate = (
4549
await fs.readFile(app.options.templateBuild)
@@ -66,9 +70,10 @@ export const build = async (
6670
}>(serverEntryPath)
6771

6872
// pre-render pages to html files
69-
const spinner = ora()
7073
for (const page of app.pages) {
71-
spinner.start(`Rendering pages ${chalk.magenta(page.path)}`)
74+
if (spinner) {
75+
spinner.text = `Rendering pages ${chalk.magenta(page.path)}`
76+
}
7277
await renderPage({
7378
app,
7479
page,
@@ -79,7 +84,6 @@ export const build = async (
7984
outputCssAsset: clientCssAsset,
8085
})
8186
}
82-
spinner.stop()
8387
})
8488

8589
// keep the server bundle files in debug mode

packages/bundler-webpack/src/build/build.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import type { CreateVueAppFunction } from '@vuepress/client'
22
import type { App, Bundler } from '@vuepress/core'
3-
import { chalk, fs, importFileDefault, ora, withSpinner } from '@vuepress/utils'
3+
import {
4+
chalk,
5+
debug,
6+
fs,
7+
importFileDefault,
8+
withSpinner,
9+
} from '@vuepress/utils'
410
import webpack from 'webpack'
511
import { resolveWebpackConfig } from '../resolveWebpackConfig.js'
612
import type { WebpackBundlerOptions } from '../types.js'
@@ -13,6 +19,8 @@ import { renderPage } from './renderPage.js'
1319
import { resolveClientManifestMeta } from './resolveClientManifestMeta.js'
1420
import type { ClientManifest } from './ssr/index.js'
1521

22+
const log = debug('vuepress:bundler-webpack/build')
23+
1624
export const build = async (
1725
options: WebpackBundlerOptions,
1826
app: App
@@ -21,6 +29,7 @@ export const build = async (
2129
await app.pluginApi.hooks.extendsBundlerOptions.process(options, app)
2230

2331
// webpack compile
32+
log('compiling start')
2433
await withSpinner('Compiling with webpack')(async () => {
2534
// create webpack config
2635
const clientConfig = resolveWebpackConfig({
@@ -56,9 +65,10 @@ export const build = async (
5665
})
5766
})
5867
})
68+
log('compiling finish')
5969

6070
// render pages
61-
await withSpinner('Rendering pages')(async () => {
71+
await withSpinner(`Rendering ${app.pages.length} pages`)(async (spinner) => {
6272
// load ssr template file
6373
const ssrTemplate = (
6474
await fs.readFile(app.options.templateBuild)
@@ -83,9 +93,10 @@ export const build = async (
8393
}>(serverEntryPath)
8494

8595
// pre-render pages to html files
86-
const spinner = ora()
8796
for (const page of app.pages) {
88-
spinner.start(`Rendering pages ${chalk.magenta(page.path)}`)
97+
if (spinner) {
98+
spinner.text = `Rendering pages ${chalk.magenta(page.path)}`
99+
}
89100
await renderPage({
90101
app,
91102
page,
@@ -97,7 +108,6 @@ export const build = async (
97108
moduleFilesMetaMap,
98109
})
99110
}
100-
spinner.stop()
101111
})
102112

103113
// keep the server bundle files in debug mode

packages/cli/src/commands/build/createBuild.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import process from 'node:process'
22
import { createBuildApp } from '@vuepress/core'
33
import type { AppConfig } from '@vuepress/core'
4-
import { debug, fs, logger } from '@vuepress/utils'
4+
import { debug, formatMs, fs, logger, withSpinner } from '@vuepress/utils'
55
import {
66
loadUserConfig,
77
resolveAppConfig,
@@ -17,6 +17,8 @@ const log = debug('vuepress:cli/build')
1717
export const createBuild =
1818
(defaultAppConfig: Partial<AppConfig>): BuildCommand =>
1919
async (sourceDir = '.', commandOptions = {}): Promise<void> => {
20+
const start = Date.now()
21+
2022
log(`commandOptions:`, commandOptions)
2123

2224
if (process.env.NODE_ENV === undefined) {
@@ -51,28 +53,32 @@ export const createBuild =
5153

5254
// clean temp and cache
5355
if (commandOptions.cleanTemp === true) {
54-
logger.info('Cleaning temp...')
55-
await fs.remove(app.dir.temp())
56+
await withSpinner('Cleaning temp')(() => {
57+
return fs.remove(app.dir.temp())
58+
})
5659
}
5760
if (commandOptions.cleanCache === true) {
58-
logger.info('Cleaning cache...')
59-
await fs.remove(app.dir.cache())
61+
await withSpinner('Cleaning cache')(() => {
62+
return fs.remove(app.dir.cache())
63+
})
6064
}
6165

6266
// empty dest directory
6367
await fs.emptyDir(app.dir.dest())
6468

6569
// initialize and prepare
66-
logger.info('Initializing VuePress and preparing data...')
67-
68-
await app.init()
69-
await app.prepare()
70+
await withSpinner('Initializing and preparing data')(async () => {
71+
await app.init()
72+
await app.prepare()
73+
})
7074

7175
// build
7276
await app.build()
7377

7478
// plugin hook: onGenerated
7579
await app.pluginApi.hooks.onGenerated.process(app)
7680

77-
logger.success('VuePress build successfully!')
81+
logger.success(
82+
`VuePress build completed in ${formatMs(Date.now() - start)}!`
83+
)
7884
}

packages/cli/src/commands/dev/createDev.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import process from 'node:process'
22
import { createDevApp } from '@vuepress/core'
33
import type { AppConfig } from '@vuepress/core'
4-
import { debug, fs, logger } from '@vuepress/utils'
4+
import { debug, fs, logger, withSpinner } from '@vuepress/utils'
55
import type { FSWatcher } from 'chokidar'
66
import {
77
loadUserConfig,
@@ -58,18 +58,21 @@ export const createDev = (defaultAppConfig: Partial<AppConfig>): DevCommand => {
5858

5959
// clean temp and cache
6060
if (commandOptions.cleanTemp === true) {
61-
logger.info('Cleaning temp...')
62-
await fs.remove(app.dir.temp())
61+
await withSpinner('Cleaning temp')(() => {
62+
return fs.remove(app.dir.temp())
63+
})
6364
}
6465
if (commandOptions.cleanCache === true) {
65-
logger.info('Cleaning cache...')
66-
await fs.remove(app.dir.cache())
66+
await withSpinner('Cleaning cache')(() => {
67+
return fs.remove(app.dir.cache())
68+
})
6769
}
6870

6971
// initialize and prepare
70-
logger.info('Initializing VuePress and preparing data...')
71-
await app.init()
72-
await app.prepare()
72+
await withSpinner('Initializing and preparing data')(async () => {
73+
await app.init()
74+
await app.prepare()
75+
})
7376

7477
// start dev server
7578
const close = await app.dev()

packages/utils/src/formatMs.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Format millisecond
3+
*/
4+
export const formatMs = (ms: number): string => {
5+
if (ms < 1000) return `${ms}ms`
6+
return `${(ms / 1000).toFixed(2)}s`
7+
}

packages/utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export { debug, chalk, fs, globby, hash, ora, path }
1010

1111
export * from './getDirname.js'
1212
export * from './importFile.js'
13+
export * from './formatMs.js'
1314
export * from './isChildPath.js'
1415
export * from './logger.js'
1516
export * from './renderHead.js'

packages/utils/src/withSpinner.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import process from 'node:process'
2-
import ora from 'ora'
2+
import ora, { type Ora } from 'ora'
3+
import { formatMs } from './formatMs.js'
34

45
export const withSpinner =
56
(msg: string) =>
6-
async <T>(target: () => Promise<T>): Promise<T> => {
7+
async <T>(target: (spinner?: Ora) => Promise<T>): Promise<T> => {
78
if (process.env.DEBUG) {
89
return target()
910
}
1011

12+
const start = Date.now()
1113
const spinner = ora()
1214
try {
1315
spinner.start(msg)
14-
const result = await target()
15-
spinner.succeed(`${msg} - done`)
16+
const result = await target(spinner)
17+
spinner.succeed(`${msg} - done in ${formatMs(Date.now() - start)}`)
1618
return result
1719
} catch (e) {
18-
spinner.fail(`${msg} - failed`)
20+
spinner.fail(`${msg} - failed in ${formatMs(Date.now() - start)}`)
1921
throw e
2022
}
2123
}

0 commit comments

Comments
 (0)