plugin.ts 820 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import { isFunction } from '@vue/shared'
import { UniCompiler } from './compiler'

export interface UniPluginConfig {
  init?: () => void
  define?: () => Record<string, string>
  inject?: () => Record<string, string | [string, string]>
  done?: () => void
}
export class UniPlugin {
  name: string
  private options: UniPluginConfig
  constructor(name: string, config: UniPluginConfig = {}) {
    this.name = name
    this.options = config
  }
  apply(compiler: UniCompiler) {
    const {
      name,
      options: { init, define, inject, done },
    } = this
    isFunction(init) && compiler.hooks.init.tap(name, init)
    isFunction(define) && compiler.hooks.define.tap(name, define)
    isFunction(inject) && compiler.hooks.inject.tap(name, inject)
    isFunction(done) && compiler.hooks.done.tap(name, done)
  }
}