import { Logger } from "@libs/logger"
import type { Nullable, record } from "@libs/typing"
export type { record }
const regex = /^(?<statement>import\s*(?:(?:\s+type)?\s+(?:[^\n]+?|(?:\{[\s\S]*?\}))\s+from\s+)?(?<quote>["']))(?<module>[^\n]+?)\k<quote>/gm
export function unmap(content: string, imports: record<string>, { logger: log = new Logger() } = {}): { result: string; resolved: number } {
let resolved = 0
const directories = Object.keys(imports).filter((module) => module.endsWith("/")) as string[]
const result = content.replace(regex, function (match, statement, quote, module, i) {
log.with({ i }).trace(match)
let mapped = null as Nullable<string>
if (module in imports) {
mapped = imports[module]
} else if (directories.some((directory) => module.startsWith(directory))) {
const [directory] = directories.filter((directory) => module.startsWith(directory)).sort((a, b) => b.length - a.length)
mapped = module.replace(directory, imports[directory])
}
if (typeof mapped === "string") {
resolved++
log.with({ i }).debug(`${module} → ${mapped}`)
return `${statement}${mapped}${quote}`
}
return match
})
return { result, resolved }
}
|