@esydoc/plugin-codegen
v2.1.3
Published
> TODO: description
Downloads
218
Readme
@esydoc/plugin-codegen
A jsdoc plugin for schedule resolver to codeGen.
CodeGenScheduler
Hooks
学习 CodeGenScheduler Hooks 知识时,建议先了解下 tapable 的 hooks 有什么类型和作用。
import { AsyncParallelHook, SyncHook, SyncWaterfallHook } from 'tapable'
class CodeGenScheduler {
hooks = {
schedule: new SyncHook<ScheduledData>(['resolverData']), // 调度时触发
resolve: new SyncWaterfallHook<ApiSource>(['apiSource']), // 解析 api 信息触发
emit: new AsyncParallelHook<[SourceUpdateRsp[]]>(['results']), // emit 时触发
collect: new SyncWaterfallHook<ConfigCollect>(['apiConfig']), // 收集配置时触发
}
}
// 数据结构
export type ApiSource = {
path: string,
name: string,
parent: string | null,
filename: string
configPath?: string
}
export type ApiInfo<T> = {
apiConfig: T | null
source: ApiSource
}
export type SourceUpdateRsp = {
key: string
status: 'success' | 'fail'
msg?: string
}
type ScheduledData<T = any> = {
doclet: any
ast: {
args?: TransformedDataTreeNode
ret?: TransformedDataTreeNode
} | null
config: ApiInfo<T>
}
type ConfigCollect = {
update: boolean
apiConfig: any
}
有上述提供的 hooks ,就可以开发相应的 resolver 和 scheduler plugin 啦~
resolver开发教程请戳这里, scheduler plugin 教程 请看下列例子:
开发
import { CodeGenScheduler, ApiSource } from '@esydoc/plugin-codegen'
const HyextLifeCircleSet = new Set([
'onLoad',
'onUnload',
'onEnterForeground',
'onLeaveForeground',
'onAppear',
'onDisappear',
])
class HyextCodeGenPlugin {
apply(scheduler: CodeGenScheduler) {
scheduler.hooks.resolve.tap(
'HyextCodeGenPlugin',
(apiSource: ApiSource) => {
if (apiSource.parent === null) {
if (HyextLifeCircleSet.has(apiSource.name)) {
apiSource.parent = 'lifeCircle'
} else {
apiSource.parent = 'global'
}
apiSource.filename = `${apiSource.parent}.${apiSource.name}`
}
return apiSource
}
)
}
}
export default HyextCodeGenPlugin
使用
const HYExtSchedulerPlugin = require('./plugins/HYExtSchedulerPlugin').default
const esydocConfig = {
plugins: [
new HYExtSchedulerPlugin()
]
}
这个插件的作用就是通过修改 apiSource,去改变接口的信息,改变其渲染的结果。
note: scheduler plugin 适合承担一些全局的操作,而 resolver 不合适,resolver的设计理念是彼此独立,不回去修改 元数据(apiSource...),这样子会影响其他resolver解析的结果。
Helper
scheduler.helper
- helper.perttyCode(code: string): string - 格式化代码
- helper.addFile(dist: string, content: string): Promise - 输出文件