prepare.js 8.2 KB
Newer Older
E
Evan You 已提交
1
const path = require('path')
2
const fs = require('fs-extra')
E
Evan You 已提交
3
const globby = require('globby')
A
Aliex Leung 已提交
4
const yamlParser = require('js-yaml')
5 6
const yaml = require('yaml-front-matter')
const createMarkdown = require('./markdown')
E
Evan You 已提交
7
const tempPath = path.resolve(__dirname, 'app/.temp')
E
Evan You 已提交
8
const { inferTitle, extractHeaders } = require('./util')
E
Evan You 已提交
9

E
Evan You 已提交
10
fs.ensureDirSync(tempPath)
E
Evan You 已提交
11

E
Evan You 已提交
12
const tempCache = new Map()
E
Evan You 已提交
13
async function writeTemp (file, content) {
E
Evan You 已提交
14 15 16
  // cache write to avoid hitting the dist if it didn't change
  const cached = tempCache.get(file)
  if (cached !== content) {
E
Evan You 已提交
17
    await fs.writeFile(path.join(tempPath, file), content)
E
Evan You 已提交
18 19 20 21
    tempCache.set(file, content)
  }
}

E
Evan You 已提交
22 23 24 25
module.exports = async function prepare (sourceDir) {
  // 1. load options
  const options = await resolveOptions(sourceDir)

E
Evan You 已提交
26 27
  // 2. generate routes & user components registration code
  const routesCode = await genRoutesFile(options)
E
tweaks  
Evan You 已提交
28
  const componentCode = await genComponentRegistrationFile(options)
E
Evan You 已提交
29

E
Evan You 已提交
30
  await writeTemp('routes.js', [
E
Evan You 已提交
31 32 33
    componentCode,
    routesCode
  ].join('\n\n'))
E
tweaks  
Evan You 已提交
34

E
Evan You 已提交
35
  // 3. generate siteData
E
tweaks  
Evan You 已提交
36
  const dataCode = `export const siteData = ${JSON.stringify(options.siteData, null, 2)}`
E
Evan You 已提交
37
  await writeTemp('siteData.js', dataCode)
E
tweaks  
Evan You 已提交
38

E
Evan You 已提交
39
  // 4. generate basic polyfill if need to support older browsers
40 41 42 43 44 45
  let polyfillCode = ``
  if (!options.siteConfig.evergreen) {
    polyfillCode =
`import 'es6-promise/auto'
if (!Object.assign) Object.assign = require('object-assign')`
  }
E
Evan You 已提交
46
  await writeTemp('polyfill.js', polyfillCode)
47

E
Evan You 已提交
48
  // 5. handle user override
E
Evan You 已提交
49
  if (options.useDefaultTheme) {
E
Evan You 已提交
50 51
    const overridePath = path.resolve(sourceDir, '.vuepress/override.styl')
    const hasUserOverride = fs.existsSync(overridePath)
E
Evan You 已提交
52
    await writeTemp(`override.styl`, hasUserOverride ? `@import(${JSON.stringify(overridePath)})` : ``)
E
Evan You 已提交
53 54
  }

方剑成 已提交
55 56 57 58 59 60 61 62 63 64
  // 6. handle enhanceApp.js
  const enhancePath = path.resolve(sourceDir, '.vuepress/enhanceApp.js')
  const hasEnhancePath = fs.existsSync(enhancePath)
  await writeTemp(
    'enhanceApp.js',
    hasEnhancePath
      ? `export { default } from ${JSON.stringify(enhancePath)}`
      : `export default function () {}`
  )

E
Evan You 已提交
65 66 67 68
  return options
}

