forked from kuitos/import-html-entry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
56 lines (42 loc) · 1.7 KB
/
index.d.ts
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* @author kuitos
* @since 2019-05-16
*/
interface IImportResult {
template: string;
assetPublicPath: string;
execScripts<T>(sandbox?: object, strictGlobal?: boolean, execScriptsHooks?: ExecScriptsHooks): Promise<T>;
getExternalScripts(): Promise<string[]>;
getExternalStyleSheets(): Promise<string[]>;
}
interface TemplateScriptObject {
async: boolean;
src: string;
}
interface TemplateResult {
template: string;
scripts: (string | TemplateScriptObject)[];
styles: string[];
entry: string | TemplateScriptObject;
}
export type ImportEntryOpts = {
fetch?: typeof window.fetch | { fn?: typeof window.fetch, autoDecodeResponse?: boolean }
getPublicPath?: (entry: Entry) => string;
getTemplate?: (tpl: string) => string;
postProcessTemplate?: (tplResult: TemplateResult) => TemplateResult;
}
export type ExecScriptsHooks = {
// 每个脚本执行之前触发,如果返回的是非空string, 那么将把返回值替换code执行
beforeExec?: (code: string, script: string) => string | void;
// 每个脚本执行完毕后触发,如果脚本执行报错,那么就不会触发
afterExec?: (code: string, script: string) => void;
}
type ExecScriptsOpts = Pick<ImportEntryOpts, 'fetch'> & ExecScriptsHooks & {
strictGlobal?: boolean;
success?: CallableFunction;
error?: CallableFunction;
}
export type Entry = string | { styles?: string[], scripts?: string[], html?: string };
export function execScripts<T>(entry: string | null, scripts: string[], proxy: Window, opts?: ExecScriptsOpts): Promise<T>;
export default function importHTML(url: string, opts?: ImportEntryOpts | Function): Promise<IImportResult>;
export function importEntry(entry: Entry, opts?: ImportEntryOpts): Promise<IImportResult>;