Skip to content

Commit

Permalink
Merge branch 'feat/infer-alias-from-tsconfig' of github.com:kricsleo/…
Browse files Browse the repository at this point in the history
…unbuild into feat/infer-alias-from-tsconfig
  • Loading branch information
kricsleo committed Jan 25, 2025
2 parents 0b9a05d + 91d4f3c commit 4c8edd0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
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
73 changes: 44 additions & 29 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 Down Expand Up @@ -42,52 +44,65 @@ export async function resolveAliases(ctx: BuildContext): Promise<Record<string,
* REVIEW: This makes alias resolution asynchronous (which is contagious),
* because we are dynamic loading TypeScript (cause it's a peer dependency),
* 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

0 comments on commit 4c8edd0

Please sign in to comment.