async function resolveOptions (sourceDir) {
E
Evan You 已提交
69 70
  const vuepressDir = path.resolve(sourceDir, '.vuepress')
  const configPath = path.resolve(vuepressDir, 'config.js')
A
Aliex Leung 已提交
71
  const configYmlPath = path.resolve(vuepressDir, 'config.yml')
72 73

  delete require.cache[configPath]
A
Aliex Leung 已提交
74 75 76 77 78 79 80
  let siteConfig = {}
  if (fs.existsSync(configYmlPath)) {
    const content = await fs.readFile(configYmlPath, 'utf-8')
    siteConfig = yamlParser.safeLoad(content)
  } else if (fs.existsSync(configPath)) {
    siteConfig = require(configPath)
  }
E
Evan You 已提交
81

E
Evan You 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  // normalize head tag urls for base
  const base = siteConfig.base || '/'
  if (base !== '/' && siteConfig.head) {
    siteConfig.head.forEach(tag => {
      const attrs = tag[1]
      if (attrs) {
        for (const name in attrs) {
          if (name === 'src' || name === 'href') {
            const value = attrs[name]
            if (value.charAt(0) === '/') {
              attrs[name] = base + value.slice(1)
            }
          }
        }
      }
    })
  }

E
Evan You 已提交
100 101 102 103 104 105
  // resolve theme
  const useDefaultTheme = (
    !siteConfig.theme &&
    !fs.existsSync(path.resolve(vuepressDir, 'theme'))
  )

E
Evan You 已提交
106
  const options = {
E
Evan You 已提交
107
    siteConfig,
E
Evan You 已提交
108
    sourceDir,
E
Evan You 已提交
109 110 111
    outDir: siteConfig.dest
      ? path.resolve(siteConfig.dest)
      : path.resolve(sourceDir, '.vuepress/dist'),
E
Evan You 已提交
112
    publicPath: base,
113
    pageFiles: sort(await globby(['**/*.md', '!.vuepress', '!node_modules'], { cwd: sourceDir })),
E
Evan You 已提交
114 115
    pagesData: null,
    themePath: null,
E
Evan You 已提交
116
    notFoundPath: null,
117 118
    useDefaultTheme,
    markdown: createMarkdown(siteConfig)
E
Evan You 已提交
119 120
  }

E
Evan You 已提交
121
  if (useDefaultTheme) {
E
Evan You 已提交
122 123 124 125
    // use default theme
    options.themePath = path.resolve(__dirname, 'default-theme/Layout.vue')
    options.notFoundPath = path.resolve(__dirname, 'default-theme/NotFound.vue')
  } else {
E
Evan You 已提交
126 127
    let themeDir
    let themePath
E
Evan You 已提交
128
    // resolve custom theme
E
Evan You 已提交
129 130 131 132 133 134 135 136 137
    if (siteConfig.theme) {
      try {
        themePath = require.resolve(`vuepress-theme-${siteConfig.theme}/Layout.vue`)
        themeDir = path.dirname(themePath)
      } catch (e) {
        throw new Error(`[vuepress] Failed to load custom theme "${
          siteConfig.theme
        }". File vuepress-theme-${siteConfig.theme}/Layout.vue does not exist.`)
      }
E
Evan You 已提交
138
    } else {
E
Evan You 已提交
139 140 141 142 143
      themeDir = path.resolve(vuepressDir, 'theme')
      themePath = path.resolve(themeDir, 'Layout.vue')
      if (!fs.existsSync(themePath)) {
        throw new Error(`[vuepress] Cannot resolve Layout.vue file in .vuepress/theme.`)
      }
E
Evan You 已提交
144
    }
E
Evan You 已提交
145
    options.themePath = themePath
E
Evan You 已提交
146

147
    const notFoundPath = path.resolve(themeDir, 'NotFound.vue')
E
Evan You 已提交
148 149 150
    if (fs.existsSync(notFoundPath)) {
      options.notFoundPath = notFoundPath
    } else {
E
Evan You 已提交
151
      options.notFoundPath = path.resolve(__dirname, 'default-theme/NotFound.vue')
E
Evan You 已提交
152
    }
E
tweaks  
Evan You 已提交
153 154
  }

E
Evan You 已提交
155
  // resolve pages
E
Evan You 已提交
156
  const pagesData = await Promise.all(options.pageFiles.map(async (file) => {
E
tweaks  
Evan You 已提交
157
    const data = {
E
Evan You 已提交
158
      path: fileToPath(file)
E
Evan You 已提交
159
    }
E
Evan You 已提交
160 161

    // extract yaml frontmatter
E
Evan You 已提交
162
    const content = await fs.readFile(path.resolve(sourceDir, file), 'utf-8')
E
Evan You 已提交
163
    const frontmatter = yaml.loadFront(content)
E
Evan You 已提交
164
    // infer title
E
Evan You 已提交
165 166 167
    const title = inferTitle(frontmatter)
    if (title) {
      data.title = title
E
Evan You 已提交
168
    }
169 170 171 172 173
    const headers = extractHeaders(
      frontmatter.__content,
      ['h2', 'h3'],
      options.markdown
    )
E
Evan You 已提交
174 175 176
    if (headers.length) {
      data.headers = headers
    }
E
Evan You 已提交
177
    delete frontmatter.__content
E
Evan You 已提交
178 179
    if (Object.keys(frontmatter).length) {
      data.frontmatter = frontmatter
E
Evan You 已提交
180
    }
E
tweaks  
Evan You 已提交
181
    return data
E
Evan You 已提交
182
  }))
E
Evan You 已提交
183

E
Evan You 已提交
184
  // resolve site data
E
Evan You 已提交
185
  options.siteData = {
E
Evan You 已提交
186 187
    title: siteConfig.title || '',
    description: siteConfig.description || '',
E
Evan You 已提交
188
    base: siteConfig.base || '/',
E
Evan You 已提交
189
    pages: pagesData,
190
    themeConfig: siteConfig.themeConfig || {},
E
Evan You 已提交
191
    locales: siteConfig.locales
E
Evan You 已提交
192
  }
E
Evan You 已提交
193 194 195 196

  return options
}

