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
Changes from 2 commits
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
53 changes: 26 additions & 27 deletions src/builders/rollup/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,45 @@ export function resolveAliases(ctx: BuildContext): Record<string, string> {
return aliases;
}

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

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

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

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

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];
}),
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
function getTsconfig(ctx: BuildContext): {
path?: string;
compilerOptions?: CompilerOptions;
} {
const {
tsconfig: overridePath,
compilerOptions: overrideCompilerOptions
} = ctx.options.rollup.dts;
const { tsconfig: overridePath, compilerOptions: overrideCompilerOptions } =
ctx.options.rollup.dts;

const tsconfigPath = overridePath
? resolve(ctx.options.rootDir, overridePath)
Expand All @@ -99,10 +101,7 @@ function getTsconfig(ctx: BuildContext): {
return { compilerOptions: overrideCompilerOptions };
}

const { config: tsconfigRaw } = readConfigFile(
tsconfigPath,
sys.readFile,
);
const { config: tsconfigRaw } = readConfigFile(tsconfigPath, sys.readFile);
const { options: compilerOptions } = parseJsonConfigFileContent(
tsconfigRaw,
sys,
Expand All @@ -112,7 +111,7 @@ function getTsconfig(ctx: BuildContext): {
return {
path: tsconfigPath,
compilerOptions: { ...compilerOptions, ...overrideCompilerOptions },
}
};
}

export function getChunkFilename(
Expand Down
Loading
0