From 658ea759610eb1b32e068af2fc58951c285849cc Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Mon, 24 Sep 2018 01:56:40 +0800 Subject: [PATCH] refactor: unify code style. --- packages/@vuepress/core/lib/build.js | 2 + packages/@vuepress/core/lib/dev.js | 2 + packages/@vuepress/core/lib/eject.js | 2 + packages/@vuepress/core/lib/index.js | 2 + .../lib/plugin-api/abstract/AsyncOption.js | 10 +- .../core/lib/plugin-api/abstract/Option.js | 18 ++- .../@vuepress/core/lib/plugin-api/index.js | 20 ++- .../override/EnhanceAppFilesOption.js | 1 - .../@vuepress/core/lib/prepare/AppContext.js | 43 ++++++- packages/@vuepress/core/lib/prepare/I18n.js | 18 +++ packages/@vuepress/core/lib/prepare/Page.js | 114 +++++++++++++----- packages/@vuepress/core/lib/prepare/index.js | 10 ++ .../@vuepress/core/lib/prepare/loadConfig.js | 10 ++ packages/@vuepress/core/lib/util/index.js | 39 ++++++ .../core/lib/webpack/ClientPlugin.js | 2 + .../core/lib/webpack/DevLogPlugin.js | 10 ++ .../@vuepress/core/lib/webpack/HeadPlugin.js | 12 +- .../core/lib/webpack/createBaseConfig.js | 10 ++ .../core/lib/webpack/createClientConfig.js | 6 + .../core/lib/webpack/createServerConfig.js | 6 + .../core/lib/webpack/markdownLoader.js | 10 ++ 21 files changed, 308 insertions(+), 39 deletions(-) diff --git a/packages/@vuepress/core/lib/build.js b/packages/@vuepress/core/lib/build.js index 402e7fec66..5c4fe16929 100644 --- a/packages/@vuepress/core/lib/build.js +++ b/packages/@vuepress/core/lib/build.js @@ -1,3 +1,5 @@ +'use strict' + module.exports = async function build (sourceDir, cliOptions = {}) { process.env.NODE_ENV = 'production' diff --git a/packages/@vuepress/core/lib/dev.js b/packages/@vuepress/core/lib/dev.js index d7606e1331..5383efb893 100644 --- a/packages/@vuepress/core/lib/dev.js +++ b/packages/@vuepress/core/lib/dev.js @@ -1,3 +1,5 @@ +'use strict' + module.exports = async function dev (sourceDir, cliOptions = {}) { const path = require('path') const webpack = require('webpack') diff --git a/packages/@vuepress/core/lib/eject.js b/packages/@vuepress/core/lib/eject.js index f365df354f..f3cfa5df6b 100644 --- a/packages/@vuepress/core/lib/eject.js +++ b/packages/@vuepress/core/lib/eject.js @@ -1,3 +1,5 @@ +'use strict' + const path = require('path') const { chalk, fs, logger } = require('@vuepress/shared-utils') diff --git a/packages/@vuepress/core/lib/index.js b/packages/@vuepress/core/lib/index.js index 77b22e74fd..43a6d0124b 100644 --- a/packages/@vuepress/core/lib/index.js +++ b/packages/@vuepress/core/lib/index.js @@ -1,3 +1,5 @@ +'use strict' + exports.dev = require('./dev') exports.build = require('./build') exports.eject = require('./eject') diff --git a/packages/@vuepress/core/lib/plugin-api/abstract/AsyncOption.js b/packages/@vuepress/core/lib/plugin-api/abstract/AsyncOption.js index c23ed3bdac..2685ef09af 100644 --- a/packages/@vuepress/core/lib/plugin-api/abstract/AsyncOption.js +++ b/packages/@vuepress/core/lib/plugin-api/abstract/AsyncOption.js @@ -8,14 +8,16 @@ const { logger, chalk, datatypes: { isFunction }} = require('@vuepress/shared-ut const Option = require('./Option') /** - * Expose Asynchronous Option. + * Expose asynchronous option class. */ class AsyncOption extends Option { /** * Asynchronous serial running + * * @param args * @param {Array} args + * @api public */ async asyncApply (...args) { @@ -42,8 +44,10 @@ class AsyncOption extends Option { /** * Asynchronous serial running + * * @param args * @param {Array} args + * @api public */ async parallelApply (...args) { @@ -72,8 +76,10 @@ class AsyncOption extends Option { /** * Process a value via a pipeline. + * * @param input - * @returns {*} + * @returns {any} + * @api public */ async pipeline (input) { diff --git a/packages/@vuepress/core/lib/plugin-api/abstract/Option.js b/packages/@vuepress/core/lib/plugin-api/abstract/Option.js index ad215cbd18..6ffd7d48f6 100644 --- a/packages/@vuepress/core/lib/plugin-api/abstract/Option.js +++ b/packages/@vuepress/core/lib/plugin-api/abstract/Option.js @@ -7,7 +7,7 @@ const { logger, chalk, compose, datatypes: { isFunction }} = require('@vuepress/shared-utils') /** - * Expose Option. + * Expose synchronous option class. */ class Option { @@ -18,8 +18,10 @@ class Option { /** * Set value with name. + * * @param {string} name * @param {T} value + * @api public */ add (name, value) { @@ -31,7 +33,9 @@ class Option { /** * Delete value with name. + * * @param {string} name + * @api public */ delete (name) { @@ -44,7 +48,9 @@ class Option { /** * Clean option store + * * @param {string} name + * @api public */ clear (name) { @@ -53,7 +59,9 @@ class Option { /** * Get values. + * * @returns {any} + * @api public */ get values () { @@ -61,8 +69,10 @@ class Option { } /** + * Get applied values * * @returns {Array|*|any[]} + * @api public */ get appliedValues () { @@ -70,8 +80,10 @@ class Option { } /** - * Get values. + * Get entries. + * * @returns {any} + * @api public */ get entries () { @@ -80,7 +92,9 @@ class Option { /** * Synchronous running + * * @param {Array} args + * @api public */ syncApply (...args) { diff --git a/packages/@vuepress/core/lib/plugin-api/index.js b/packages/@vuepress/core/lib/plugin-api/index.js index 40c17bdea0..cc04e2d70a 100644 --- a/packages/@vuepress/core/lib/plugin-api/index.js +++ b/packages/@vuepress/core/lib/plugin-api/index.js @@ -23,12 +23,14 @@ module.exports = class PluginAPI { this.options = {} this._pluginContext = context this._pluginQueue = [] - this._initializeOptions(PLUGIN_OPTION_MAP) + this.initializeOptions(PLUGIN_OPTION_MAP) } /** * Get enabled plugins + * * @returns {array} + * @api public */ get enabledPlugins () { @@ -37,7 +39,9 @@ module.exports = class PluginAPI { /** * Get disabled plugins + * * @returns {array} + * @api public */ get disabledPlugins () { @@ -46,6 +50,8 @@ module.exports = class PluginAPI { /** * apply plugin. + * + * @api public */ apply () { @@ -60,9 +66,11 @@ module.exports = class PluginAPI { /** * Normalize plugin and push it to the plugin queue. + * * @param {object} pluginRaw * @param {object} pluginOptions * @returns {module.PluginAPI} + * @api public */ use (pluginRaw, pluginOptions = {}) { @@ -84,8 +92,10 @@ module.exports = class PluginAPI { /** * Use plugin by config. + * * @param pluginsConfig * @returns {module.PluginAPI} + * @api public */ useByPluginsConfig (pluginsConfig) { @@ -98,9 +108,11 @@ module.exports = class PluginAPI { /** * initialize plugin options. + * + * @api private */ - _initializeOptions () { + initializeOptions () { Object.keys(PLUGIN_OPTION_MAP).forEach(key => { const option = PLUGIN_OPTION_MAP[key] this.options[option.name] = instantiateOption(option.name) @@ -109,10 +121,12 @@ module.exports = class PluginAPI { /** * Register plugin option. + * * @param {string} key * @param {any} value * @param {string} pluginName * @returns {module.PluginAPI} + * @api private */ registerOption (key, value, pluginName) { @@ -132,6 +146,8 @@ module.exports = class PluginAPI { /** * apply plugin. + * + * @api private */ applyPlugin ({ diff --git a/packages/@vuepress/core/lib/plugin-api/override/EnhanceAppFilesOption.js b/packages/@vuepress/core/lib/plugin-api/override/EnhanceAppFilesOption.js index b96ab515bb..9e5b3f5ac5 100644 --- a/packages/@vuepress/core/lib/plugin-api/override/EnhanceAppFilesOption.js +++ b/packages/@vuepress/core/lib/plugin-api/override/EnhanceAppFilesOption.js @@ -1,4 +1,3 @@ - 'use strict' /** diff --git a/packages/@vuepress/core/lib/prepare/AppContext.js b/packages/@vuepress/core/lib/prepare/AppContext.js index 89d17a0034..fc00e69d10 100644 --- a/packages/@vuepress/core/lib/prepare/AppContext.js +++ b/packages/@vuepress/core/lib/prepare/AppContext.js @@ -1,3 +1,9 @@ +'use strict' + +/** + * Module dependencies. + */ + const path = require('path') const createMarkdown = require('../markdown/index') const loadConfig = require('./loadConfig') @@ -8,10 +14,15 @@ const Page = require('./Page') const I18n = require('./I18n') const PluginAPI = require('../plugin-api/index') +/** + * Expose AppContext. + */ + module.exports = class AppContext { /** * Instantiate the app context with a new API - * @param { string } sourceDir + * + * @param {string} sourceDir * @param {{ * isProd: boolean, * plugins: pluginsConfig, @@ -19,6 +30,7 @@ module.exports = class AppContext { * temp: string * }} options */ + constructor (sourceDir, cliOptions = {}, isProd) { this.sourceDir = sourceDir this.cliOptions = cliOptions @@ -47,8 +59,11 @@ module.exports = class AppContext { /** * Load pages, load plugins, apply plugins / plugin options, etc. + * * @returns {Promise} + * @api private */ + async process () { this.normalizeHeadTagUrls() this.markdown = createMarkdown(this.siteConfig) @@ -72,7 +87,10 @@ module.exports = class AppContext { /** * Apply internal and user plugins + * + * @api private */ + resolvePlugins () { const themeConfig = this.themeConfig const siteConfig = this.siteConfig @@ -111,7 +129,10 @@ module.exports = class AppContext { /** * normalize head tag urls for base + * + * @api private */ + normalizeHeadTagUrls () { if (this.base !== '/' && this.siteConfig.head) { this.siteConfig.head.forEach(tag => { @@ -132,7 +153,10 @@ module.exports = class AppContext { /** * Make template configurable + * + * @api private */ + resolveTemplates () { let { ssrTemplate, devTemplate } = this.siteConfig const templateDir = path.resolve(this.vuepressDir, 'templates') @@ -156,8 +180,11 @@ module.exports = class AppContext { /** * Find all page source files located in sourceDir + * * @returns {Promise} + * @api private */ + async resolvePages () { // resolve pageFiles const patterns = ['**/*.md', '!.vuepress', '!node_modules'] @@ -179,8 +206,11 @@ module.exports = class AppContext { /** * Add a page - * @returns { Promise } + * + * @returns {Promise} + * @api public */ + async addPage (options) { options.permalinkPattern = this.siteConfig.permalink const page = new Page(options) @@ -194,8 +224,11 @@ module.exports = class AppContext { /** * Resolve theme - * @returns { Promise } + * + * @returns {Promise} + * @api private */ + async resolveTheme () { const theme = this.siteConfig.theme || this.cliOptions.theme Object.assign(this, (await loadTheme(theme, this.sourceDir, this.vuepressDir))) @@ -203,6 +236,7 @@ module.exports = class AppContext { /** * Get the data to be delivered to the client. + * * @returns {{ * title: string, * description: string, @@ -211,7 +245,9 @@ module.exports = class AppContext { * themeConfig: ThemeConfig, * locales: Locales * }} + * @api public */ + getSiteData () { return { title: this.siteConfig.title || '', @@ -233,6 +269,7 @@ module.exports = class AppContext { * tempPath: string * }} */ + function createTemp (tempPath) { if (!tempPath) { tempPath = path.resolve(__dirname, '../../.temp') diff --git a/packages/@vuepress/core/lib/prepare/I18n.js b/packages/@vuepress/core/lib/prepare/I18n.js index 3606337897..8176c592b8 100644 --- a/packages/@vuepress/core/lib/prepare/I18n.js +++ b/packages/@vuepress/core/lib/prepare/I18n.js @@ -1,3 +1,13 @@ +'use strict' + +/** + * Get page data via path (permalink). + * + * @param {array} pages + * @param {string} path + * @returns {object} + */ + function findPageForPath (pages, path) { for (let i = 0; i < pages.length; i++) { const page = pages[i] @@ -11,6 +21,14 @@ function findPageForPath (pages, path) { } } +/** + * Expose I18n constructor.Note that this file will + * be run in both server and client side. + * + * @param store + * @returns {{new(*): I18n, prototype: I18n}} + */ + module.exports = (store /* null in server side */) => class I18n { constructor (dataProvider) { this.__ssrContext = true diff --git a/packages/@vuepress/core/lib/prepare/Page.js b/packages/@vuepress/core/lib/prepare/Page.js index 40fae91041..5f30d0de24 100644 --- a/packages/@vuepress/core/lib/prepare/Page.js +++ b/packages/@vuepress/core/lib/prepare/Page.js @@ -1,17 +1,28 @@ +'use strict' + +/** + * Module dependencies. + */ + const path = require('path') const slugify = require('../markdown/slugify') const { inferTitle, extractHeaders } = require('../util/index') const { fs, fileToPath, parseFrontmatter, getPermalink } = require('@vuepress/shared-utils') +/** + * Expose Page class. + */ + module.exports = class Page { /** - * @param { string } title markdown title - * @param { string } content markdown file content - * @param { string } filePath absolute file path of source markdown file. - * @param { string } relative relative file path of source markdown file. - * @param { string } permalink the URL (excluding the domain name) for your pages, posts. - * @param { object } frontmatter - * @param { string } permalinkPattern + * @param {string} path the URL (excluding the domain name) for your page/post. + * @param {string} title markdown title + * @param {string} content markdown file content + * @param {string} filePath absolute file path of source markdown file. + * @param {string} relative relative file path of source markdown file. + * @param {string} permalink same to path, the URL (excluding the domain name) for your page/post. + * @param {object} frontmatter + * @param {string} permalinkPattern */ constructor ({ path, @@ -44,6 +55,16 @@ module.exports = class Page { this._permalinkPattern = permalinkPattern } + /** + * Process a page. + * + * 1. If it's a page pointing to a md file, this method will try + * to resolve the page's title / headers from the content. + * 2. If it's a pure route. this method will only enhance it. + * + * @api public + */ + async process ({ i18n, markdown, @@ -85,10 +106,57 @@ module.exports = class Page { this._i18n = i18n this._localePath = i18n.$localePath - this._enhance(enhancers) + this.enhance(enhancers) this.buildPermalink() } + /** + * file name of page's source markdown file, or the last cut of regularPath. + * + * @returns {string} + * @api public + */ + + get filename () { + return path.parse(this._filePath || this.regularPath).name + } + + /** + * slugified file name. + * + * @returns {string} + * @api public + */ + + get slug () { + return slugify(this.filename) + } + + /** + * Convert page's metadata to JSON, note that all fields beginning + * with an underscore will not be serialized. + * + * @returns {object} + * @api public + */ + + toJson () { + const json = {} + Object.keys(this).reduce((json, key) => { + if (!key.startsWith('_')) { + json[key] = this[key] + } + return json + }, json) + return json + } + + /** + * Build permalink via permalink pattern and page's metadata. + * + * @api private + */ + buildPermalink () { if (!this._permalink) { this._permalink = getPermalink({ @@ -105,7 +173,16 @@ module.exports = class Page { } } - _enhance (enhancers) { + /** + * Execute the page enhancers. A enhancer could do following things: + * + * 1. Modify page's frontmetter. + * 2. Add extra field to the page. + * + * @api private + */ + + enhance (enhancers) { for (const { name: pluginName, value: enhancer } of enhancers) { try { enhancer(this) @@ -115,23 +192,4 @@ module.exports = class Page { } } } - - get filename () { - return path.parse(this._filePath || this.regularPath).name - } - - get slug () { - return slugify(this.filename) - } - - toJson () { - const json = {} - Object.keys(this).reduce((json, key) => { - if (!key.startsWith('_')) { - json[key] = this[key] - } - return json - }, json) - return json - } } diff --git a/packages/@vuepress/core/lib/prepare/index.js b/packages/@vuepress/core/lib/prepare/index.js index 73bfa46905..06bd77ed83 100644 --- a/packages/@vuepress/core/lib/prepare/index.js +++ b/packages/@vuepress/core/lib/prepare/index.js @@ -1,5 +1,15 @@ +'use strict' + +/** + * Module dependencies. + */ + const AppContext = require('./AppContext') +/** + * Expose prepare. + */ + module.exports = async function prepare (sourceDir, cliOptions, isProd) { const appContext = new AppContext(sourceDir, cliOptions, isProd) await appContext.process() diff --git a/packages/@vuepress/core/lib/prepare/loadConfig.js b/packages/@vuepress/core/lib/prepare/loadConfig.js index 949e4c6efa..a6f264bf7e 100644 --- a/packages/@vuepress/core/lib/prepare/loadConfig.js +++ b/packages/@vuepress/core/lib/prepare/loadConfig.js @@ -1,8 +1,18 @@ +'use strict' + +/** + * Module dependencies. + */ + const { fs } = require('@vuepress/shared-utils') const path = require('path') const yamlParser = require('js-yaml') const tomlParser = require('toml') +/** + * Expose loadConfig. + */ + module.exports = function loadConfig (vuepressDir, bustCache = true) { const configPath = path.resolve(vuepressDir, 'config.js') const configYmlPath = path.resolve(vuepressDir, 'config.yml') diff --git a/packages/@vuepress/core/lib/util/index.js b/packages/@vuepress/core/lib/util/index.js index b57c36003d..4d682728f9 100644 --- a/packages/@vuepress/core/lib/util/index.js +++ b/packages/@vuepress/core/lib/util/index.js @@ -1,5 +1,18 @@ +'use strict' + +/** + * Module dependencies. + */ + const { deeplyParseHeaders } = require('@vuepress/shared-utils') +/** + * Normalize head tag config. + * + * @param {string|array} tag + * @returns {object} + */ + exports.normalizeHeadTag = function (tag) { if (typeof tag === 'string') { tag = [tag] @@ -13,6 +26,15 @@ exports.normalizeHeadTag = function (tag) { } } +/** + * Use webpack-merge to merge user's config into default config. + * + * @param {object} userConfig + * @param {object} config + * @param {boolean} isServer + * @returns {object} + */ + exports.applyUserWebpackConfig = function (userConfig, config, isServer) { const merge = require('webpack-merge') if (typeof userConfig === 'object') { @@ -27,6 +49,14 @@ exports.applyUserWebpackConfig = function (userConfig, config, isServer) { return config } +/** + * Infer a page's title via frontmatter and content. + * + * @param frontmatter + * @param strippedContent + * @returns {*} + */ + exports.inferTitle = function (frontmatter, strippedContent) { if (frontmatter.home) { return 'Home' @@ -43,6 +73,15 @@ exports.inferTitle = function (frontmatter, strippedContent) { const LRU = require('lru-cache') const cache = LRU({ max: 1000 }) +/** + * Extract heeaders from markdown source content. + * + * @param {string} content + * @param {array} include + * @param {object} md + * @returns {array} + */ + exports.extractHeaders = function (content, include = [], md) { const key = content + include.join(',') const hit = cache.get(key) diff --git a/packages/@vuepress/core/lib/webpack/ClientPlugin.js b/packages/@vuepress/core/lib/webpack/ClientPlugin.js index 82028475bb..80aeb8927d 100644 --- a/packages/@vuepress/core/lib/webpack/ClientPlugin.js +++ b/packages/@vuepress/core/lib/webpack/ClientPlugin.js @@ -1,3 +1,5 @@ +'use strict' + // Temporarily hacked dev build var isJS = function (file) { return /\.js(\?[^.]+)?$/.test(file) } diff --git a/packages/@vuepress/core/lib/webpack/DevLogPlugin.js b/packages/@vuepress/core/lib/webpack/DevLogPlugin.js index 79dd5e0086..254f0bd4dd 100644 --- a/packages/@vuepress/core/lib/webpack/DevLogPlugin.js +++ b/packages/@vuepress/core/lib/webpack/DevLogPlugin.js @@ -1,5 +1,15 @@ +'use strict' + +/** + * Module dependencies. + */ + const { chalk, logger } = require('@vuepress/shared-utils') +/** + * Expose DevLogPlugin class. + */ + module.exports = class DevLogPlugin { constructor (options) { this.options = options diff --git a/packages/@vuepress/core/lib/webpack/HeadPlugin.js b/packages/@vuepress/core/lib/webpack/HeadPlugin.js index b8a96845f7..828c2f2212 100644 --- a/packages/@vuepress/core/lib/webpack/HeadPlugin.js +++ b/packages/@vuepress/core/lib/webpack/HeadPlugin.js @@ -1,6 +1,16 @@ +'use strict' + +/** + * Module dependencies. + */ + const { normalizeHeadTag } = require('../util/index') -module.exports = class SiteDataPlugin { +/** + * Expose HeadPlugin class. + */ + +module.exports = class HeadPlugin { constructor ({ tags }) { this.tags = tags } diff --git a/packages/@vuepress/core/lib/webpack/createBaseConfig.js b/packages/@vuepress/core/lib/webpack/createBaseConfig.js index 0ae51ae589..c153646661 100644 --- a/packages/@vuepress/core/lib/webpack/createBaseConfig.js +++ b/packages/@vuepress/core/lib/webpack/createBaseConfig.js @@ -1,6 +1,16 @@ +'use strict' + +/** + * Module dependencies. + */ + const path = require('path') const { fs, logger, chalk } = require('@vuepress/shared-utils') +/** + * Expose createBaseConfig method. + */ + module.exports = function createBaseConfig ({ siteConfig, sourceDir, diff --git a/packages/@vuepress/core/lib/webpack/createClientConfig.js b/packages/@vuepress/core/lib/webpack/createClientConfig.js index 592d29df34..c818c29310 100644 --- a/packages/@vuepress/core/lib/webpack/createClientConfig.js +++ b/packages/@vuepress/core/lib/webpack/createClientConfig.js @@ -1,3 +1,9 @@ +'use strict' + +/** + * Expose createClientConfig method. + */ + module.exports = function createClientConfig (ctx) { const path = require('path') const WebpackBar = require('webpackbar') diff --git a/packages/@vuepress/core/lib/webpack/createServerConfig.js b/packages/@vuepress/core/lib/webpack/createServerConfig.js index 764923d5ac..b8592a98fa 100644 --- a/packages/@vuepress/core/lib/webpack/createServerConfig.js +++ b/packages/@vuepress/core/lib/webpack/createServerConfig.js @@ -1,3 +1,9 @@ +'use strict' + +/** + * Expose createServerConfig method. + */ + module.exports = function createServerConfig (ctx) { const fs = require('fs') const path = require('path') diff --git a/packages/@vuepress/core/lib/webpack/markdownLoader.js b/packages/@vuepress/core/lib/webpack/markdownLoader.js index 126c16d826..1f4e4d84dd 100644 --- a/packages/@vuepress/core/lib/webpack/markdownLoader.js +++ b/packages/@vuepress/core/lib/webpack/markdownLoader.js @@ -1,3 +1,9 @@ +'use strict' + +/** + * Module dependencies. + */ + const fs = require('fs') const path = require('path') const hash = require('hash-sum') @@ -10,6 +16,10 @@ const LRU = require('lru-cache') const cache = LRU({ max: 1000 }) const devCache = LRU({ max: 1000 }) +/** + * Expose markdown loader. + */ + module.exports = function (src) { const isProd = process.env.NODE_ENV === 'production' const isServer = this.target === 'node'