|
| 1 | +// Modified and inlined to avoid extra dependency |
| 2 | +// Source: https://github.com/guybedford/es-module-lexer/blob/main/types/lexer.d.ts |
| 3 | +// MIT Licensed https://github.com/guybedford/es-module-lexer/blob/main/LICENSE |
| 4 | + |
| 5 | +export interface ImportSpecifier { |
| 6 | + /** |
| 7 | + * Module name |
| 8 | + * |
| 9 | + * To handle escape sequences in specifier strings, the .n field of imported specifiers will be provided where possible. |
| 10 | + * |
| 11 | + * For dynamic import expressions, this field will be empty if not a valid JS string. |
| 12 | + * |
| 13 | + * @example |
| 14 | + * const [imports1, exports1] = parse(String.raw`import './\u0061\u0062.js'`); |
| 15 | + * imports1[0].n; |
| 16 | + * // Returns "./ab.js" |
| 17 | + * |
| 18 | + * const [imports2, exports2] = parse(`import("./ab.js")`); |
| 19 | + * imports2[0].n; |
| 20 | + * // Returns "./ab.js" |
| 21 | + * |
| 22 | + * const [imports3, exports3] = parse(`import("./" + "ab.js")`); |
| 23 | + * imports3[0].n; |
| 24 | + * // Returns undefined |
| 25 | + */ |
| 26 | + readonly n: string | undefined |
| 27 | + /** |
| 28 | + * Start of module specifier |
| 29 | + * |
| 30 | + * @example |
| 31 | + * const source = `import { a } from 'asdf'`; |
| 32 | + * const [imports, exports] = parse(source); |
| 33 | + * source.substring(imports[0].s, imports[0].e); |
| 34 | + * // Returns "asdf" |
| 35 | + */ |
| 36 | + readonly s: number |
| 37 | + /** |
| 38 | + * End of module specifier |
| 39 | + */ |
| 40 | + readonly e: number |
| 41 | + |
| 42 | + /** |
| 43 | + * Start of import statement |
| 44 | + * |
| 45 | + * @example |
| 46 | + * const source = `import { a } from 'asdf'`; |
| 47 | + * const [imports, exports] = parse(source); |
| 48 | + * source.substring(imports[0].ss, imports[0].se); |
| 49 | + * // Returns `"import { a } from 'asdf';"` |
| 50 | + */ |
| 51 | + readonly ss: number |
| 52 | + /** |
| 53 | + * End of import statement |
| 54 | + */ |
| 55 | + readonly se: number |
| 56 | + |
| 57 | + /** |
| 58 | + * If this import statement is a dynamic import, this is the start value. |
| 59 | + * Otherwise this is `-1`. |
| 60 | + */ |
| 61 | + readonly d: number |
| 62 | + |
| 63 | + /** |
| 64 | + * If this import has an import assertion, this is the start value. |
| 65 | + * Otherwise this is `-1`. |
| 66 | + */ |
| 67 | + readonly a: number |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Wait for init to resolve before calling `parse`. |
| 72 | + */ |
| 73 | +export const init: Promise<void> |
| 74 | + |
| 75 | +/** |
| 76 | + * Outputs the list of exports and locations of import specifiers, |
| 77 | + * including dynamic import and import meta handling. |
| 78 | + * |
| 79 | + * @param source - Source code to parser |
| 80 | + * @param name - Optional sourcename |
| 81 | + * @returns Tuple contaning imports list and exports list. |
| 82 | + */ |
| 83 | +export function parse( |
| 84 | + source: string, |
| 85 | + name?: string |
| 86 | +): readonly [ |
| 87 | + imports: ReadonlyArray<ImportSpecifier>, |
| 88 | + exports: ReadonlyArray<string>, |
| 89 | + facade: boolean |
| 90 | +] |
0 commit comments