File size: 6,093 Bytes
369fac9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
interface ResolveOptions {
    url?: string | URL | (string | URL)[];
    extensions?: string[];
    conditions?: string[];
}
declare function resolveSync(id: string, options?: ResolveOptions): string;
declare function resolve(id: string, options?: ResolveOptions): Promise<string>;
declare function resolvePathSync(id: string, options?: ResolveOptions): string;
declare function resolvePath(id: string, options?: ResolveOptions): Promise<string>;
declare function createResolve(defaults?: ResolveOptions): (id: string, url?: ResolveOptions["url"]) => Promise<string>;
declare function parseNodeModulePath(path: string): {
    dir?: undefined;
    name?: undefined;
    subpath?: undefined;
} | {
    dir: string;
    name: string;
    subpath: string | undefined;
};
/** Reverse engineer a subpath export if possible */
declare function lookupNodeModuleSubpath(path: string): Promise<string | undefined>;

interface ESMImport {
    type: "static" | "dynamic";
    code: string;
    start: number;
    end: number;
}
interface StaticImport extends ESMImport {
    type: "static";
    imports: string;
    specifier: string;
}
interface ParsedStaticImport extends StaticImport {
    defaultImport?: string;
    namespacedImport?: string;
    namedImports?: {
        [name: string]: string;
    };
}
interface DynamicImport extends ESMImport {
    type: "dynamic";
    expression: string;
}
interface TypeImport extends Omit<ESMImport, "type"> {
    type: "type";
    imports: string;
    specifier: string;
}
interface ESMExport {
    _type?: "declaration" | "named" | "default" | "star";
    type: "declaration" | "named" | "default" | "star";
    declarationType?: "let" | "var" | "const" | "enum" | "const enum" | "class" | "function" | "async function";
    code: string;
    start: number;
    end: number;
    name?: string;
    defaultName?: string;
    names: string[];
    specifier?: string;
}
interface DeclarationExport extends ESMExport {
    type: "declaration";
    declaration: string;
    name: string;
}
interface NamedExport extends ESMExport {
    type: "named";
    exports: string;
    names: string[];
    specifier?: string;
}
interface DefaultExport extends ESMExport {
    type: "default";
}
declare const ESM_STATIC_IMPORT_RE: RegExp;
declare const DYNAMIC_IMPORT_RE: RegExp;
declare const EXPORT_DECAL_RE: RegExp;
declare const EXPORT_DECAL_TYPE_RE: RegExp;
declare function findStaticImports(code: string): StaticImport[];
declare function findDynamicImports(code: string): DynamicImport[];
declare function findTypeImports(code: string): TypeImport[];
declare function parseStaticImport(matched: StaticImport | TypeImport): ParsedStaticImport;
declare function parseTypeImport(matched: TypeImport | StaticImport): ParsedStaticImport;
declare function findExports(code: string): ESMExport[];
declare function findTypeExports(code: string): ESMExport[];
declare function findExportNames(code: string): string[];
declare function resolveModuleExportNames(id: string, options?: ResolveOptions): Promise<string[]>;

interface CommonjsContext {
    __filename: string;
    __dirname: string;
    require: NodeRequire;
}
declare function createCommonJS(url: string): CommonjsContext;
declare function interopDefault(sourceModule: any, opts?: {
    preferNamespace?: boolean;
}): any;

interface EvaluateOptions extends ResolveOptions {
    url?: string;
}
declare function loadModule(id: string, options?: EvaluateOptions): Promise<any>;
declare function evalModule(code: string, options?: EvaluateOptions): Promise<any>;
declare function transformModule(code: string, options?: EvaluateOptions): Promise<string>;
declare function resolveImports(code: string, options?: EvaluateOptions): Promise<string>;

type DetectSyntaxOptions = {
    stripComments?: boolean;
};
declare function hasESMSyntax(code: string, opts?: DetectSyntaxOptions): boolean;
declare function hasCJSSyntax(code: string, opts?: DetectSyntaxOptions): boolean;
declare function detectSyntax(code: string, opts?: DetectSyntaxOptions): {
    hasESM: boolean;
    hasCJS: boolean;
    isMixed: boolean;
};
interface ValidNodeImportOptions extends ResolveOptions {
    /**
     * The contents of the import, which may be analyzed to see if it contains
     * CJS or ESM syntax as a last step in checking whether it is a valid import.
     */
    code?: string;
    /**
     * Protocols that are allowed as valid node imports.
     *
     * Default: ['node', 'file', 'data']
     */
    allowedProtocols?: Array<string>;
}
declare function isValidNodeImport(id: string, _options?: ValidNodeImportOptions): Promise<boolean>;

declare function fileURLToPath(id: string | URL): string;
declare function pathToFileURL(id: string | URL): string;
declare function sanitizeURIComponent(name?: string, replacement?: string): string;
declare function sanitizeFilePath(filePath?: string): string;
declare function normalizeid(id: string): string;
declare function loadURL(url: string): Promise<string>;
declare function toDataURL(code: string): string;
declare function isNodeBuiltin(id?: string): boolean;
declare function getProtocol(id: string): string | undefined;

export { type CommonjsContext, DYNAMIC_IMPORT_RE, type DeclarationExport, type DefaultExport, type DetectSyntaxOptions, type DynamicImport, type ESMExport, type ESMImport, ESM_STATIC_IMPORT_RE, EXPORT_DECAL_RE, EXPORT_DECAL_TYPE_RE, type EvaluateOptions, type NamedExport, type ParsedStaticImport, type ResolveOptions, type StaticImport, type TypeImport, type ValidNodeImportOptions, createCommonJS, createResolve, detectSyntax, evalModule, fileURLToPath, findDynamicImports, findExportNames, findExports, findStaticImports, findTypeExports, findTypeImports, getProtocol, hasCJSSyntax, hasESMSyntax, interopDefault, isNodeBuiltin, isValidNodeImport, loadModule, loadURL, lookupNodeModuleSubpath, normalizeid, parseNodeModulePath, parseStaticImport, parseTypeImport, pathToFileURL, resolve, resolveImports, resolveModuleExportNames, resolvePath, resolvePathSync, resolveSync, sanitizeFilePath, sanitizeURIComponent, toDataURL, transformModule };