Skip to content
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

feat: infer aliases from tsconfig.json #494

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from
Draft
70 changes: 70 additions & 0 deletions src/builders/rollup/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import { dirname, resolve } from "pathe";
import {
sys,
findConfigFile,
readConfigFile,
parseJsonConfigFileContent,
} from "typescript";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should avoid loading typescript package as it adds runtime overhead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have considered this issue and come up with two alternative solutions:

  1. Implement a lightweight tsconfig.json parsing logic that only parses the paths and baseUrl fields to infer the alias.

  2. Use a lightweight alternative library (e.g., https://github.com/privatenumber/get-tsconfig) (or implement one ourselves) to parse the complete tsconfig.json configuration, which can not only be used for alias inference but also for other configurations, such as automatically passing it to esbuild's tsconfigRaw (which can resolve Decorators support #309), thereby eliminating the need to duplicate existing content from tsconfig.json in configuration files.

I'd love to hear your feedback! 🤔

import type { CompilerOptions } from "typescript";
import type { PreRenderedChunk } from "rollup";
import type { BuildContext } from "../../types";

@@ -15,6 +23,7 @@ export const DEFAULT_EXTENSIONS: string[] = [

export function resolveAliases(ctx: BuildContext): Record<string, string> {
const aliases: Record<string, string> = {
...inferAliasesFromTsconfig(ctx),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be opt-in

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, do you suggest adding a new configuration for this feature?

[ctx.pkg.name!]: ctx.options.rootDir,
...ctx.options.alias,
};
@@ -40,6 +49,67 @@ export function resolveAliases(ctx: BuildContext): Record<string, string> {
return aliases;
}

function inferAliasesFromTsconfig(
ctx: BuildContext,
): Record<string, string> | undefined {
const tsconfig = getTsconfig(ctx);

if (!tsconfig.compilerOptions?.paths) {
return;
}

const tsconfigDir = tsconfig.path
? dirname(tsconfig.path)
: ctx.options.rootDir;

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

const aliases = Object.fromEntries(
Object.entries(tsconfig.compilerOptions.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;
}

function getTsconfig(ctx: BuildContext): {
path?: string;
compilerOptions?: CompilerOptions;
} {
const { tsconfig: overridePath, compilerOptions: overrideCompilerOptions } =
ctx.options.rollup.dts;

const tsconfigPath = overridePath
? resolve(ctx.options.rootDir, overridePath)
: findConfigFile(ctx.options.rootDir, sys.fileExists);

if (!tsconfigPath) {
return { compilerOptions: overrideCompilerOptions };
}

const { config: tsconfigRaw } = readConfigFile(tsconfigPath, sys.readFile);
const { options: compilerOptions } = parseJsonConfigFileContent(
tsconfigRaw,
sys,
dirname(tsconfigPath),
);

return {
path: tsconfigPath,
compilerOptions: { ...compilerOptions, ...overrideCompilerOptions },
};
}

export function getChunkFilename(
ctx: BuildContext,
chunk: PreRenderedChunk,
Loading