From 3871f4a1a3078ab6bcf0afeb6077eb1532fae9d9 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Tue, 13 Nov 2018 23:45:26 +0800 Subject: [PATCH] feat: cache option (boolean | absolute path | relative path) It also close #993 via including siteConfig.extendMarkdown & siteConfig.markdown.extendMarkdown to cacheIdentifier. --- .../@vuepress/core/lib/prepare/AppContext.js | 10 +++ .../@vuepress/core/lib/prepare/CacheLoader.js | 61 +++++++++++++++++++ .../core/lib/webpack/createBaseConfig.js | 27 ++------ packages/@vuepress/shared-utils/index.js | 1 + .../shared-utils/lib/toAbsolutePath.js | 18 ++++++ 5 files changed, 96 insertions(+), 21 deletions(-) create mode 100644 packages/@vuepress/core/lib/prepare/CacheLoader.js create mode 100644 packages/@vuepress/shared-utils/lib/toAbsolutePath.js diff --git a/packages/@vuepress/core/lib/prepare/AppContext.js b/packages/@vuepress/core/lib/prepare/AppContext.js index eda8085770..67dbd89c69 100644 --- a/packages/@vuepress/core/lib/prepare/AppContext.js +++ b/packages/@vuepress/core/lib/prepare/AppContext.js @@ -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 }, @@ -73,6 +74,7 @@ module.exports = class AppContext { */ async process () { + this.resolveCacheLoaderOptions() this.normalizeHeadTagUrls() await this.resolveTheme() this.resolveTemplates() @@ -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 * diff --git a/packages/@vuepress/core/lib/prepare/CacheLoader.js b/packages/@vuepress/core/lib/prepare/CacheLoader.js new file mode 100644 index 0000000000..a3c01825ff --- /dev/null +++ b/packages/@vuepress/core/lib/prepare/CacheLoader.js @@ -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 } +} diff --git a/packages/@vuepress/core/lib/webpack/createBaseConfig.js b/packages/@vuepress/core/lib/webpack/createBaseConfig.js index c5fe6a301c..564288f69a 100644 --- a/packages/@vuepress/core/lib/webpack/createBaseConfig.js +++ b/packages/@vuepress/core/lib/webpack/createBaseConfig.js @@ -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. @@ -18,6 +18,8 @@ module.exports = function createBaseConfig ({ themePath, markdown, tempPath, + cacheDirectory, + cacheIdentifier, cliOptions: { debug, cache @@ -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 diff --git a/packages/@vuepress/shared-utils/index.js b/packages/@vuepress/shared-utils/index.js index fcc1bf3f45..49c2b134bc 100644 --- a/packages/@vuepress/shared-utils/index.js +++ b/packages/@vuepress/shared-utils/index.js @@ -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') diff --git a/packages/@vuepress/shared-utils/lib/toAbsolutePath.js b/packages/@vuepress/shared-utils/lib/toAbsolutePath.js new file mode 100644 index 0000000000..8da5712bb0 --- /dev/null +++ b/packages/@vuepress/shared-utils/lib/toAbsolutePath.js @@ -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) +}