Skip to content

Commit 4c8edd0

Browse files
committed
Merge branch 'feat/infer-alias-from-tsconfig' of github.com:kricsleo/unbuild into feat/infer-alias-from-tsconfig
2 parents 0b9a05d + 91d4f3c commit 4c8edd0

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

src/builders/rollup/config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import { cjsPlugin } from "./plugins/cjs";
1515
import { shebangPlugin } from "./plugins/shebang";
1616
import { DEFAULT_EXTENSIONS, getChunkFilename, resolveAliases } from "./utils";
1717

18-
export async function getRollupOptions(ctx: BuildContext): Promise<RollupOptions> {
18+
export async function getRollupOptions(
19+
ctx: BuildContext,
20+
): Promise<RollupOptions> {
1921
const _aliases = await resolveAliases(ctx);
2022
return (<RollupOptions>{
2123
input: Object.fromEntries(

src/builders/rollup/stub.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export async function rollupStub(ctx: BuildContext): Promise<void> {
1515
{
1616
...ctx.options.stubOptions.jiti,
1717
alias: {
18-
...await resolveAliases(ctx),
18+
...(await resolveAliases(ctx)),
1919
...ctx.options.stubOptions.jiti.alias,
2020
},
2121
transformOptions: {

src/builders/rollup/utils.ts

+44-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dirname, resolve } from 'pathe'
1+
import { dirname, resolve } from "pathe";
22
import type { PreRenderedChunk } from "rollup";
33
import type { BuildContext } from "../../types";
44

@@ -14,7 +14,9 @@ export const DEFAULT_EXTENSIONS: string[] = [
1414
".json",
1515
];
1616

17-
export async function resolveAliases(ctx: BuildContext): Promise<Record<string, string>> {
17+
export async function resolveAliases(
18+
ctx: BuildContext,
19+
): Promise<Record<string, string>> {
1820
const aliases: Record<string, string> = {
1921
[ctx.pkg.name!]: ctx.options.rootDir,
2022
...ctx.options.alias,
@@ -42,52 +44,65 @@ export async function resolveAliases(ctx: BuildContext): Promise<Record<string,
4244
* REVIEW: This makes alias resolution asynchronous (which is contagious),
4345
* because we are dynamic loading TypeScript (cause it's a peer dependency),
4446
* or we can use a synchronous alternative [get-tsconfig](https://github.com/privatenumber/get-tsconfig).
45-
*
47+
*
4648
* Additionally, do we need a flag to explicitly enable this feature?
4749
*/
48-
const tsconfigAliases = await tryInferTsconfigAliases()
49-
if(tsconfigAliases) {
50-
Object.assign(aliases, tsconfigAliases)
50+
const tsconfigAliases = await tryInferTsconfigAliases();
51+
if (tsconfigAliases) {
52+
Object.assign(aliases, tsconfigAliases);
5153
}
5254

5355
return aliases;
5456
}
5557

56-
async function tryInferTsconfigAliases(): Promise<Record<string, string> | null> {
57-
const ts = await import('typescript').catch(() => null)
58+
async function tryInferTsconfigAliases(): Promise<Record<
59+
string,
60+
string
61+
> | null> {
62+
const ts = await import("typescript").catch(() => null);
5863

59-
if(!ts) {
60-
return null
64+
if (!ts) {
65+
return null;
6166
}
6267

63-
const tsconfigPath = ts.findConfigFile(process.cwd(), ts.sys.fileExists, 'tsconfig.json')
68+
const tsconfigPath = ts.findConfigFile(
69+
process.cwd(),
70+
ts.sys.fileExists,
71+
"tsconfig.json",
72+
);
6473

65-
if(!tsconfigPath) {
66-
return null
74+
if (!tsconfigPath) {
75+
return null;
6776
}
6877

69-
const tsconfigDir = dirname(tsconfigPath)
70-
const { config: rawTsconfig } = ts.readConfigFile(tsconfigPath, ts.sys.readFile)
71-
const { options: tsconfig } = ts.parseJsonConfigFileContent(rawTsconfig, ts.sys, tsconfigDir)
78+
const tsconfigDir = dirname(tsconfigPath);
79+
const { config: rawTsconfig } = ts.readConfigFile(
80+
tsconfigPath,
81+
ts.sys.readFile,
82+
);
83+
const { options: tsconfig } = ts.parseJsonConfigFileContent(
84+
rawTsconfig,
85+
ts.sys,
86+
tsconfigDir,
87+
);
7288

73-
if(!tsconfig.paths) {
74-
return null
89+
if (!tsconfig.paths) {
90+
return null;
7591
}
7692

77-
const resolvedBaseUrl = resolve(tsconfigDir, tsconfig.baseUrl || '.');
93+
const resolvedBaseUrl = resolve(tsconfigDir, tsconfig.baseUrl || ".");
7894

7995
const aliases = Object.fromEntries(
80-
Object.entries(tsconfig.paths)
81-
.map(([pattern, substitutions]) => {
82-
const find = pattern.replace(/\/\*$/, '')
83-
// Pick only the first path.
84-
const replacement = substitutions[0].replace(/\*$/, '')
85-
const resolvedReplacement = resolve(resolvedBaseUrl, replacement)
86-
return [find, resolvedReplacement]
87-
})
88-
)
96+
Object.entries(tsconfig.paths).map(([pattern, substitutions]) => {
97+
const find = pattern.replace(/\/\*$/, "");
98+
// Pick only the first path.
99+
const replacement = substitutions[0].replace(/\*$/, "");
100+
const resolvedReplacement = resolve(resolvedBaseUrl, replacement);
101+
return [find, resolvedReplacement];
102+
}),
103+
);
89104

90-
return aliases
105+
return aliases;
91106
}
92107

93108
export function getChunkFilename(

0 commit comments

Comments
 (0)