Skip to content

Commit

Permalink
feat: cache option (boolean | absolute path | relative path)
Browse files Browse the repository at this point in the history
It also close #993 via including siteConfig.extendMarkdown & siteConfig.markdown.extendMarkdown to cacheIdentifier.
  • Loading branch information
ulivz committed Nov 13, 2018
1 parent bfd4d02 commit 3871f4a
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 21 deletions.
10 changes: 10 additions & 0 deletions packages/@vuepress/core/lib/prepare/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const createMarkdown = require('./createMarkdown')
const loadConfig = require('./loadConfig')
const loadTheme = require('./loadTheme')
const { getCacheLoaderOptions } = require('./CacheLoader')
const {
fs, path, logger, chalk, globby, sort,
datatypes: { isFunction },
Expand Down Expand Up @@ -73,6 +74,7 @@ module.exports = class AppContext {
*/

async process () {
this.resolveCacheLoaderOptions()
this.normalizeHeadTagUrls()
await this.resolveTheme()
this.resolveTemplates()
Expand Down Expand Up @@ -174,6 +176,14 @@ module.exports = class AppContext {
}
}

/**
* Resolve options of cache loader.
*/

resolveCacheLoaderOptions () {
Object.assign(this, (getCacheLoaderOptions(this.siteConfig, this.cliOptions, this.cwd, this.isProd)))
}

/**
* Make template configurable
*
Expand Down
61 changes: 61 additions & 0 deletions packages/@vuepress/core/lib/prepare/CacheLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'use strict'

/**
* Module dependencies.
*/

const {
path, toAbsolutePath, chalk, logger,
datatypes: { isString, isBoolean }
} = require('@vuepress/shared-utils')

/**
* Get cache directory and cache identifier via config.
* @param {object} siteConfig
* @param {object} cliOptions
*/

exports.getCacheLoaderOptions = function (siteConfig, cliOptions, cwd, isProd) {
const defaultCacheDirectory = path.resolve(__dirname, '../../node_modules/.cache/vuepress')
let cache = cliOptions.cache || siteConfig.cache || defaultCacheDirectory

if (isBoolean(cache)) {
if (cache === true) {
cache = defaultCacheDirectory
}
} else if (!isString(cache)) {
throw new Error(`expected cache option to be string or boolean, but got ${typeof cache}`)
}

const cacheDirectory = toAbsolutePath(cache, cwd)
const cacheIdentifier = JSON.stringify({
vuepress: require('../../package.json').version,
'cache-loader': require('cache-loader/package.json').version,
'vue-loader': require('cache-loader/package.json').version,
isProd,
config: (
(
siteConfig.markdown
? JSON.stringify(siteConfig.markdown)
: ''
) +
(
siteConfig.markdown && siteConfig.markdown.extendMarkdown
? siteConfig.markdown.extendMarkdown.toString()
: ''
) +
(
siteConfig.extendMarkdown
? siteConfig.extendMarkdown.toString()
: ''
) +
(siteConfig.chainWebpack || '').toString() +
(siteConfig.configureWebpack || '').toString()
)
})

logger.debug('\nCache directory: ' + chalk.gray(cacheDirectory))
logger.debug('\nCache identifier : ' + chalk.gray(cacheIdentifier))

return { cacheDirectory, cacheIdentifier }
}
27 changes: 6 additions & 21 deletions packages/@vuepress/core/lib/webpack/createBaseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Module dependencies.
*/

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

/**
* Expose createBaseConfig method.
Expand All @@ -18,6 +18,8 @@ module.exports = function createBaseConfig ({
themePath,
markdown,
tempPath,
cacheDirectory,
cacheIdentifier,
cliOptions: {
debug,
cache
Expand Down Expand Up @@ -73,29 +75,12 @@ module.exports = function createBaseConfig ({
config.module
.noParse(/^(vue|vue-router|vuex|vuex-router-sync)$/)

let cacheDirectory
if (cache && typeof cache === 'string') {
cacheDirectory = path.resolve(cache)
} else {
cacheDirectory = path.resolve(__dirname, '../../node_modules/.cache/vuepress')
}
logger.debug('\nCache directory: ' + chalk.gray(cacheDirectory))
if (!cache) {
if (cache === false) {
logger.tip('\nClean cache...\n')
fs.emptyDirSync(cacheDirectory)
}
const cacheIdentifier = JSON.stringify({
vuepress: require('../../package.json').version,
'cache-loader': require('cache-loader/package.json').version,
'vue-loader': require('cache-loader/package.json').version,
isProd,
isServer,
config: (
(siteConfig.markdown ? JSON.stringify(siteConfig.markdown) : '') +
(siteConfig.chainWebpack || '').toString() +
(siteConfig.configureWebpack || '').toString()
)
})

cacheIdentifier += `isServer:${isServer}`

function applyVuePipeline (rule) {
rule
Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/shared-utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ exports.hash = require('hash-sum')
exports.fallback = require('./lib/fallback')
exports.slugify = require('./lib/slugify')
exports.tryChain = require('./lib/tryChain')
exports.toAbsolutePath = require('./lib/toAbsolutePath')
18 changes: 18 additions & 0 deletions packages/@vuepress/shared-utils/lib/toAbsolutePath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

/**
* Module dependencies.
*/

const path = require('upath')

/**
* Normalize path request to absolute path.
*/

module.exports = function toAbsolutePath (raw, cwd = process.cwd()) {
if (path.isAbsolute(raw)) {
return raw
}
return path.resolve(cwd, raw)
}

0 comments on commit 3871f4a

Please sign in to comment.