提交 6f7bff63 编写于 作者: E Evan You

avoid unnecessary reloads

上级 cc8080f1
......@@ -3,13 +3,8 @@ import Router from 'vue-router'
import Content from './Content'
import NotFound from '~notFound'
import metadataMixin from './metadataMixin'
// .temp/siteData.js is a dynamically generated file that:
// 1. registers all *.md pages and *.vue files found in _components as global
// async components;
// 2. exports siteData
// 3. exports routes
import { routes, siteData } from './.temp/siteData'
import { routes } from './.temp/routes'
import { siteData } from './.temp/siteData'
Vue.use(Router)
......
......@@ -8,36 +8,41 @@ const { inferTitle, extractHeaders } = require('./util')
mkdirp(tempPath)
const tempCache = new Map()
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)
tempCache.set(file, content)
}
}
module.exports = async function prepare (sourceDir) {
// 1. load options
const options = await resolveOptions(sourceDir)
// 2. generate dynamic component registration file
// 2. generate routes & user components registration code
const routesCode = await genRoutesFile(options)
const componentCode = await genComponentRegistrationFile(options)
// 3. generate routes
const routesCode = await genRoutesFile(options)
writeTemp('routes.js', [
componentCode,
routesCode
].join('\n\n'))
// 4. generate siteData
// 3. generate siteData
const dataCode = `export const siteData = ${JSON.stringify(options.siteData, null, 2)}`
writeTemp('siteData.js', dataCode)
fs.writeFileSync(
path.join(tempPath, 'siteData.js'),
[
componentCode,
routesCode,
dataCode
].join('\n\n')
)
// 5. generate basic polyfill if need to support older browsers
// 4. generate basic polyfill if need to support older browsers
let polyfillCode = ``
if (!options.siteConfig.evergreen) {
polyfillCode =
`import 'es6-promise/auto'
if (!Object.assign) Object.assign = require('object-assign')`
}
fs.writeFileSync(path.join(tempPath, 'polyfill.js'), polyfillCode)
writeTemp('polyfill.js', polyfillCode)
return options
}
......@@ -63,7 +68,7 @@ async function resolveOptions (sourceDir) {
? path.resolve(siteConfig.dest)
: path.resolve(sourceDir, '.vuepress/dist'),
publicPath: siteConfig.base || '/',
pageFiles: await globby(['**/*.md', '!.vuepress'], { cwd: sourceDir }),
pageFiles: sort(await globby(['**/*.md', '!.vuepress'], { cwd: sourceDir })),
pagesData: null,
themePath: null,
notFoundPath: null
......@@ -186,7 +191,7 @@ async function resolveComponents (sourceDir) {
if (!fs.existsSync(componentDir)) {
return
}
return await globby(['**/*.vue'], { cwd: componentDir })
return sort(await globby(['**/*.vue'], { cwd: componentDir }))
}
async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
......@@ -212,3 +217,11 @@ async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
`export const routes = [${pages.map(genRoute).join(',')}\n]`
)
}
function sort (arr) {
return arr.sort((a, b) => {
if (a < b) return -1
if (a > b) return 1
return 0
})
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册