build.js 6.2 KB
Newer Older
E
cli  
Evan You 已提交
1
module.exports = async function build (sourceDir, cliOptions = {}) {
E
Evan You 已提交
2 3 4 5
  process.env.NODE_ENV = 'production'

  const path = require('path')
  const webpack = require('webpack')
E
Evan You 已提交
6
  const readline = require('readline')
7
  const escape = require('escape-html')
E
wip  
Evan You 已提交
8

U
ULIVZ 已提交
9
  const { chalk, fs, logger } = require('@vuepress/shared-utils')
U
ULIVZ 已提交
10
  const prepare = require('./prepare/index')
E
Evan You 已提交
11 12
  const createClientConfig = require('./webpack/createClientConfig')
  const createServerConfig = require('./webpack/createServerConfig')
E
wip  
Evan You 已提交
13
  const { createBundleRenderer } = require('vue-server-renderer')
U
ULIVZ 已提交
14
  const { normalizeHeadTag, applyUserWebpackConfig } = require('./util/index')
E
Evan You 已提交
15

U
ULIVZ 已提交
16
  logger.wait('\nExtracting site metadata...')
17 18
  const { theme, plugins, temp } = cliOptions
  const options = await prepare(sourceDir, { theme, plugins, temp, isProd: true })
E
cli  
Evan You 已提交
19 20 21
  if (cliOptions.outDir) {
    options.outDir = cliOptions.outDir
  }
E
Evan You 已提交
22

E
Evan You 已提交
23
  const { outDir } = options
24
  if (path.resolve() === outDir) {
U
ULIVZ 已提交
25
    return console.error(logger.error(chalk.red('Unexpected option: outDir cannot be set to the current working directory.\n'), false))
26
  }
E
Evan You 已提交
27
  await fs.remove(outDir)
28
  logger.debug('Dist directory: ' + chalk.gray(path.resolve(outDir)))
E
Evan You 已提交
29

E
Evan You 已提交
30 31 32 33 34 35 36 37 38
  let clientConfig = createClientConfig(options, cliOptions).toConfig()
  let serverConfig = createServerConfig(options, cliOptions).toConfig()

  // apply user config...
  const userConfig = options.siteConfig.configureWebpack
  if (userConfig) {
    clientConfig = applyUserWebpackConfig(userConfig, clientConfig, false)
    serverConfig = applyUserWebpackConfig(userConfig, serverConfig, true)
  }
E
Evan You 已提交
39

E
wip  
Evan You 已提交
40
  // compile!
E
Evan You 已提交
41
  const stats = await compile([clientConfig, serverConfig])
E
wip  
Evan You 已提交
42

E
Evan You 已提交
43 44
  const serverBundle = require(path.resolve(outDir, 'manifest/server.json'))
  const clientManifest = require(path.resolve(outDir, 'manifest/client.json'))
E
wip  
Evan You 已提交
45 46

  // remove manifests after loading them.
E
Evan You 已提交
47
  await fs.remove(path.resolve(outDir, 'manifest'))
E
wip  
Evan You 已提交
48

E
Evan You 已提交
49
  // find and remove empty style chunk caused by
E
Evan You 已提交
50
  // https://github.com/webpack-contrib/mini-css-extract-plugin/issues/85
E
Evan You 已提交
51
  // TODO remove when it's fixed
E
Evan You 已提交
52 53
  await workaroundEmptyStyleChunk()

E
Evan You 已提交
54
  // create server renderer using built manifests
E
wip  
Evan You 已提交
55 56 57 58
  const renderer = createBundleRenderer(serverBundle, {
    clientManifest,
    runInNewContext: false,
    inject: false,
59
    shouldPrefetch: options.siteConfig.shouldPrefetch || (() => true),
60
    template: await fs.readFile(options.ssrTemplate, 'utf-8')
E
wip  
Evan You 已提交
61 62
  })

E
Evan You 已提交
63
  // pre-render head tags from user config
E
wip  
Evan You 已提交
64 65 66 67
  const userHeadTags = (options.siteConfig.head || [])
    .map(renderHeadTag)
    .join('\n  ')

E
Evan You 已提交
68
  // render pages
U
ULIVZ 已提交
69
  logger.wait('Rendering static HTML...')
70
  for (const page of options.pages) {
E
Evan You 已提交
71 72
    await renderPage(page)
  }
E
wip  
Evan You 已提交
73

E
Evan You 已提交
74
  // if the user does not have a custom 404.md, generate the theme's default
75
  if (!options.pages.some(p => p.path === '/404.html')) {
E
lint  
Evan You 已提交
76
    await renderPage({ path: '/404.html' })
E
Evan You 已提交
77 78
  }

E
Evan You 已提交
79 80 81
  readline.clearLine(process.stdout, 0)
  readline.cursorTo(process.stdout, 0)

82
  await options.pluginAPI.options.generated.apply()
83

E
Evan You 已提交
84
  // DONE.
E
cli  
Evan You 已提交
85
  const relativeDir = path.relative(process.cwd(), outDir)
U
ULIVZ 已提交
86
  logger.success(`\n${chalk.green('Success!')} Generated static files in ${chalk.cyan(relativeDir)}.\n`)
E
Evan You 已提交
87 88

  // --- helpers ---
E
Evan You 已提交
89 90 91 92 93 94 95 96 97 98 99

  function compile (config) {
    return new Promise((resolve, reject) => {
      webpack(config, (err, stats) => {
        if (err) {
          return reject(err)
        }
        if (stats.hasErrors()) {
          stats.toJson().errors.forEach(err => {
            console.error(err)
          })
E
tweaks  
Evan You 已提交
100
          reject(new Error(`Failed to compile with errors.`))
E
Evan You 已提交
101 102
          return
        }
103 104 105 106 107
        if (cliOptions.debug && stats.hasWarnings()) {
          stats.toJson().warnings.forEach(warning => {
            console.warn(warning)
          })
        }
E
Evan You 已提交
108
        resolve(stats.toJson({ modules: false }))
E
Evan You 已提交
109 110 111
      })
    })
  }
E
wip  
Evan You 已提交
112

E
Evan You 已提交
113 114 115
  function renderHeadTag (tag) {
    const { tagName, attributes, innerHTML, closeTag } = normalizeHeadTag(tag)
    return `<${tagName}${renderAttrs(attributes)}>${innerHTML}${closeTag ? `</${tagName}>` : ``}`
E
wip  
Evan You 已提交
116 117 118 119 120
  }

  function renderAttrs (attrs = {}) {
    const keys = Object.keys(attrs)
    if (keys.length) {
121
      return ' ' + keys.map(name => `${name}="${escape(attrs[name])}"`).join(' ')
E
wip  
Evan You 已提交
122 123 124 125
    } else {
      return ''
    }
  }
E
Evan You 已提交
126

E
Evan You 已提交
127 128
  async function renderPage (page) {
    const pagePath = page.path
E
Evan You 已提交
129 130 131
    readline.clearLine(process.stdout, 0)
    readline.cursorTo(process.stdout, 0)
    process.stdout.write(`Rendering page: ${pagePath}`)
E
Evan You 已提交
132

U
ULIVZ 已提交
133
    // #565 Avoid duplicate description meta at SSR.
134 135 136
    const meta = (page.frontmatter && page.frontmatter.meta || []).filter(item => item.name !== 'description')
    const pageMeta = renderPageMeta(meta)

E
Evan You 已提交
137 138
    const context = {
      url: pagePath,
E
Evan You 已提交
139
      userHeadTags,
E
Evan You 已提交
140
      pageMeta,
E
Evan You 已提交
141
      title: 'VuePress',
142 143
      lang: 'en',
      description: ''
E
Evan You 已提交
144 145 146 147 148 149
    }

    let html
    try {
      html = await renderer.renderToString(context)
    } catch (e) {
U
ULIVZ 已提交
150
      console.error(logger.error(chalk.red(`Error rendering ${pagePath}:`), false))
E
Evan You 已提交
151
      throw e
E
Evan You 已提交
152
    }
153
    const filename = decodeURIComponent(pagePath.replace(/\/$/, '/index.html').replace(/^\//, ''))
E
Evan You 已提交
154
    const filePath = path.resolve(outDir, filename)
E
Evan You 已提交
155 156
    await fs.ensureDir(path.dirname(filePath))
    await fs.writeFile(filePath, html)
E
Evan You 已提交
157
  }
E
Evan You 已提交
158

E
Evan You 已提交
159 160 161 162 163
  function renderPageMeta (meta) {
    if (!meta) return ''
    return meta.map(m => {
      let res = `<meta`
      Object.keys(m).forEach(key => {
164
        res += ` ${key}="${escape(m[key])}"`
E
Evan You 已提交
165 166 167 168 169
      })
      return res + `>`
    }).join('')
  }

E
Evan You 已提交
170 171 172 173
  async function workaroundEmptyStyleChunk () {
    const styleChunk = stats.children[0].assets.find(a => {
      return /styles\.\w{8}\.js$/.test(a.name)
    })
174
    if (!styleChunk) return
E
Evan You 已提交
175
    const styleChunkPath = path.resolve(outDir, styleChunk.name)
E
Evan You 已提交
176 177
    const styleChunkContent = await fs.readFile(styleChunkPath, 'utf-8')
    await fs.remove(styleChunkPath)
E
Evan You 已提交
178 179 180 181 182 183
    // prepend it to app.js.
    // this is necessary for the webpack runtime to work properly.
    const appChunk = stats.children[0].assets.find(a => {
      return /app\.\w{8}\.js$/.test(a.name)
    })
    const appChunkPath = path.resolve(outDir, appChunk.name)
E
Evan You 已提交
184 185
    const appChunkContent = await fs.readFile(appChunkPath, 'utf-8')
    await fs.writeFile(appChunkPath, styleChunkContent + appChunkContent)
E
Evan You 已提交
186
  }
E
Evan You 已提交
187
}