Skip to content

Commit 3871f4a

Browse files
committed
feat: cache option (boolean | absolute path | relative path)
It also close #993 via including siteConfig.extendMarkdown & siteConfig.markdown.extendMarkdown to cacheIdentifier.
1 parent bfd4d02 commit 3871f4a

File tree

5 files changed

+96
-21
lines changed

5 files changed

+96
-21
lines changed

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

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
const createMarkdown = require('./createMarkdown')
88
const loadConfig = require('./loadConfig')
99
const loadTheme = require('./loadTheme')
10+
const { getCacheLoaderOptions } = require('./CacheLoader')
1011
const {
1112
fs, path, logger, chalk, globby, sort,
1213
datatypes: { isFunction },
@@ -73,6 +74,7 @@ module.exports = class AppContext {
7374
*/
7475

7576
async process () {
77+
this.resolveCacheLoaderOptions()
7678
this.normalizeHeadTagUrls()
7779
await this.resolveTheme()
7880
this.resolveTemplates()
@@ -174,6 +176,14 @@ module.exports = class AppContext {
174176
}
175177
}
176178

179+
/**
180+
* Resolve options of cache loader.
181+
*/
182+
183+
resolveCacheLoaderOptions () {
184+
Object.assign(this, (getCacheLoaderOptions(this.siteConfig, this.cliOptions, this.cwd, this.isProd)))
185+
}
186+
177187
/**
178188
* Make template configurable
179189
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict'
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const {
8+
path, toAbsolutePath, chalk, logger,
9+
datatypes: { isString, isBoolean }
10+
} = require('@vuepress/shared-utils')
11+
12+
/**
13+
* Get cache directory and cache identifier via config.
14+
* @param {object} siteConfig
15+
* @param {object} cliOptions
16+
*/
17+
18+
exports.getCacheLoaderOptions = function (siteConfig, cliOptions, cwd, isProd) {
19+
const defaultCacheDirectory = path.resolve(__dirname, '../../node_modules/.cache/vuepress')
20+
let cache = cliOptions.cache || siteConfig.cache || defaultCacheDirectory
21+
22+
if (isBoolean(cache)) {
23+
if (cache === true) {
24+
cache = defaultCacheDirectory
25+
}
26+
} else if (!isString(cache)) {
27+
throw new Error(`expected cache option to be string or boolean, but got ${typeof cache}`)
28+
}
29+
30+
const cacheDirectory = toAbsolutePath(cache, cwd)
31+
const cacheIdentifier = JSON.stringify({
32+
vuepress: require('../../package.json').version,
33+
'cache-loader': require('cache-loader/package.json').version,
34+
'vue-loader': require('cache-loader/package.json').version,
35+
isProd,
36+
config: (
37+
(
38+
siteConfig.markdown
39+
? JSON.stringify(siteConfig.markdown)
40+
: ''
41+
) +
42+
(
43+
siteConfig.markdown && siteConfig.markdown.extendMarkdown
44+
? siteConfig.markdown.extendMarkdown.toString()
45+
: ''
46+
) +
47+
(
48+
siteConfig.extendMarkdown
49+
? siteConfig.extendMarkdown.toString()
50+
: ''
51+
) +
52+
(siteConfig.chainWebpack || '').toString() +
53+
(siteConfig.configureWebpack || '').toString()
54+
)
55+
})
56+
57+
logger.debug('\nCache directory: ' + chalk.gray(cacheDirectory))
58+
logger.debug('\nCache identifier : ' + chalk.gray(cacheIdentifier))
59+
60+
return { cacheDirectory, cacheIdentifier }
61+
}

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

+6-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Module dependencies.
55
*/
66

7-
const { fs, path, logger, chalk } = require('@vuepress/shared-utils')
7+
const { fs, path, logger } = require('@vuepress/shared-utils')
88

99
/**
1010
* Expose createBaseConfig method.
@@ -18,6 +18,8 @@ module.exports = function createBaseConfig ({
1818
themePath,
1919
markdown,
2020
tempPath,
21+
cacheDirectory,
22+
cacheIdentifier,
2123
cliOptions: {
2224
debug,
2325
cache
@@ -73,29 +75,12 @@ module.exports = function createBaseConfig ({
7375
config.module
7476
.noParse(/^(vue|vue-router|vuex|vuex-router-sync)$/)
7577

76-
let cacheDirectory
77-
if (cache && typeof cache === 'string') {
78-
cacheDirectory = path.resolve(cache)
79-
} else {
80-
cacheDirectory = path.resolve(__dirname, '../../node_modules/.cache/vuepress')
81-
}
82-
logger.debug('\nCache directory: ' + chalk.gray(cacheDirectory))
83-
if (!cache) {
78+
if (cache === false) {
8479
logger.tip('\nClean cache...\n')
8580
fs.emptyDirSync(cacheDirectory)
8681
}
87-
const cacheIdentifier = JSON.stringify({
88-
vuepress: require('../../package.json').version,
89-
'cache-loader': require('cache-loader/package.json').version,
90-
'vue-loader': require('cache-loader/package.json').version,
91-
isProd,
92-
isServer,
93-
config: (
94-
(siteConfig.markdown ? JSON.stringify(siteConfig.markdown) : '') +
95-
(siteConfig.chainWebpack || '').toString() +
96-
(siteConfig.configureWebpack || '').toString()
97-
)
98-
})
82+
83+
cacheIdentifier += `isServer:${isServer}`
9984

10085
function applyVuePipeline (rule) {
10186
rule

packages/@vuepress/shared-utils/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@ exports.hash = require('hash-sum')
3333
exports.fallback = require('./lib/fallback')
3434
exports.slugify = require('./lib/slugify')
3535
exports.tryChain = require('./lib/tryChain')
36+
exports.toAbsolutePath = require('./lib/toAbsolutePath')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
const path = require('upath')
8+
9+
/**
10+
* Normalize path request to absolute path.
11+
*/
12+
13+
module.exports = function toAbsolutePath (raw, cwd = process.cwd()) {
14+
if (path.isAbsolute(raw)) {
15+
return raw
16+
}
17+
return path.resolve(cwd, raw)
18+
}

0 commit comments

Comments
 (0)