E
Evan You 已提交
197
async function genComponentRegistrationFile ({ sourceDir }) {
E
Evan You 已提交
198
  function genImport (file) {
E
Evan You 已提交
199
    const name = fileToComponentName(file)
E
Evan You 已提交
200
    const baseDir = path.resolve(sourceDir, '.vuepress/components')
E
Evan You 已提交
201 202 203 204 205
    const absolutePath = path.resolve(baseDir, file)
    const code = `Vue.component(${JSON.stringify(name)}, () => import(${JSON.stringify(absolutePath)}))`
    return code
  }
  const components = (await resolveComponents(sourceDir)) || []
E
Evan You 已提交
206
  return `import Vue from 'vue'\n` + components.map(genImport).join('\n')
E
Evan You 已提交
207 208
}

209
const indexRE = /\b(index|readme)\.md$/i
E
Evan You 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
const extRE = /\.(vue|md)$/

function fileToPath (file) {
  if (isIndexFile(file)) {
    // README.md -> /
    // foo/README.md -> /foo/
    return '/' + file.replace(indexRE, '')
  } else {
    // foo.md -> /foo.html
    // foo/bar.md -> /foo/bar.html
    return `/${file.replace(extRE, '').replace(/\\/g, '/')}.html`
  }
}

function fileToComponentName (file) {
  let normalizedName = file
    .replace(/\/|\\/g, '-')
    .replace(extRE, '')
  if (isIndexFile(file)) {
    normalizedName = normalizedName.replace(/readme$/i, 'index')
  }
  const pagePrefix = /\.md$/.test(file) ? `page-` : ``
  return `${pagePrefix}${normalizedName}`
E
Evan You 已提交
233 234 235
}

function isIndexFile (file) {
E
Evan You 已提交
236
  return indexRE.test(file)
E
Evan You 已提交
237 238
}

E
Evan You 已提交
239
async function resolveComponents (sourceDir) {
E
Evan You 已提交
240
  const componentDir = path.resolve(sourceDir, '.vuepress/components')
E
Evan You 已提交
241 242 243
  if (!fs.existsSync(componentDir)) {
    return
  }
E
Evan You 已提交
244
  return sort(await globby(['**/*.vue'], { cwd: componentDir }))
E
Evan You 已提交
245 246
}

E
Evan You 已提交
247 248 249 250
async function genRoutesFile ({ siteData: { pages }, sourceDir, pageFiles }) {
  function genRoute ({ path: pagePath }, index) {
    const file = pageFiles[index]
    const filePath = path.resolve(sourceDir, file)
251
    let code = `
E
Evan You 已提交
252
    {
E
Evan You 已提交
253 254 255 256 257 258 259 260
      path: ${JSON.stringify(pagePath)},
      component: Theme,
      beforeEnter: (to, from, next) => {
        import(${JSON.stringify(filePath)}).then(comp => {
          Vue.component(${JSON.stringify(fileToComponentName(file))}, comp.default)
          next()
        })
      }
E
Evan You 已提交
261
    }`
262 263 264 265 266 267 268 269

    if (/\/$/.test(pagePath)) {
      code += `,{
        path: ${JSON.stringify(pagePath + 'index.html')},
        redirect: ${JSON.stringify(pagePath)}
      }`
    }

E
Evan You 已提交
270 271 272
    return code
  }

E
tweaks  
Evan You 已提交
273
  return (
274
    `import Theme from '@theme'\n` +
E
tweaks  
Evan You 已提交
275 276
    `export const routes = [${pages.map(genRoute).join(',')}\n]`
  )
E
Evan You 已提交
277
}
E
Evan You 已提交
278 279 280 281 282 283 284 285

function sort (arr) {
  return arr.sort((a, b) => {
    if (a < b) return -1
    if (a > b) return 1
    return 0
  })
}