UNPKG

5.95 kBTypeScriptView Raw
1import type { BaseAtomAssetsParser as IAtomAssetsParser } from "./assetParsers/BaseParser";
2import type { IParsedBlockAsset } from "./assetParsers/block";
3import type { IDumiDemoProps } from "./client/theme-api/DumiDemo";
4import type { ILocalesConfig, IThemeConfig } from "./client/theme-api/types";
5import type { IContentTab } from "./features/tabs";
6import type { IThemeLoadResult } from "./features/theme/loader";
7import { OnLoadArgs, OnLoadResult } from '@umijs/bundler-utils/compiled/esbuild';
8import type { IModify } from '@umijs/core';
9import type { AssetsPackage, ExampleBlockAsset } from 'dumi-assets-types';
10import type { Element } from 'hast';
11import type { IApi as IUmiApi, defineConfig as defineUmiConfig } from 'umi';
12type Subset<K> = {
13 [attr in keyof K]?: K[attr] extends Array<any> ? K[attr] : K[attr] extends Function | undefined ? K[attr] : K[attr] extends object ? Subset<K[attr]> : K[attr] extends object | null ? Subset<K[attr]> | null : K[attr] extends object | null | undefined ? Subset<K[attr]> | null | undefined : K[attr];
14};
15type NoStringIndex<T> = {
16 [K in keyof T as string extends K ? never : K]: T[K];
17};
18type IUmiConfig = Omit<NoStringIndex<Parameters<typeof defineUmiConfig>[0]>, 'resolve' | 'extraRemarkPlugins' | 'extraRehypePlugins' | 'themeConfig'>;
19interface IDumiExtendsConfig {
20 resolve: {
21 docDirs: (string | {
22 type?: string;
23 dir: string;
24 })[];
25 atomDirs: {
26 type: string;
27 subType?: string;
28 dir: string;
29 }[];
30 codeBlockMode: 'active' | 'passive';
31 entryFile?: string;
32 forceKebabCaseRouting: boolean;
33 };
34 locales: ILocalesConfig;
35 themeConfig: IThemeConfig;
36 autoAlias?: boolean;
37 /**
38 * extra unified plugins
39 */
40 extraRemarkPlugins?: (string | Function | [string | Function, object])[];
41 extraRehypePlugins?: (string | Function | [string | Function, object])[];
42}
43export type IDumiConfig = Omit<IUmiConfig, 'locales'> & IDumiExtendsConfig;
44export type IDumiUserConfig = Subset<Omit<IDumiConfig, 'locales'>> & {
45 locales?: Exclude<IDumiConfig['locales'][0], {
46 base: string;
47 }>[] | Omit<Exclude<IDumiConfig['locales'][0], {
48 suffix: string;
49 }>, 'base'>[];
50 [key: string]: any;
51};
52export interface IDumiTechStackOnBlockLoadResult {
53 content: string;
54 type: Required<OnLoadResult>['loader'];
55}
56export type IDumiTechStackOnBlockLoadArgs = OnLoadArgs & {
57 entryPointCode: string;
58 filename: string;
59};
60export interface IDumiTechStackRuntimeOpts {
61 /**
62 * Component detection function path,
63 * used to detect errors that occur from application creation to component mounting.
64 */
65 preflightPath?: string;
66 /**
67 * path of the cancelable{@link IDemoCancelableFn} function
68 * that manipulate(mount/unmount) third-party framework component
69 */
70 rendererPath?: string;
71 /**
72 * path to runtime compile function module
73 */
74 compilePath?: string;
75 /**
76 * path to runtime plugin for this tech stack
77 */
78 pluginPath?: string;
79}
80export declare abstract class IDumiTechStack {
81 /**
82 * tech stack name, such as 'react'
83 */
84 abstract name: string;
85 /**
86 * runtime options
87 */
88 abstract runtimeOpts?: IDumiTechStackRuntimeOpts;
89 /**
90 * Is the lang supported by the current tech stack?
91 * @param lang
92 * @param node hast Element https://github.com/syntax-tree/hast?tab=readme-ov-file#element
93 */
94 abstract isSupported(node: Element, lang: string): boolean;
95 /**
96 * transform for parse demo source to expression/function/class
97 */
98 abstract transformCode(raw: string, opts: {
99 type: 'external' | 'code-block';
100 fileAbsPath: string;
101 }): string;
102 /**
103 * generator for return asset metadata
104 */
105 abstract generateMetadata?(asset: ExampleBlockAsset, opts: {
106 type: Parameters<IDumiTechStack['transformCode']>[1]['type'];
107 mdAbsPath: string;
108 fileAbsPath?: string;
109 entryPointCode?: string;
110 }): Promise<ExampleBlockAsset> | ExampleBlockAsset;
111 /**
112 * generator for return previewer props
113 */
114 abstract generatePreviewerProps?(props: IDumiDemoProps['previewerProps'], opts: Parameters<NonNullable<IDumiTechStack['generateMetadata']>>[1]): Promise<IDumiDemoProps['previewerProps']> | IDumiDemoProps['previewerProps'];
115 /**
116 * generator for return file path of demo source
117 */
118 abstract generateSources?(source: IParsedBlockAsset['resolveMap'], opts: Parameters<NonNullable<IDumiTechStack['generateMetadata']>>[1]): Promise<IParsedBlockAsset['resolveMap']> | IParsedBlockAsset['resolveMap'];
119 /**
120 * Use current function as onLoad CallBack(https://esbuild.github.io/plugins/#on-load)
121 * @description
122 * Why use this method?
123 * By default, dumi can only support the parsing of js/ts related code blocks,
124 * but many front-end frameworks have custom extensions,
125 * so this method is provided to facilitate developers to convert codes.
126 */
127 abstract onBlockLoad?(args: IDumiTechStackOnBlockLoadArgs): IDumiTechStackOnBlockLoadResult | null;
128}
129export type IApi = IUmiApi & {
130 config: IDumiConfig & {
131 [key: string]: any;
132 };
133 userConfig: IDumiUserConfig;
134 service: IUmiApi['service'] & {
135 themeData: IThemeLoadResult;
136 atomParser: IAtomAssetsParser;
137 };
138 /**
139 * register a new tech stack
140 */
141 registerTechStack: (fn: () => IDumiTechStack) => void;
142 /**
143 * modify original theme data
144 */
145 modifyTheme: IModify<IThemeLoadResult, null>;
146 /**
147 * add content tab
148 */
149 addContentTab: (fn: () => IContentTab) => void;
150 /**
151 * modify assets metadata
152 */
153 modifyAssetsMetadata: IModify<AssetsPackage, null>;
154 /**
155 * get assets metadata
156 */
157 getAssetsMetadata?: () => Promise<AssetsPackage>;
158};
159export {};