import type { BuildContext, TransformEntry } from "../types.ts"; import { pathToFileURL } from "node:url"; import { dirname, extname, join, relative } from "pathe"; import { mkdir, readFile, symlink, writeFile } from "node:fs/promises"; import { consola } from "consola"; import { colors as c } from "consola/utils"; import { resolveModulePath, type ResolveOptions } from "exsolve"; import MagicString from "magic-string"; import { fmtPath, removeComments } from "../utils.ts"; import { glob } from "tinyglobby"; import { minifySync, transformSync, parseSync, Visitor } from "rolldown/utils"; import { makeExecutable, SHEBANG_RE } from "./plugins/shebang.ts"; /** * Transform all .ts modules in a directory using oxc-transform. */ export async function transformDir(ctx: BuildContext, entry: TransformEntry): Promise { const promises: Promise[] = []; for await (const entryName of await glob("**/*.*", { cwd: entry.input })) { if (entry.filter && (await entry.filter(entryName)) === false) { continue; } promises.push( (async () => { const entryPath = join(entry.input, entryName); const ext = extname(entryPath); switch (ext) { case ".ts": { { const entryDistPath = join(entry.outDir!, entryName.replace(/\.ts$/, ".mjs")); const transformed = await transformModule(entryPath, entry, entryDistPath); await mkdir(dirname(entryDistPath), { recursive: true }); await writeFile(entryDistPath, transformed.code, "utf8"); if (SHEBANG_RE.test(transformed.code)) { await makeExecutable(entryDistPath); } if (transformed.declaration) { await writeFile( entryDistPath.replace(/\.mjs$/, ".d.mts"), transformed.declaration, "utf8", ); } return entryDistPath; } } default: { { const entryDistPath = join(entry.outDir!, entryName); await mkdir(dirname(entryDistPath), { recursive: true }); if (entry.stub) { await symlink(entryPath, entryDistPath, "junction").catch(() => { /* exists */ }); } else { const code = await readFile(entryPath, "utf8"); await writeFile(entryDistPath, code, "utf8"); if (SHEBANG_RE.test(code)) { await makeExecutable(entryDistPath); } } return entryDistPath; } } } })(), ); } const results = await Promise.allSettled(promises); const errors = results .filter((result): result is PromiseRejectedResult => result.status === "rejected") .map((result) => result.reason); if (errors.length > 0) { let isolatedErrors: Error[] = []; let otherErrors: Error[] = []; for (const error of errors.flatMap((err) => (Array.isArray(err.cause) ? err.cause : [err]))) { if (error.message?.includes("--isolatedDeclarations")) { isolatedErrors.push(error); } else { otherErrors.push(error); } } for (const error of otherErrors) { consola.error(error); } for (const error of isolatedErrors) { consola.warn(error); } if (otherErrors.length > 0) { throw new Error(`Errors while transforming ${entry.input}`); } } const writtenFiles = results .filter((result): result is PromiseFulfilledResult => result.status === "fulfilled") .map((result) => result.value); consola.log( `\n${c.magenta("[transform] ")}${c.underline(fmtPath(entry.outDir!) + "/")}${entry.stub ? c.dim(" (stub)") : ""}\n${c.dim(itemsTable(writtenFiles.map((f) => fmtPath(f))))}`, ); } function itemsTable(items: string[], consoleWidth: number = process.stdout.columns || 80): string { if (items.length === 0) { return ""; } const maxItemWidth = Math.max(...items.map((item) => item.length)); const colWidth = maxItemWidth + 2; const columns = Math.max(1, Math.floor(consoleWidth / colWidth)); const rows: string[] = []; for (let i = 0; i < items.length; i += columns) { const row = items.slice(i, i + columns); rows.push(row.map((item) => item.padEnd(colWidth)).join("")); } return rows.join("\n"); } /** * Transform a .ts module using oxc-transform. */ async function transformModule(entryPath: string, entry: TransformEntry, entryDistPath: string) { let sourceText = await readFile(entryPath, "utf8"); const sourceOptions = { lang: "ts", sourceType: "module", } as const; const parsed = parseSync(entryPath, sourceText, { ...sourceOptions, }); if (entry.stub) { const hasDefaultExport = parsed?.module?.staticExports?.find((exp) => exp.entries.some((e) => e.exportName.kind === "Default"), ) !== undefined; const relativePath = relative(dirname(entryDistPath), entryPath); const code = `export * from "${relativePath}";${ hasDefaultExport ? `\nexport { default } from "${relativePath}";` : "" }`; return { code, declaration: code, }; } if (parsed.errors.length > 0) { throw new Error(`Errors while parsing ${entryPath}:`, { cause: parsed.errors, }); } const resolveOptions: ResolveOptions = { ...entry.resolve, // Keep default results local to this transform so a rebuild sees filesystem changes. cache: entry.resolve?.cache ?? new Map(), from: pathToFileURL(entryPath), extensions: entry.resolve?.extensions ?? [".ts", ".js", ".mjs", ".cjs", ".json"], suffixes: entry.resolve?.suffixes ?? ["", "/index"], }; const magicString = new MagicString(sourceText); // Rewrite relative imports const updatedStarts = new Set(); const rewriteSpecifier = ( req: { value: string; start: number; end: number }, tryResolve = false, ) => { const moduleId = req.value; if (!moduleId.startsWith(".")) { return; } if (updatedStarts.has(req.start)) { return; // prevent double rewritings } updatedStarts.add(req.start); // Resolve the path without its URL suffix so extension and index lookup still work. const suffixStart = moduleId.search(/[?#]/); const pathname = suffixStart === -1 ? moduleId : moduleId.slice(0, suffixStart); const suffix = suffixStart === -1 ? "" : moduleId.slice(suffixStart); const resolvedAbsolute = tryResolve ? resolveModulePath(pathname, { ...resolveOptions, try: true }) : resolveModulePath(pathname, resolveOptions); if (!resolvedAbsolute) { return; } const newPath = relative(dirname(entryPath), resolvedAbsolute.replace(/\.ts$/, ".mjs")) .split("/") .map(encodeURIComponent) .join("/"); const newId = (newPath.startsWith(".") ? newPath : `./${newPath}`) + suffix; magicString.remove(req.start, req.end); magicString.prependLeft(req.start, JSON.stringify(newId)); }; for (const staticImport of parsed.module.staticImports) { rewriteSpecifier(staticImport.moduleRequest); } for (const staticExport of parsed.module.staticExports) { for (const staticExportEntry of staticExport.entries) { if (staticExportEntry.moduleRequest) { rewriteSpecifier(staticExportEntry.moduleRequest); } } } if (parsed.module.dynamicImports.length > 0 || sourceText.includes("import")) { new Visitor({ ImportExpression(node) { let source = node.source; while ( source.type === "ParenthesizedExpression" || source.type === "TSAsExpression" || source.type === "TSSatisfiesExpression" || source.type === "TSTypeAssertion" ) { source = source.expression; } if (source.type === "Literal" && typeof source.value === "string") { rewriteSpecifier({ value: source.value, start: source.start, end: source.end }, true); } else if (source.type === "TemplateLiteral" && source.expressions.length === 0) { const cooked = source.quasis[0]?.value.cooked; if (cooked !== null && cooked !== undefined) { rewriteSpecifier({ value: cooked, start: source.start, end: source.end }, true); } } }, TSImportType(node) { rewriteSpecifier(node.source, true); }, }).visit(parsed.program); } sourceText = magicString.toString(); let emitDeclaration = entry.dts ?? true; if (typeof emitDeclaration === "function") { emitDeclaration = await emitDeclaration(entryPath); } const runTransform = (declaration: boolean) => transformSync(entryPath, sourceText, { ...entry.oxc, ...sourceOptions, cwd: dirname(entryPath), typescript: { ...entry.oxc?.typescript, declaration: declaration ? { stripInternal: true } : undefined, }, }); let transformed = runTransform(!!emitDeclaration); // If the only failures are isolated-declaration parse errors, retry without // declaration emit so the .mjs is still written. Without this, a single // un-inferable type drops the whole file from dist while the build reports // success — see https://github.com/unjs/obuild/issues/82. if ( emitDeclaration && transformed.errors.length > 0 && transformed.errors.every((err) => err.message?.includes("--isolatedDeclarations")) ) { for (const err of transformed.errors) { consola.warn(err); } consola.warn( `[obuild] declaration emit skipped for ${fmtPath(entryPath)} (--isolatedDeclarations); .mjs still emitted, .d.mts missing`, ); transformed = runTransform(false); } if (transformed.warnings.length > 0) { for (const warning of transformed.warnings) { consola.warn(warning); } } if (transformed.errors.length > 0) { if (transformed.errors.length === 1) { throw transformed.errors[0]; } throw new Error(`Errors while transforming ${entryPath}`, { cause: transformed.errors, }); } if (entry.minify) { const res = minifySync(entryPath, transformed.code, entry.minify === true ? {} : entry.minify); transformed.code = res.code; transformed.map = res.map; } transformed.code = removeComments(transformed.code); return transformed; }