提交 9719bc32 编写于 作者: E Evan You

tweak cli output

上级 e71e0860
......@@ -5,6 +5,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
const path = require('path')
const chalk = require('chalk')
const webpack = require('webpack')
const readline = require('readline')
const { promisify } = require('util')
const rimraf = promisify(require('rimraf'))
const mkdirp = promisify(require('mkdirp'))
......@@ -17,6 +18,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
const { createBundleRenderer } = require('vue-server-renderer')
const { normalizeHeadTag, applyUserWebpackConfig } = require('./util')
process.stdout.write('Extracting site metadata...')
const options = await prepare(sourceDir)
if (cliOptions.outDir) {
options.outDir = cliOptions.outDir
......@@ -28,6 +30,9 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
let clientConfig = createClientConfig(options, cliOptions).toConfig()
let serverConfig = createServerConfig(options, cliOptions).toConfig()
// disable uglify for server
serverConfig.optimization = { minimizer: [] }
// apply user config...
const userConfig = options.siteConfig.configureWebpack
if (userConfig) {
......@@ -53,7 +58,6 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
const renderer = createBundleRenderer(serverBundle, {
clientManifest,
runInNewContext: false,
// shouldPrefetch: () => false,
inject: false,
template: fs.readFileSync(path.resolve(__dirname, 'app/index.ssr.html'), 'utf-8')
})
......@@ -64,14 +68,21 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
.join('\n ')
// render pages
await Promise.all(options.siteData.pages.map(renderPage))
console.log('Rendering static HTML...')
for (const page of options.siteData.pages) {
await renderPage(page)
}
// if the user does not have a custom 404.md, generate the theme's default
if (!options.siteData.pages.some(p => p.path === '/404.html')) {
await renderPage({ path: '/404.html' })
}
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
if (options.siteConfig.serviceWorker) {
console.log('Generating service worker...')
const wbb = require('workbox-build')
wbb.generateSW({
swDest: path.resolve(outDir, 'service-worker.js'),
......@@ -120,8 +131,11 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
async function renderPage (page) {
const pagePath = page.path
const pageMeta = renderPageMeta(page.frontmatter && page.frontmatter.meta)
readline.clearLine(process.stdout, 0)
readline.cursorTo(process.stdout, 0)
process.stdout.write(`Rendering page: ${pagePath}`)
const pageMeta = renderPageMeta(page.frontmatter && page.frontmatter.meta)
const context = {
url: pagePath,
userHeadTags,
......
......@@ -17,6 +17,7 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
const { applyUserWebpackConfig } = require('./util')
const { frontmatterEmitter } = require('./webpack/markdownLoader')
process.stdout.write('Extracting site metadata...')
const options = await prepare(sourceDir)
// setup watchers to update options and dynamically generated files
......
......@@ -2,6 +2,9 @@ const fs = require('fs')
const path = require('path')
const globby = require('globby')
const mkdirp = require('mkdirp')
const { promisify } = require('util')
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const yaml = require('yaml-front-matter')
const tempPath = path.resolve(__dirname, 'app/.temp')
const { inferTitle, extractHeaders } = require('./util')
......@@ -9,11 +12,11 @@ const { inferTitle, extractHeaders } = require('./util')
mkdirp(tempPath)
const tempCache = new Map()
function writeTemp (file, content) {
async function writeTemp (file, content) {
// cache write to avoid hitting the dist if it didn't change
const cached = tempCache.get(file)
if (cached !== content) {
fs.writeFileSync(path.join(tempPath, file), content)
await writeFile(path.join(tempPath, file), content)
tempCache.set(file, content)
}
}
......@@ -26,14 +29,14 @@ module.exports = async function prepare (sourceDir) {
const routesCode = await genRoutesFile(options)
const componentCode = await genComponentRegistrationFile(options)
writeTemp('routes.js', [
await writeTemp('routes.js', [
componentCode,
routesCode
].join('\n\n'))
// 3. generate siteData
const dataCode = `export const siteData = ${JSON.stringify(options.siteData, null, 2)}`
writeTemp('siteData.js', dataCode)
await writeTemp('siteData.js', dataCode)
// 4. generate basic polyfill if need to support older browsers
let polyfillCode = ``
......@@ -42,13 +45,13 @@ module.exports = async function prepare (sourceDir) {
`import 'es6-promise/auto'
if (!Object.assign) Object.assign = require('object-assign')`
}
writeTemp('polyfill.js', polyfillCode)
await writeTemp('polyfill.js', polyfillCode)
// 5. handle user override
if (options.useDefaultTheme) {
const overridePath = path.resolve(sourceDir, '.vuepress/override.styl')
const hasUserOverride = fs.existsSync(overridePath)
writeTemp(`override.styl`, hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``)
await writeTemp(`override.styl`, hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``)
}
return options
......@@ -141,13 +144,13 @@ async function resolveOptions (sourceDir) {
}
// resolve pages
const pagesData = options.pageFiles.map(file => {
const pagesData = await Promise.all(options.pageFiles.map(async (file) => {
const data = {
path: fileToPath(file)
}
// extract yaml frontmatter
const content = fs.readFileSync(path.resolve(sourceDir, file), 'utf-8')
const content = await readFile(path.resolve(sourceDir, file), 'utf-8')
const frontmatter = yaml.loadFront(content)
// infer title
const title = inferTitle(frontmatter)
......@@ -163,7 +166,7 @@ async function resolveOptions (sourceDir) {
data.frontmatter = frontmatter
}
return data
})
}))
// resolve site data
options.siteData = {
......
......@@ -43,7 +43,16 @@ exports.parseFrontmatter = content => {
return yaml.loadFront(content)
}
const LRU = require('lru-cache')
const cache = LRU({ max: 1000 })
exports.extractHeaders = (content, include = []) => {
const key = content + include.join(',')
const hit = cache.get(key)
if (hit) {
return hit
}
const md = require('./markdown')({})
const S = require('string')
const tokens = md.parse(content, {})
......@@ -59,5 +68,7 @@ exports.extractHeaders = (content, include = []) => {
})
}
})
cache.set(key, res)
return res
}
......@@ -5,25 +5,26 @@ const { EventEmitter } = require('events')
const { getOptions } = require('loader-utils')
const yaml = require('yaml-front-matter')
const { inferTitle, extractHeaders } = require('../util')
const LRU = require('lru-cache')
const cache = new Map()
const devCache = new Map()
const cache = LRU({ max: 1000 })
const devCache = LRU({ max: 1000 })
module.exports = function (src) {
const isProd = process.env.NODE_ENV === 'production'
const isServer = this.target === 'node'
const { markdown, sourceDir } = getOptions(this)
// we implement a manual cache here because this loader is chained before
// vue-loader, and will be applied on the same file multiple times when
// selecting the individual blocks.
const file = this.resourcePath
const key = hash(file + src)
const cached = cache.get(key)
if (cached) {
if (cached && (isProd || /\?vue/.test(this.resourceQuery))) {
return cached
}
const isProd = process.env.NODE_ENV === 'production'
const isServer = this.target === 'node'
const { markdown, sourceDir } = getOptions(this)
const frontmatter = yaml.loadFront(src)
const content = frontmatter.__content
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册