|
| 1 | +/** |
| 2 | + * @file fillModules |
| 3 | + * @module mlly/utils/fillModules |
| 4 | + */ |
| 5 | + |
| 6 | +import { SpecifierKind } from '#src/enums' |
| 7 | +import type { FillModuleOptions } from '#src/interfaces' |
| 8 | +import isFunction from '#src/internal/is-function' |
| 9 | +import { ERR_UNKNOWN_FILE_EXTENSION } from '@flex-development/errnode' |
| 10 | +import pathe, { type Ext } from '@flex-development/pathe' |
| 11 | +import type { EmptyString } from '@flex-development/tutils' |
| 12 | +import type { URL } from 'node:url' |
| 13 | +import CONDITIONS from './conditions' |
| 14 | +import extractStatements from './extract-statements' |
| 15 | +import isAbsoluteSpecifier from './is-absolute-specifier' |
| 16 | +import isBareSpecifier from './is-bare-specifier' |
| 17 | +import resolveModule from './resolve-module' |
| 18 | +import toBareSpecifier from './to-bare-specifier' |
| 19 | +import toRelativeSpecifier from './to-relative-specifier' |
| 20 | + |
| 21 | +/** |
| 22 | + * Ensures all absolute and relative module specifiers in the given piece of |
| 23 | + * source `code` are fully specified. |
| 24 | + * |
| 25 | + * Ignores specifiers that already have file extensions. |
| 26 | + * |
| 27 | + * @see {@linkcode FillModuleOptions} |
| 28 | + * @see https://nodejs.org/api/esm.html#mandatory-file-extensions |
| 29 | + * @see https://nodejs.org/api/esm.html#terminology |
| 30 | + * |
| 31 | + * @async |
| 32 | + * |
| 33 | + * @param {string} code - Code to evaluate |
| 34 | + * @param {FillModuleOptions} options - Module fill options |
| 35 | + * @return {Promise<string>} `code` with fully specified module specifiers |
| 36 | + */ |
| 37 | +const fillModules = async ( |
| 38 | + code: string, |
| 39 | + options: FillModuleOptions |
| 40 | +): Promise<string> => { |
| 41 | + const { conditions = CONDITIONS, ext, parent = import.meta.url } = options |
| 42 | + |
| 43 | + // ensure specifiers have file extensions |
| 44 | + for (const statement of extractStatements(code)) { |
| 45 | + // do nothing if statement does not have specifier |
| 46 | + if (!statement.specifier) continue |
| 47 | + |
| 48 | + // ignore statements with dynamic specifiers |
| 49 | + if (statement.specifier_kind === SpecifierKind.DYNAMIC) continue |
| 50 | + |
| 51 | + /** |
| 52 | + * Resolved module URL. |
| 53 | + * |
| 54 | + * @const {URL} url |
| 55 | + */ |
| 56 | + const url: URL = await resolveModule(statement.specifier, { |
| 57 | + ...options, |
| 58 | + /** |
| 59 | + * Returns a replacement file extension for the given module `specifier` |
| 60 | + * **if it is non-bare and does not already have an extension**. |
| 61 | + * |
| 62 | + * Throws [`ERR_UNKNOWN_FILE_EXTENSION`][1] if the replacement extension |
| 63 | + * is `null`, `undefined`, an empty string, or a dot character (`'.'`). |
| 64 | + * |
| 65 | + * [1]: https://nodejs.org/api/errors.html#err_unknown_file_extension |
| 66 | + * |
| 67 | + * @async |
| 68 | + * |
| 69 | + * @param {string} specifier - Module specifier |
| 70 | + * @param {URL} url - Resolved module URL |
| 71 | + * @return {Promise<string | undefined>} New file extension or `undefined` |
| 72 | + */ |
| 73 | + async ext(specifier: string, url: URL): Promise<string | undefined> { |
| 74 | + /** |
| 75 | + * Current file extension of {@linkcode specifier}. |
| 76 | + * |
| 77 | + * @const {EmptyString | Ext} extname |
| 78 | + */ |
| 79 | + const extname: EmptyString | Ext = pathe.extname(specifier) |
| 80 | + |
| 81 | + // skip replacement for bare and already fully specified specifiers |
| 82 | + if (isBareSpecifier(specifier) || extname.length > 1) return undefined |
| 83 | + |
| 84 | + /** |
| 85 | + * Replacement file extension. |
| 86 | + * |
| 87 | + * @var {string} rext |
| 88 | + */ |
| 89 | + const rext: string = isFunction(ext) ? await ext(specifier, url) : ext |
| 90 | + |
| 91 | + // ensure replacement extension is non-empty and non-dot ('.') |
| 92 | + if (!(rext && rext.trim().length > (rext.startsWith('.') ? 1 : 0))) { |
| 93 | + throw new ERR_UNKNOWN_FILE_EXTENSION(rext, specifier) |
| 94 | + } |
| 95 | + |
| 96 | + return rext |
| 97 | + } |
| 98 | + }) |
| 99 | + |
| 100 | + // replace original module specifier |
| 101 | + code = code.replace( |
| 102 | + statement.code, |
| 103 | + statement.code.replace( |
| 104 | + statement.specifier, |
| 105 | + // convert module url back to absolute, bare, or relative specifier |
| 106 | + statement.specifier.startsWith('#') |
| 107 | + ? statement.specifier |
| 108 | + : isAbsoluteSpecifier(statement.specifier) |
| 109 | + ? url.href |
| 110 | + : isBareSpecifier(statement.specifier) |
| 111 | + ? toBareSpecifier(url, parent, conditions) |
| 112 | + : toRelativeSpecifier(url, parent) |
| 113 | + ) |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + return code |
| 118 | +} |
| 119 | + |
| 120 | +export default fillModules |
0 commit comments