Skip to content

Commit 2a46178

Browse files
committed
feat($cli): support '--cache' and '--no-cache' flag
1 parent 38b3468 commit 2a46178

File tree

8 files changed

+51
-39
lines changed

8 files changed

+51
-39
lines changed

packages/@vuepress/cli/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,24 @@ exports.bootstrap = function ({
4343
.option('-p, --port <port>', 'use specified port (default: 8080)')
4444
.option('-h, --host <host>', 'use specified host (default: 0.0.0.0)')
4545
.option('-t, --temp <temp>', 'set the directory of the temporary file')
46+
.option('-c, --cache <cache>', 'set the directory of cache')
47+
.option('--no-cache', 'clean the cache before build')
4648
.option('--debug', 'start development server in debug mode')
47-
.action((dir = '.', { host, port, debug, temp }) => {
48-
wrapCommand(dev)(path.resolve(dir), { host, port, debug, plugins, theme, temp })
49+
.action((dir = '.', { host, port, debug, temp, cache }) => {
50+
wrapCommand(dev)(path.resolve(dir), { host, port, debug, temp, cache, plugins, theme })
4951
})
5052

5153
program
5254
.command('build [targetDir]')
5355
.description('build dir as static site')
5456
.option('-d, --dest <outDir>', 'specify build output dir (default: .vuepress/dist)')
5557
.option('-t, --temp <temp>', 'set the directory of the temporary file')
58+
.option('-c, --cache <cache>', 'set the directory of cache')
59+
.option('--no-cache', 'clean the cache before build')
5660
.option('--debug', 'build in development mode for debugging')
57-
.action((dir = '.', { debug, dest, temp }) => {
61+
.action((dir = '.', { debug, dest, temp, cache }) => {
5862
const outDir = dest ? path.resolve(dest) : null
59-
wrapCommand(build)(path.resolve(dir), { debug, outDir, plugins, theme, temp })
63+
wrapCommand(build)(path.resolve(dir), { debug, outDir, plugins, theme, temp, cache })
6064
})
6165

6266
program

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
1414
const { normalizeHeadTag, applyUserWebpackConfig } = require('./util/index')
1515

1616
logger.wait('\nExtracting site metadata...')
17-
const { theme, plugins, temp } = cliOptions
18-
const options = await prepare(sourceDir, { theme, plugins, temp, isProd: true })
17+
const options = await prepare(sourceDir, cliOptions, true /* isProd */)
1918
if (cliOptions.outDir) {
2019
options.outDir = cliOptions.outDir
2120
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
1818
const { frontmatterEmitter } = require('./webpack/markdownLoader')
1919

2020
logger.wait('\nExtracting site metadata...')
21-
const { theme, plugins, temp } = cliOptions
22-
const options = await prepare(sourceDir, { theme, plugins, temp, isProd: false })
21+
const options = await prepare(sourceDir, cliOptions, false /* isProd */)
2322

2423
// setup watchers to update options and dynamically generated files
2524
const update = () => {
2625
options.pluginAPI.options.updated.syncApply()
27-
prepare(sourceDir, { theme, plugins, temp, isProd: false }).catch(err => {
26+
prepare(sourceDir, cliOptions, false /* isProd */).catch(err => {
2827
console.error(logger.error(chalk.red(err.stack), false))
2928
})
3029
}
@@ -58,7 +57,7 @@ module.exports = async function dev (sourceDir, cliOptions = {}) {
5857
frontmatterEmitter.on('update', update)
5958

6059
// resolve webpack config
61-
let config = createClientConfig(options, cliOptions)
60+
let config = createClientConfig(options)
6261

6362
config
6463
.plugin('html')

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ module.exports = class AppContext {
1919
* temp: string
2020
* }} options
2121
*/
22-
constructor (sourceDir, options) {
22+
constructor (sourceDir, cliOptions = {}, isProd) {
2323
this.sourceDir = sourceDir
24-
this._options = options
25-
this.isProd = options.isProd
24+
this.cliOptions = cliOptions
25+
this.isProd = isProd
2626

27-
const { tempPath, writeTemp } = createTemp(options.temp)
27+
const { tempPath, writeTemp } = createTemp(cliOptions.temp)
2828
this.tempPath = tempPath
2929
this.writeTemp = writeTemp
3030

@@ -90,7 +90,7 @@ module.exports = class AppContext {
9090
.use(require('../internal-plugins/layoutComponents'))
9191
.use(require('../internal-plugins/pageComponents'))
9292
// user plugin
93-
.useByPluginsConfig(this._options.plugins)
93+
.useByPluginsConfig(this.cliOptions.plugins)
9494
.useByPluginsConfig(this.siteConfig.plugins)
9595
.useByPluginsConfig(this.themePlugins)
9696
// built-in plugins
@@ -192,7 +192,7 @@ module.exports = class AppContext {
192192
* @returns { Promise<void> }
193193
*/
194194
async resolveTheme () {
195-
const theme = this.siteConfig.theme || this._options.theme
195+
const theme = this.siteConfig.theme || this.cliOptions.theme
196196
Object.assign(this, (await loadTheme(theme, this.sourceDir, this.vuepressDir)))
197197
}
198198

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
const AppContext = require('./AppContext')
22

3-
module.exports = async function prepare (sourceDir, {
4-
isProd,
5-
plugins,
6-
theme,
7-
temp
8-
}) {
9-
const appContext = new AppContext(sourceDir, { plugins, theme, isProd, temp })
3+
module.exports = async function prepare (sourceDir, cliOptions, isProd) {
4+
const appContext = new AppContext(sourceDir, cliOptions, isProd)
105
await appContext.process()
116
return appContext
127
}

packages/@vuepress/core/lib/webpack/createBaseConfig.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require('path')
2+
const { fs, logger, chalk } = require('@vuepress/shared-utils')
23

34
module.exports = function createBaseConfig ({
45
siteConfig,
@@ -7,8 +8,12 @@ module.exports = function createBaseConfig ({
78
base: publicPath,
89
themePath,
910
markdown,
10-
tempPath
11-
}, { debug } = {}, isServer) {
11+
tempPath,
12+
cliOptions: {
13+
debug,
14+
cache
15+
}
16+
}, isServer) {
1217
const Config = require('webpack-chain')
1318
const { VueLoaderPlugin } = require('vue-loader')
1419
const CSSExtractPlugin = require('mini-css-extract-plugin')
@@ -61,7 +66,17 @@ module.exports = function createBaseConfig ({
6166
config.module
6267
.noParse(/^(vue|vue-router|vuex|vuex-router-sync)$/)
6368

64-
const cacheDirectory = path.resolve(__dirname, '../../node_modules/.cache/vuepress')
69+
let cacheDirectory
70+
if (cache && typeof cache === 'string') {
71+
cacheDirectory = path.resolve(cache)
72+
} else {
73+
cacheDirectory = path.resolve(__dirname, '../../node_modules/.cache/vuepress')
74+
}
75+
logger.debug('Cache directory: ' + chalk.gray(cacheDirectory))
76+
if (!cache) {
77+
logger.tip('\nClean cache...\n')
78+
fs.emptyDirSync(cacheDirectory)
79+
}
6580
const cacheIdentifier = JSON.stringify({
6681
vuepress: require('../../package.json').version,
6782
'cache-loader': require('cache-loader').version,

packages/@vuepress/core/lib/webpack/createClientConfig.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
module.exports = function createClientConfig (options, cliOptions) {
1+
module.exports = function createClientConfig (ctx) {
22
const path = require('path')
33
const WebpackBar = require('webpackbar')
44
const createBaseConfig = require('./createBaseConfig')
55

6-
const config = createBaseConfig(options, cliOptions)
6+
const config = createBaseConfig(ctx)
77

88
config
99
.entry('app')
@@ -51,7 +51,7 @@ module.exports = function createClientConfig (options, cliOptions) {
5151
}])
5252
}
5353

54-
if (!cliOptions.debug) {
54+
if (!ctx.cliOptions.debug) {
5555
config
5656
.plugin('bar')
5757
.use(WebpackBar, [{
@@ -61,11 +61,11 @@ module.exports = function createClientConfig (options, cliOptions) {
6161
}])
6262
}
6363

64-
if (options.siteConfig.chainWebpack) {
65-
options.siteConfig.chainWebpack(config, false /* isServer */)
64+
if (ctx.siteConfig.chainWebpack) {
65+
ctx.siteConfig.chainWebpack(config, false /* isServer */)
6666
}
6767

68-
options.pluginAPI.options.chainWebpack.syncApply(config, false /* isServer */)
68+
ctx.pluginAPI.options.chainWebpack.syncApply(config, false /* isServer */)
6969

7070
return config
7171
}

packages/@vuepress/core/lib/webpack/createServerConfig.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
module.exports = function createServerConfig (options, cliOptions) {
1+
module.exports = function createServerConfig (ctx) {
22
const fs = require('fs')
33
const path = require('path')
44
const WebpackBar = require('webpackbar')
55
const createBaseConfig = require('./createBaseConfig')
66
const VueSSRServerPlugin = require('vue-server-renderer/server-plugin')
77
const CopyPlugin = require('copy-webpack-plugin')
88

9-
const config = createBaseConfig(options, cliOptions, true /* isServer */)
10-
const { sourceDir, outDir } = options
9+
const config = createBaseConfig(ctx, true /* isServer */)
10+
const { sourceDir, outDir } = ctx
1111

1212
config
1313
.target('node')
@@ -40,7 +40,7 @@ module.exports = function createServerConfig (options, cliOptions) {
4040
]])
4141
}
4242

43-
if (!cliOptions.debug) {
43+
if (!ctx.cliOptions.debug) {
4444
config
4545
.plugin('bar')
4646
.use(WebpackBar, [{
@@ -50,11 +50,11 @@ module.exports = function createServerConfig (options, cliOptions) {
5050
}])
5151
}
5252

53-
if (options.siteConfig.chainWebpack) {
54-
options.siteConfig.chainWebpack(config, true /* isServer */)
53+
if (ctx.siteConfig.chainWebpack) {
54+
ctx.siteConfig.chainWebpack(config, true /* isServer */)
5555
}
5656

57-
options.pluginAPI.options.chainWebpack.syncApply(config, true /* isServer */)
57+
ctx.pluginAPI.options.chainWebpack.syncApply(config, true /* isServer */)
5858

5959
return config
6060
}

0 commit comments

Comments
 (0)