forPackage

tcmts.cli.pack.forPackage · #q3q4wu33 · term · runs anywhere

Emitted code and declarations, for outside the cache: packages by name rather than the cache's aliases (where that's unambiguous; TypeScript calls an alias's DefinitelyTyped types @npm/<alias>), other modules by their .js files (declaration emit keeps .ts), and no JSX pragmas naming aliases.

export function forPackage(text: string, aliases: Map<string, string>): string {
  return text
    .replace(/^\/\/ tcm: transform\n/m, "")
    .replace(/^\/\*\* @jsxImportSource npm__[^*]*\*\/\n/gm, "")
    .replace(/((?:from|import)\s*\(?\s*["']\.\/[^"']+)\.tsx?(["'])/g, "$1.js$2")
    .replace(/((?:from|import)\s*\(?\s*["'])(npm__[^"'/]+|@npm\/[^"'/]+)((?:\/[^"']*)?["'])/g, (m, lead: string, alias: string, rest: string) => {
      const name = aliases.get(alias.startsWith("@npm/") ? `npm__${alias.slice(5)}` : alias);
      return name ? `${lead}${name}${rest}` : m;
    });
}

used by