Skip to content

Commit 9719bc3

Browse files
committed
tweak cli output
1 parent e71e086 commit 9719bc3

File tree

6 files changed

+50
-19
lines changed

6 files changed

+50
-19
lines changed

lib/build.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
55
const path = require('path')
66
const chalk = require('chalk')
77
const webpack = require('webpack')
8+
const readline = require('readline')
89
const { promisify } = require('util')
910
const rimraf = promisify(require('rimraf'))
1011
const mkdirp = promisify(require('mkdirp'))
@@ -17,6 +18,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
1718
const { createBundleRenderer } = require('vue-server-renderer')
1819
const { normalizeHeadTag, applyUserWebpackConfig } = require('./util')
1920

21+
process.stdout.write('Extracting site metadata...')
2022
const options = await prepare(sourceDir)
2123
if (cliOptions.outDir) {
2224
options.outDir = cliOptions.outDir
@@ -28,6 +30,9 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
2830
let clientConfig = createClientConfig(options, cliOptions).toConfig()
2931
let serverConfig = createServerConfig(options, cliOptions).toConfig()
3032

33+
// disable uglify for server
34+
serverConfig.optimization = { minimizer: [] }
35+
3136
// apply user config...
3237
const userConfig = options.siteConfig.configureWebpack
3338
if (userConfig) {
@@ -53,7 +58,6 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
5358
const renderer = createBundleRenderer(serverBundle, {
5459
clientManifest,
5560
runInNewContext: false,
56-
// shouldPrefetch: () => false,
5761
inject: false,
5862
template: fs.readFileSync(path.resolve(__dirname, 'app/index.ssr.html'), 'utf-8')
5963
})
@@ -64,14 +68,21 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
6468
.join('\n ')
6569

6670
// render pages
67-
await Promise.all(options.siteData.pages.map(renderPage))
71+
console.log('Rendering static HTML...')
72+
for (const page of options.siteData.pages) {
73+
await renderPage(page)
74+
}
6875

6976
// if the user does not have a custom 404.md, generate the theme's default
7077
if (!options.siteData.pages.some(p => p.path === '/404.html')) {
7178
await renderPage({ path: '/404.html' })
7279
}
7380

81+
readline.clearLine(process.stdout, 0)
82+
readline.cursorTo(process.stdout, 0)
83+
7484
if (options.siteConfig.serviceWorker) {
85+
console.log('Generating service worker...')
7586
const wbb = require('workbox-build')
7687
wbb.generateSW({
7788
swDest: path.resolve(outDir, 'service-worker.js'),
@@ -120,8 +131,11 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
120131

121132
async function renderPage (page) {
122133
const pagePath = page.path
123-
const pageMeta = renderPageMeta(page.frontmatter && page.frontmatter.meta)
134+
readline.clearLine(process.stdout, 0)
135+
readline.cursorTo(process.stdout, 0)
136+
process.stdout.write(`Rendering page: ${pagePath}`)
124137

138+
const pageMeta = renderPageMeta(page.frontmatter && page.frontmatter.meta)
125139
const context = {
126140
url: pagePath,
127141
userHeadTags,

lib/dev.js

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
1717
const { applyUserWebpackConfig } = require('./util')
1818
const { frontmatterEmitter } = require('./webpack/markdownLoader')
1919

20+
process.stdout.write('Extracting site metadata...')
2021
const options = await prepare(sourceDir)
2122

2223
// setup watchers to update options and dynamically generated files

lib/prepare.js

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ const fs = require('fs')
22
const path = require('path')
33
const globby = require('globby')
44
const mkdirp = require('mkdirp')
5+
const { promisify } = require('util')
6+
const readFile = promisify(fs.readFile)
7+
const writeFile = promisify(fs.writeFile)
58
const yaml = require('yaml-front-matter')
69
const tempPath = path.resolve(__dirname, 'app/.temp')
710
const { inferTitle, extractHeaders } = require('./util')
811

912
mkdirp(tempPath)
1013

1114
const tempCache = new Map()
12-
function writeTemp (file, content) {
15+
async function writeTemp (file, content) {
1316
// cache write to avoid hitting the dist if it didn't change
1417
const cached = tempCache.get(file)
1518
if (cached !== content) {
16-
fs.writeFileSync(path.join(tempPath, file), content)
19+
await writeFile(path.join(tempPath, file), content)
1720
tempCache.set(file, content)
1821
}
1922
}
@@ -26,14 +29,14 @@ module.exports = async function prepare (sourceDir) {
2629
const routesCode = await genRoutesFile(options)
2730
const componentCode = await genComponentRegistrationFile(options)
2831

29-
writeTemp('routes.js', [
32+
await writeTemp('routes.js', [
3033
componentCode,
3134
routesCode
3235
].join('\n\n'))
3336

3437
// 3. generate siteData
3538
const dataCode = `export const siteData = ${JSON.stringify(options.siteData, null, 2)}`
36-
writeTemp('siteData.js', dataCode)
39+
await writeTemp('siteData.js', dataCode)
3740

3841
// 4. generate basic polyfill if need to support older browsers
3942
let polyfillCode = ``
@@ -42,13 +45,13 @@ module.exports = async function prepare (sourceDir) {
4245
`import 'es6-promise/auto'
4346
if (!Object.assign) Object.assign = require('object-assign')`
4447
}
45-
writeTemp('polyfill.js', polyfillCode)
48+
await writeTemp('polyfill.js', polyfillCode)
4649

4750
// 5. handle user override
4851
if (options.useDefaultTheme) {
4952
const overridePath = path.resolve(sourceDir, '.vuepress/override.styl')
5053
const hasUserOverride = fs.existsSync(overridePath)
51-
writeTemp(`override.styl`, hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``)
54+
await writeTemp(`override.styl`, hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``)
5255
}
5356

5457
return options
@@ -141,13 +144,13 @@ async function resolveOptions (sourceDir) {
141144
}
142145

143146
// resolve pages
144-
const pagesData = options.pageFiles.map(file => {
147+
const pagesData = await Promise.all(options.pageFiles.map(async (file) => {
145148
const data = {
146149
path: fileToPath(file)
147150
}
148151

149152
// extract yaml frontmatter
150-
const content = fs.readFileSync(path.resolve(sourceDir, file), 'utf-8')
153+
const content = await readFile(path.resolve(sourceDir, file), 'utf-8')
151154
const frontmatter = yaml.loadFront(content)
152155
// infer title
153156
const title = inferTitle(frontmatter)
@@ -163,7 +166,7 @@ async function resolveOptions (sourceDir) {
163166
data.frontmatter = frontmatter
164167
}
165168
return data
166-
})
169+
}))
167170

168171
// resolve site data
169172
options.siteData = {

lib/util.js

+11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,16 @@ exports.parseFrontmatter = content => {
4343
return yaml.loadFront(content)
4444
}
4545

46+
const LRU = require('lru-cache')
47+
const cache = LRU({ max: 1000 })
48+
4649
exports.extractHeaders = (content, include = []) => {
50+
const key = content + include.join(',')
51+
const hit = cache.get(key)
52+
if (hit) {
53+
return hit
54+
}
55+
4756
const md = require('./markdown')({})
4857
const S = require('string')
4958
const tokens = md.parse(content, {})
@@ -59,5 +68,7 @@ exports.extractHeaders = (content, include = []) => {
5968
})
6069
}
6170
})
71+
72+
cache.set(key, res)
6273
return res
6374
}

lib/webpack/markdownLoader.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@ const { EventEmitter } = require('events')
55
const { getOptions } = require('loader-utils')
66
const yaml = require('yaml-front-matter')
77
const { inferTitle, extractHeaders } = require('../util')
8+
const LRU = require('lru-cache')
89

9-
const cache = new Map()
10-
const devCache = new Map()
10+
const cache = LRU({ max: 1000 })
11+
const devCache = LRU({ max: 1000 })
1112

1213
module.exports = function (src) {
14+
const isProd = process.env.NODE_ENV === 'production'
15+
const isServer = this.target === 'node'
16+
const { markdown, sourceDir } = getOptions(this)
17+
1318
// we implement a manual cache here because this loader is chained before
1419
// vue-loader, and will be applied on the same file multiple times when
1520
// selecting the individual blocks.
1621
const file = this.resourcePath
1722
const key = hash(file + src)
1823
const cached = cache.get(key)
19-
if (cached) {
24+
if (cached && (isProd || /\?vue/.test(this.resourceQuery))) {
2025
return cached
2126
}
2227

23-
const isProd = process.env.NODE_ENV === 'production'
24-
const isServer = this.target === 'node'
25-
const { markdown, sourceDir } = getOptions(this)
26-
2728
const frontmatter = yaml.loadFront(src)
2829
const content = frontmatter.__content
2930

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"koa-mount": "^3.0.0",
5959
"koa-static": "^4.0.2",
6060
"loader-utils": "^1.1.0",
61+
"lru-cache": "^4.1.2",
6162
"markdown-it": "^8.4.1",
6263
"markdown-it-anchor": "^4.0.0",
6364
"markdown-it-container": "^2.0.0",

0 commit comments

Comments
 (0)