From 2b776d16039fd21e4eeec28be9500773eeeb4cab Mon Sep 17 00:00:00 2001 From: "guo.xu" Date: Wed, 24 Feb 2021 15:44:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E5=BF=AB=E6=8D=B7?= =?UTF-8?q?=E6=B3=A8=E5=86=8C=E6=96=B9=E6=B3=95=E7=9A=84=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=8E=A8=E5=AF=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/cli/src/exports/pluginAPI.ts | 2 +- packages/cli/src/index.ts | 8 +++- packages/core/src/cli/PluginAPI.ts | 10 ++++- packages/core/src/types/hook.ts | 8 ++++ packages/plugin-build-lib/package.json | 4 +- packages/plugin-build-lib/src/plugin/enum.ts | 4 +- packages/plugin-build-lib/src/plugin/index.ts | 31 +++++++------- packages/plugin-build-lib/src/plugin/types.ts | 40 +++++++++++-------- 8 files changed, 69 insertions(+), 38 deletions(-) diff --git a/packages/cli/src/exports/pluginAPI.ts b/packages/cli/src/exports/pluginAPI.ts index edcb8ef..e761288 100644 --- a/packages/cli/src/exports/pluginAPI.ts +++ b/packages/cli/src/exports/pluginAPI.ts @@ -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'] diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 9d048bd..b385a38 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -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' diff --git a/packages/core/src/cli/PluginAPI.ts b/packages/core/src/cli/PluginAPI.ts index e20a67b..84e03a9 100644 --- a/packages/core/src/cli/PluginAPI.ts +++ b/packages/core/src/cli/PluginAPI.ts @@ -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' @@ -72,7 +78,7 @@ class PluginAPI { this.service.pluginMethods[methodName] = ops?.fn || // point this to caller - function (fn: Omit | IHook['fn']) { + function (fn: IRegisterMethodArgs) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore this.registerHook({ diff --git a/packages/core/src/types/hook.ts b/packages/core/src/types/hook.ts index b85a2f7..e8ae66b 100644 --- a/packages/core/src/types/hook.ts +++ b/packages/core/src/types/hook.ts @@ -19,3 +19,11 @@ export interface IHookApplyOps { initialValue?: any args?: any } + +export type IRegisterMethodArgs< + T extends (...args: any[]) => any = (...args: any[]) => any +> = Omit, 'name' | 'pluginName'> | T + +export type IFastHookRegister< + T extends (...args: any[]) => any = (...args: any[]) => any +> = (ops: IRegisterMethodArgs) => void diff --git a/packages/plugin-build-lib/package.json b/packages/plugin-build-lib/package.json index 539c988..1785832 100644 --- a/packages/plugin-build-lib/package.json +++ b/packages/plugin-build-lib/package.json @@ -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" diff --git a/packages/plugin-build-lib/src/plugin/enum.ts b/packages/plugin-build-lib/src/plugin/enum.ts index b8a1818..72bba79 100644 --- a/packages/plugin-build-lib/src/plugin/enum.ts +++ b/packages/plugin-build-lib/src/plugin/enum.ts @@ -1,6 +1,6 @@ -export enum Methods { +export enum BuildLibMethods { ModifyLibBundler = 'modifyLibBundler', OnLibBuildSucceed = 'onLibBuildSucceed', OnLibBuildFailed = 'onLibBuildFailed', - RunBuild = 'runBuild' + RunLibBuild = 'runLibBuild' } diff --git a/packages/plugin-build-lib/src/plugin/index.ts b/packages/plugin-build-lib/src/plugin/index.ts index e7d91a1..430f677 100644 --- a/packages/plugin-build-lib/src/plugin/index.ts +++ b/packages/plugin-build-lib/src/plugin/index.ts @@ -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({ - 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 }) diff --git a/packages/plugin-build-lib/src/plugin/types.ts b/packages/plugin-build-lib/src/plugin/types.ts index 672e5bd..0f3495b 100644 --- a/packages/plugin-build-lib/src/plugin/types.ts +++ b/packages/plugin-build-lib/src/plugin/types.ts @@ -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) => 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 + build: ( + ops: T + ) => Promise [key: string]: any } @@ -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'