Skip to content

Commit 6911756

Browse files
committed
refactor: tweaks
1 parent 80cb1d5 commit 6911756

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

packages/@vuepress/core/lib/build.js

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,21 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
1616
const { normalizeHeadTag, applyUserWebpackConfig } = require('./util/index')
1717

1818
logger.wait('\nExtracting site metadata...')
19-
const options = await prepare(sourceDir, cliOptions, true /* isProd */)
20-
if (cliOptions.outDir) {
21-
options.outDir = cliOptions.outDir
22-
}
19+
const ctx = await prepare(sourceDir, cliOptions, true /* isProd */)
2320

24-
const { outDir } = options
25-
if (process.cwd() === outDir) {
21+
const { outDir, cwd } = ctx
22+
if (cwd === outDir) {
2623
return console.error(logger.error(chalk.red('Unexpected option: outDir cannot be set to the current working directory.\n'), false))
2724
}
25+
2826
await fs.remove(outDir)
29-
logger.debug('Dist directory: ' + chalk.gray(path.resolve(process.cwd(), outDir)))
27+
logger.debug('Dist directory: ' + chalk.gray(outDir))
3028

31-
let clientConfig = createClientConfig(options, cliOptions).toConfig()
32-
let serverConfig = createServerConfig(options, cliOptions).toConfig()
29+
let clientConfig = createClientConfig(ctx, cliOptions).toConfig()
30+
let serverConfig = createServerConfig(ctx, cliOptions).toConfig()
3331

3432
// apply user config...
35-
const userConfig = options.siteConfig.configureWebpack
33+
const userConfig = ctx.siteConfig.configureWebpack
3634
if (userConfig) {
3735
clientConfig = applyUserWebpackConfig(userConfig, clientConfig, false)
3836
serverConfig = applyUserWebpackConfig(userConfig, serverConfig, true)
@@ -57,33 +55,33 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
5755
clientManifest,
5856
runInNewContext: false,
5957
inject: false,
60-
shouldPrefetch: options.siteConfig.shouldPrefetch || (() => true),
61-
template: await fs.readFile(options.ssrTemplate, 'utf-8')
58+
shouldPrefetch: ctx.siteConfig.shouldPrefetch || (() => true),
59+
template: await fs.readFile(ctx.ssrTemplate, 'utf-8')
6260
})
6361

6462
// pre-render head tags from user config
65-
const userHeadTags = (options.siteConfig.head || [])
63+
const userHeadTags = (ctx.siteConfig.head || [])
6664
.map(renderHeadTag)
6765
.join('\n ')
6866

6967
// render pages
7068
logger.wait('Rendering static HTML...')
71-
for (const page of options.pages) {
69+
for (const page of ctx.pages) {
7270
await renderPage(page)
7371
}
7472

7573
// 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')) {
74+
if (!ctx.pages.some(p => p.path === '/404.html')) {
7775
await renderPage({ path: '/404.html' })
7876
}
7977

8078
readline.clearLine(process.stdout, 0)
8179
readline.cursorTo(process.stdout, 0)
8280

83-
await options.pluginAPI.options.generated.apply()
81+
await ctx.pluginAPI.options.generated.apply()
8482

8583
// DONE.
86-
const relativeDir = path.relative(process.cwd(), outDir)
84+
const relativeDir = path.relative(cwd, outDir)
8785
logger.success(`\n${chalk.green('Success!')} Generated static files in ${chalk.cyan(relativeDir)}.\n`)
8886

8987
// --- helpers ---

packages/@vuepress/core/lib/dev.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
2020
const { frontmatterEmitter } = require('@vuepress/markdown-loader')
2121

2222
logger.wait('\nExtracting site metadata...')
23-
const options = await prepare(sourceDir, cliOptions, false /* isProd */)
23+
const ctx = await prepare(sourceDir, cliOptions, false /* isProd */)
2424

2525
// setup watchers to update options and dynamically generated files
2626
const update = (reason) => {
2727
logger.debug(`Re-prepare due to ${chalk.cyan(reason)}`)
28-
options.pluginAPI.options.updated.syncApply()
28+
ctx.pluginAPI.options.updated.syncApply()
2929
prepare(sourceDir, cliOptions, false /* isProd */).catch(err => {
3030
console.error(logger.error(chalk.red(err.stack), false))
3131
})
@@ -60,35 +60,35 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
6060
frontmatterEmitter.on('update', () => update('frontmatter or headers change'))
6161

6262
// resolve webpack config
63-
let config = createClientConfig(options)
63+
let config = createClientConfig(ctx)
6464

6565
config
6666
.plugin('html')
6767
// using a fork of html-webpack-plugin to avoid it requiring webpack
6868
// internals from an incompatible version.
6969
.use(require('vuepress-html-webpack-plugin'), [{
70-
template: options.devTemplate
70+
template: ctx.devTemplate
7171
}])
7272

7373
config
7474
.plugin('site-data')
7575
.use(HeadPlugin, [{
76-
tags: options.siteConfig.head || []
76+
tags: ctx.siteConfig.head || []
7777
}])
7878

79-
const port = await resolvePort(cliOptions.port || options.siteConfig.port)
80-
const { host, displayHost } = await resolveHost(cliOptions.host || options.siteConfig.host)
79+
const port = await resolvePort(cliOptions.port || ctx.siteConfig.port)
80+
const { host, displayHost } = await resolveHost(cliOptions.host || ctx.siteConfig.host)
8181

8282
config
8383
.plugin('vuepress-log')
8484
.use(DevLogPlugin, [{
8585
port,
8686
displayHost,
87-
publicPath: options.base
87+
publicPath: ctx.base
8888
}])
8989

9090
config = config.toConfig()
91-
const userConfig = options.siteConfig.configureWebpack
91+
const userConfig = ctx.siteConfig.configureWebpack
9292
if (userConfig) {
9393
config = applyUserWebpackConfig(userConfig, config, false /* isServer */)
9494
}
@@ -111,8 +111,7 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
111111
port,
112112
add: app => {
113113
// apply plugin options to extend dev server.
114-
const { pluginAPI } = options
115-
pluginAPI.options.enhanceDevServer.syncApply(app)
114+
ctx.pluginAPI.options.enhanceDevServer.syncApply(app)
116115

117116
const userPublic = path.resolve(sourceDir, '.vuepress/public')
118117

@@ -121,7 +120,7 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
121120

122121
// respect base when serving static files...
123122
if (fs.existsSync(userPublic)) {
124-
app.use(mount(options.base, serveStatic(userPublic)))
123+
app.use(mount(ctx.base, serveStatic(userPublic)))
125124
}
126125

127126
app.use(convert(history({

packages/@vuepress/core/lib/prepare/AppContext.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,15 @@ module.exports = class AppContext {
4949
this.siteConfig = this.siteConfig(this)
5050
}
5151

52+
// TODO custom cwd.
53+
this.cwd = process.cwd()
54+
5255
this.base = this.siteConfig.base || '/'
5356
this.themeConfig = this.siteConfig.themeConfig || {}
54-
this.outDir = this.siteConfig.dest
55-
? require('path').resolve(process.cwd(), this.siteConfig.dest)
57+
58+
const rawOutDir = cliOptions.outDir || this.siteConfig.dest
59+
this.outDir = rawOutDir
60+
? require('path').resolve(this.cwd, rawOutDir)
5661
: require('path').resolve(sourceDir, '.vuepress/dist')
5762

5863
this.pluginAPI = new PluginAPI(this)

0 commit comments

Comments
 (0)