Skip to content

Commit

Permalink
fix: 修正快捷注册方法的类型推导
Browse files Browse the repository at this point in the history
  • Loading branch information
xuasir committed Feb 24, 2021
1 parent 65a796b commit 2b776d1
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 38 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/exports/pluginAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface IPluginAPI extends IPluginAPIBase {
modifyLibBundler: IBuildLibMethods['modifyLibBundler']
onLibBuildFailed: IBuildLibMethods['onLibBuildFailed']
onLibBuildSucceed: IBuildLibMethods['onLibBuildSucceed']
runBuild: IBuildLibMethods['runBuild']
runLibBuild: IBuildLibMethods['runLibBuild']

// bundler rollup plugin
modifyRollupConfig: IBundlerRollupMethods['modifyRollupConfig']
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ export * from '@xus/plugin-build-lib'
export * from '@xus/plugin-bundler-rollup'

// types
export type { IArgs, IRawArgs, ICommand, IHook } from '@xus/core'
export type {
IArgs,
IRawArgs,
ICommand,
IHook,
IFastHookRegister
} from '@xus/core'
10 changes: 8 additions & 2 deletions packages/core/src/cli/PluginAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type { ICommandFn, ICommandOps, IHook, IHookApplyOps } from '../types'
import type {
ICommandFn,
ICommandOps,
IHook,
IHookApplyOps,
IRegisterMethodArgs
} from '../types'
import type { ICliService } from './Service'
import { Logger } from '@xus/cli-shared'

Expand Down Expand Up @@ -72,7 +78,7 @@ class PluginAPI {
this.service.pluginMethods[methodName] =
ops?.fn ||
// point this to caller
function (fn: Omit<IHook, 'name' | 'pluginName'> | IHook['fn']) {
function (fn: IRegisterMethodArgs) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.registerHook({
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/types/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ export interface IHookApplyOps {
initialValue?: any
args?: any
}

export type IRegisterMethodArgs<
T extends (...args: any[]) => any = (...args: any[]) => any
> = Omit<IHook<T>, 'name' | 'pluginName'> | T

export type IFastHookRegister<
T extends (...args: any[]) => any = (...args: any[]) => any
> = (ops: IRegisterMethodArgs<T>) => void
4 changes: 2 additions & 2 deletions packages/plugin-build-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"url": "git+https://github.com/xus-code/bundle-tools.git"
},
"scripts": {
"dev:clib": "tsc --watch",
"build:clib": "tsc"
"dev:blib": "tsc --watch",
"build:blib": "tsc"
},
"bugs": {
"url": "https://github.com/xus-code/bundle-tools/issues"
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-build-lib/src/plugin/enum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export enum Methods {
export enum BuildLibMethods {
ModifyLibBundler = 'modifyLibBundler',
OnLibBuildSucceed = 'onLibBuildSucceed',
OnLibBuildFailed = 'onLibBuildFailed',
RunBuild = 'runBuild'
RunLibBuild = 'runLibBuild'
}
31 changes: 17 additions & 14 deletions packages/plugin-build-lib/src/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
import type { IBundler } from './types'
import { createPlugin, HookTypes } from '@xus/cli'
import { Methods } from './enum'
import type { IBundler, ILibBuildOps } from './types'
import { createPlugin, HookTypes, RollupBundler } from '@xus/cli'
import { BuildLibMethods } from './enum'

export default createPlugin({
name: 'build:lib',
name: 'lib:build',
apply(api) {
// global method
api.registerMethod({
methodName: Methods.ModifyLibBundler,
methodName: BuildLibMethods.ModifyLibBundler,
throwOnExist: false
})
api.registerMethod({
methodName: Methods.OnLibBuildSucceed,
methodName: BuildLibMethods.OnLibBuildSucceed,
throwOnExist: false
})
api.registerMethod({
methodName: Methods.OnLibBuildFailed,
methodName: BuildLibMethods.OnLibBuildFailed,
throwOnExist: false
})
api.registerMethod({
methodName: Methods.RunBuild,
methodName: BuildLibMethods.RunLibBuild,
throwOnExist: false,
fn: async () => {
fn: async (ops: ILibBuildOps) => {
api.logger.debug(`get lib bundler: `)
// get lib bundler
const LibBundler = await api.applyHook<IBundler>({
name: Methods.ModifyLibBundler,
type: HookTypes.serial
name: BuildLibMethods.ModifyLibBundler,
type: HookTypes.serial,
initialValue: RollupBundler
})
api.logger.debug(LibBundler)

const libBundler = new LibBundler(api)

try {
const stats = await libBundler.build()
const stats = await libBundler.build(ops)
await api.applyHook({
name: Methods.OnLibBuildSucceed,
name: BuildLibMethods.OnLibBuildSucceed,
type: HookTypes.event,
args: stats
})
} catch (e) {
await api.applyHook({
name: Methods.OnLibBuildFailed,
name: BuildLibMethods.OnLibBuildFailed,
type: HookTypes.event,
args: e
})
Expand Down
40 changes: 24 additions & 16 deletions packages/plugin-build-lib/src/plugin/types.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
import type { IPluginAPI } from '@xus/cli'
import { Methods } from './enum'
import type { IPluginAPI, IFastHookRegister } from '@xus/cli'
import { BuildLibMethods } from './enum'

// for register method
export interface IMethods {
[Methods.ModifyLibBundler]: (bundler: IBundlerImp) => IBundlerImp
[Methods.OnLibBuildFailed]: (e: any) => void
[Methods.OnLibBuildSucceed]: (stats: IBuildRes) => void
[Methods.RunBuild]: () => void
export interface IBuildLibMethods {
[BuildLibMethods.ModifyLibBundler]: IFastHookRegister<
(bundler: IBundlerImp) => IBundlerImp
>
[BuildLibMethods.OnLibBuildFailed]: IFastHookRegister<(e: any) => void>
[BuildLibMethods.OnLibBuildSucceed]: IFastHookRegister<
(stats: ILibBuildRes) => void
>
[BuildLibMethods.RunLibBuild]: (ops: Partial<ILibBuildOps>) => void
}

export type IModifyConfigContext = {
[key in ITargets]: boolean
// for bundler imp
export interface ILibBuildStats {
info: string
}

// for bundler inter
export interface IBuildStats {
info: string
export type ILibBuildRes = {
[key in ILibBuildTargets]?: ILibBuildStats
}

export type IBuildRes = {
[key in ITargets]?: IBuildRes
export interface ILibBuildOps {
targets: ILibBuildTargets[]
watch: boolean
[key: string]: any
}

export interface IBundlerImp {
build: () => Promise<IBuildRes>
build: <T extends ILibBuildOps = ILibBuildOps>(
ops: T
) => Promise<ILibBuildRes>
[key: string]: any
}

Expand All @@ -32,4 +40,4 @@ export interface IBundler extends IBundlerImp {
}

// for esm tagret
export type ITargets = 'esm' | 'cjs' | 'browser' | 'modern'
export type ILibBuildTargets = 'esm' | 'cjs' | 'browser' | 'modern'

0 comments on commit 2b776d1

Please sign in to comment.