build.js 6.2 KB
Newer Older
U
ULIVZ 已提交
1 2
'use strict'

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

U
ULIVZ 已提交
6
  const { path } = require('@vuepress/shared-utils')
E
Evan You 已提交
7
  const webpack = require('webpack')
E
Evan You 已提交
8
  const readline = require('readline')
9
  const escape = require('escape-html')
E
wip  
Evan You 已提交
10

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

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

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

E
Evan You 已提交
31 32 33 34 35 36 37 38 39
  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 已提交
40

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

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

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

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

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

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

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

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

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

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

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

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

  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 已提交
101
          reject(new Error(`Failed to compile with errors.`))
E
Evan You 已提交
102 103
          return
        }
104 105 106 107 108
        if (cliOptions.debug && stats.hasWarnings()) {
          stats.toJson().warnings.forEach(warning => {
            console.warn(warning)
          })
        }
E
Evan You 已提交
109
        resolve(stats.toJson({ modules: false }))
E
Evan You 已提交
110 111 112
      })
    })
  }
E
wip  
Evan You 已提交
113

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

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

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

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

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

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

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

E
Evan You 已提交
171 172 173 174
  async function workaroundEmptyStyleChunk () {
    const styleChunk = stats.children[0].assets.find(a => {
      return /styles\.\w{8}\.js$/.test(a.name)
    })
175
    if (!styleChunk) return
E
Evan You 已提交
176
    const styleChunkPath = path.resolve(outDir, styleChunk.name)
E
Evan You 已提交
177 178
    const styleChunkContent = await fs.readFile(styleChunkPath, 'utf-8')
    await fs.remove(styleChunkPath)
E
Evan You 已提交
179 180 181 182 183 184
    // 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 已提交
185 186
    const appChunkContent = await fs.readFile(appChunkPath, 'utf-8')
    await fs.writeFile(appChunkPath, styleChunkContent + appChunkContent)
E
Evan You 已提交
187
  }
E
Evan You 已提交
188
}