8000 feat: infer aliases from tsconfig.json by kricsleo · Pull Request #494 · unjs/unbuild · GitHub
[go: up one dir, main page]

Skip to content

feat: infer aliases from tsconfig.json #494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: apply automated updates
  • Loading branch information
autofix-ci[bot] authored Jan 25, 2025
commit 91d4f3c5fe870da73cb3c0e45a1ff76aab6c9c0f
4 changes: 3 additions & 1 deletion src/builders/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import { cjsPlugin } from "./plugins/cjs";
import { shebangPlugin } from "./plugins/shebang";
import { DEFAULT_EXTENSIONS, getChunkFilename, resolveAliases } from "./utils";

export async function getRollupOptions(ctx: BuildContext): Promise<RollupOptions> {
export async function getRollupOptions(
ctx: BuildContext,
): Promise<RollupOptions> {
const _aliases = await resolveAliases(ctx);
return (<RollupOptions>{
input: Object.fromEntries(
Expand Down
2 changes: 1 addition & 1 deletion src/builders/rollup/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function rollupStub(ctx: BuildContext): Promise<void> {
{
...ctx.options.stubOptions.jiti,
alias: {
...await resolveAliases(ctx),
...(await resolveAliases(ctx)),
...ctx.options.stubOptions.jiti.alias,
},
transformOptions: {
Expand Down
75 changes: 45 additions & 30 deletions src/builders/rollup/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dirname, resolve } from 'pathe'
import { dirname, resolve } from "pathe";
import type { PreRenderedChunk } from "rollup";
import type { BuildContext } from "../../types";

Expand All @@ -14,7 +14,9 @@ export const DEFAULT_EXTENSIONS: string[] = [
".json",
];

export async function resolveAliases(ctx: BuildContext): Promise<Record<string, string>> {
export async function resolveAliases(
ctx: BuildContext,
): Promise<Record<string, string>> {
const aliases: Record<string, string> = {
[ctx.pkg.name!]: ctx.options.rootDir,
...ctx.options.alias,
Expand All @@ -40,54 +42,67 @@ export async function resolveAliases(ctx: BuildContext): Promise<Record<string,

/**
* REVIEW: This makes alias resolution asynchronous (which is contagious),
* because we are lazy loading TypeScript,
* because we are lazy loading TypeScript,
* or we can use a synchronous alternative [get-tsconfig](https://github.com/privatenumber/get-tsconfig).
*
*
* Additionally, do we need a flag to explicitly enable this feature?
*/
const tsconfigAliases = await tryInferTsconfigAliases()
if(tsconfigAliases) {
Object.assign(aliases, tsconfigAliases)
const tsconfigAliases = await tryInferTsconfigAliases();
if (tsconfigAliases) {
Object.assign(aliases, tsconfigAliases);
}

return aliases;
}

async function tryInferTsconfigAliases(): Promise<Record<string, string> | null> {
const ts = await import('typescript').catch(() => null)
async function tryInferTsconfigAliases(): Promise<Record<
string,
string
> | null> {
const ts = await import("typescript").catch(() => null);

if(!ts) {
return null
if (!ts) {
return null;
}

const tsconfigPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists, 'tsconfig.json')
const tsconfigPath = ts.findConfigFile(
process.cwd(),
ts.sys.fileExists,
"tsconfig.json",
);

if(!tsconfigPath) {
return null
if (!tsconfigPath) {
return null;
}

const tsconfigDir = dirname(tsconfigPath)
const { config: rawTsconfig } = ts.readConfigFile(tsconfigPath, ts.sys.readFile)
const { options: tsconfig } = ts.parseJsonConfigFileContent(rawTsconfig, ts.sys, tsconfigDir)
const tsconfigDir = dirname(tsconfigPath);
const { config: rawTsconfig } = ts.readConfigFile(
tsconfigPath,
ts.sys.readFile,
);
const { options: tsconfig } = ts.parseJsonConfigFileContent(
rawTsconfig,
ts.sys,
tsconfigDir,
);

if(!tsconfig.paths) {
return null
if (!tsconfig.paths) {
return null;
}

const resolvedBaseUrl = resolve(tsconfigDir, tsconfig.baseUrl || '.');
const resolvedBaseUrl = resolve(tsconfigDir, tsconfig.baseUrl || ".");

const aliases = Object.fromEntries(
Object.entries(tsconfig.paths)
.map(([pattern, substitutions]) => {
const find = pattern.replace(/\/\*$/, '')
// Pick only the first path.
const replacement = substitutions[0].replace(/\*$/, '')
const resolvedReplacement = resolve(resolvedBaseUrl, replacement)
return [find, resolvedReplacement]
})
)
Object.entries(tsconfig.paths).map(([pattern, substitutions]) => {
const find = pattern.replace(/\/\*$/, "");
// Pick only the first path.
const replacement = substitutions[0].replace(/\*$/, "");
const resolvedReplacement = resolve(resolvedBaseUrl, replacement);
return [find, resolvedReplacement];
}),
);

return aliases
return aliases;
}

export function getChunkFilename(
Expand Down
Loading
0