prepare.js 3.1 KB
Newer Older
E
Evan You 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
const fs = require('fs')
const path = require('path')
const globby = require('globby')
const yaml = require('yaml-front-matter')
const tempPath = path.resolve(__dirname, 'app/.temp')

module.exports = async function prepare (sourceDir) {
  // 1. load options
  const options = await resolveOptions(sourceDir)

  // 2. generate dynamic component registration file
  await genComponentRegistrationFile(options)

  // 3. generate routes
  await genRoutesFile(options)

  return options
}

async function resolveOptions (sourceDir) {
  const configPath = path.resolve(sourceDir, 'vuepress.config.js')
  const siteConfig = fs.existsSync(configPath) ? require(configPath) : {}

  const options = {
    siteConfig,
    sourceDir,
    publicPath: siteConfig.baseUrl || '/',
    themePath: path.resolve(__dirname, 'default-theme/App.vue'),
    pages: await globby(['**/*.md'], { cwd: sourceDir })
  }

E
wip  
Evan You 已提交
32 33 34 35
  // resolve theme
  const themePath = path.resolve(sourceDir, '_theme/App.vue')
  if (fs.existsSync(themePath)) {
    options.themePath = themePath
E
Evan You 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  }

  const pagesData = {}
  options.pages.forEach(file => {
    const name = file.replace(/\.md$/, '')
    const urlPath = '/' + (name === 'index' ? '' : name)
    const componentName = toComponentName(file)
    const content = fs.readFileSync(path.resolve(sourceDir, file), 'utf-8')
    const frontmatter = yaml.loadFront(content)
    delete frontmatter.__content
    pagesData[urlPath] = {
      name,
      path: urlPath,
      componentName,
      frontmatter
    }
  })

  options.siteData = Object.assign({}, siteConfig.data, {
    pages: pagesData
  })

  return options
}

function toComponentName (file) {
  const isPage = /\.md$/.test(file)
  return (
    (isPage ? `page-` : ``) +
    file
      .replace(/\.(vue|md)$/, '')
      .replace(/\/|\\/, '-')
  )
}

async function genComponentRegistrationFile ({ sourceDir, pages }) {
  function genImport (file) {
    const name = toComponentName(file)
    const baseDir = /\.md$/.test(file)
      ? sourceDir
      : path.resolve(sourceDir, '_components')
    const absolutePath = path.resolve(baseDir, file)
    const code = `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))`
    return code
  }

  const components = (await resolveComponents(sourceDir)) || []
  const all = [...pages, ...components]
  const file = `import Vue from 'vue'\n` + all.map(genImport).join('\n')
  fs.writeFileSync(path.join(tempPath, 'register-components.js'), file)
}

async function resolveComponents (sourceDir) {
  const componentDir = path.resolve(sourceDir, '_components')
  if (!fs.existsSync(componentDir)) {
    return
  }
  return await globby(['*.vue'], { cwd: componentDir })
}

async function genRoutesFile ({ sourceDir, pages }) {
  function genRoute (file) {
    const name = file.replace(/\.md$/, '')
    const code = `
    {
      path: ${JSON.stringify('/' + (name === 'index' ? '' : name))},
      component: Theme
    }`
    return code
  }

  const file =
    `import Theme from '~theme'\n` +
    `export default [${pages.map(genRoute).join(',')}\n]`
  fs.writeFileSync(path.join(tempPath, 'routes.js'), file)
}