index.js 3.3 KB
Newer Older
1
const chalk = require('chalk')
U
ULIVZ 已提交
2 3
const Hook = require('./core/Hook')
const instantiateAPI = require('./option/instantiateOption')
4
const logger = require('../util/logger')
U
ULIVZ 已提交
5 6 7
const { resolvePlugin, inferPluginName } = require('./util')
const { assertTypes } = require('../util/shared')
const { HOOK, OPTION } = require('./constans')
U
ULIVZ 已提交
8

U
ULIVZ 已提交
9
module.exports = class Plugin {
10
  constructor (pluginConfigs, context) {
11 12 13
    if (!Array.isArray(pluginConfigs)) {
      pluginConfigs = []
    }
U
ULIVZ 已提交
14
    this.hooks = {}
U
ULIVZ 已提交
15 16 17
    this.options = {}
    this.extendHooks(Object.values(HOOK))
    this.extendOptions(Object.values(OPTION))
18
    this.resolvePluginConfigs(pluginConfigs, context)
U
ULIVZ 已提交
19 20 21 22 23 24 25 26
  }

  extendHooks (hooks) {
    hooks.forEach(hook => {
      this.hooks[hook] = new Hook(hook)
    })
  }

U
ULIVZ 已提交
27 28 29
  extendOptions (options) {
    options.forEach(api => {
      this.options[api] = instantiateAPI(api)
U
ULIVZ 已提交
30 31 32
    })
  }

33 34 35 36 37 38 39
  registerHook (name, hook, pluginName, types) {
    const { valid, warnMsg } = assertTypes(hook, types)
    if (valid) {
      this.hooks[name].tap(pluginName, hook)
    } else if (hook !== undefined) {
      logger.warn(
        `${chalk.gray(`[vuepress-plugin-${pluginName}]`)} ` +
U
ULIVZ 已提交
40
        `Invalid value for "hook" ${chalk.cyan(name)}: ${warnMsg}`
41
      )
U
ULIVZ 已提交
42 43 44 45
    }
    return this
  }

U
ULIVZ 已提交
46
  registerOption (name, api, pluginName, types) {
47 48
    const { valid, warnMsg } = assertTypes(api, types)
    if (valid) {
U
ULIVZ 已提交
49
      this.options[name].tap(pluginName, api)
50 51 52 53 54
    } else if (api !== undefined) {
      logger.warn(
        `${chalk.gray(`[vuepress-plugin-${pluginName}]`)} ` +
        `Invalid value for "option" ${chalk.cyan(name)}: ${warnMsg}`
      )
U
ULIVZ 已提交
55 56 57 58
    }
    return this
  }

59
  resolvePluginConfigs (pluginConfigs, context) {
60 61 62 63
    pluginConfigs.forEach(pluginConfigs => {
      pluginConfigs = Array.isArray(pluginConfigs)
        ? pluginConfigs
        : [pluginConfigs]
64 65
      const [pluginRaw, pluginOptions] = pluginConfigs
      let plugin = resolvePlugin(pluginRaw)
U
ULIVZ 已提交
66
      if (typeof plugin === 'function') {
67
        plugin = plugin(pluginOptions, context)
U
ULIVZ 已提交
68
      }
69
      plugin = Object.assign(plugin, { name: inferPluginName(pluginRaw, plugin) })
U
ULIVZ 已提交
70
      this.applyPlugin(plugin)
U
ULIVZ 已提交
71 72 73
    })
  }

U
ULIVZ 已提交
74 75 76 77 78 79 80 81
  // async callOption (name, args) {
  //   await this.options[name].run(args)
  // }
  //
  // async callHook (name, args) {
  //   await this.hooks[name].run(args)
  // }

U
ULIVZ 已提交
82
  applyPlugin ({
U
ULIVZ 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95
    name,
    client,
    chainWebpack,
    enhanceDevServer,
    extendMarkdown,
    enhanceAppFiles,
    outFiles,
    extendPageData,
    ready,
    compiled,
    updated,
    generated
  }) {
U
ULIVZ 已提交
96
    logger.tip(`\nApply plugin ${chalk.cyan(name)}...`)
U
ULIVZ 已提交
97

U
ULIVZ 已提交
98
    this
U
ULIVZ 已提交
99 100 101 102
      .registerHook(HOOK.READY, ready, name, [Function])
      .registerHook(HOOK.COMPILED, compiled, name, [Function])
      .registerHook(HOOK.UPDATED, updated, name, [Function])
      .registerHook(HOOK.GENERATED, generated, name, [Function])
U
ULIVZ 已提交
103 104

    this
U
ULIVZ 已提交
105 106 107 108 109 110 111
      .registerOption(OPTION.CLIENT, client, name, [String])
      .registerOption(OPTION.CHAIN_WEBPACK, chainWebpack, name, [Function])
      .registerOption(OPTION.ENHANCE_DEV_SERVER, enhanceDevServer, name, [Function])
      .registerOption(OPTION.EXTEND_MARKDOWN, extendMarkdown, name, [Function])
      .registerOption(OPTION.EXTEND_PAGE_DATA, extendPageData, name, [Function])
      .registerOption(OPTION.ENHANCE_APP_FILES, enhanceAppFiles, name, [Array, Function])
      .registerOption(OPTION.OUT_FILES, outFiles, name, [Object])
U
ULIVZ 已提交
112 113
  }
}