@usalyt/cli 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ import { writeFileSync } from "node:fs";
2
+ import { defineCommand } from "citty";
3
+ import {
4
+ CONFIG_FILE,
5
+ ConfigError,
6
+ configPathFor,
7
+ readConfig,
8
+ serializeConfig,
9
+ } from "../lib/config";
10
+ import { printError } from "../lib/output";
11
+ import { UnsafePathError } from "../lib/paths";
12
+ import { detectCssEntry, injectThemeStylesheet } from "../lib/project";
13
+ import { loadRegistry } from "../lib/registry";
14
+ import {
15
+ applyThemePresetToCss,
16
+ DEFAULT_THEME_CONFIG,
17
+ encodeThemePreset,
18
+ hasThemeFlags,
19
+ NAMED_THEME_PRESETS,
20
+ resolveThemeFromFlags,
21
+ ThemeOptionError,
22
+ toUsalytThemeConfig,
23
+ type ThemePresetConfig,
24
+ } from "../lib/theme";
25
+
26
+ const applyThemeCommand = defineCommand({
27
+ meta: {
28
+ name: "apply",
29
+ description: "Apply a theme preset or token overrides to usalyt-theme.css",
30
+ },
31
+ args: {
32
+ preset: {
33
+ type: "string",
34
+ description:
35
+ "Named theme preset (e.g. linear, console-dense, minimal-light) or Theme Studio URL/query",
36
+ },
37
+ base: {
38
+ type: "string",
39
+ description: "Surface base palette (linear, zinc, slate, stone)",
40
+ },
41
+ accent: {
42
+ type: "string",
43
+ description:
44
+ "Primary accent (lavender, cobalt, emerald, amber, crimson, monochrome, or #RRGGBB)",
45
+ },
46
+ radius: {
47
+ type: "string",
48
+ description: "Border radius scale (none, sm, md, lg, xl)",
49
+ },
50
+ density: {
51
+ type: "string",
52
+ description: "Spacing density scale (compact, default, comfortable)",
53
+ },
54
+ font: {
55
+ type: "string",
56
+ description: "Sans font stack (inter, system, geist, jakarta)",
57
+ },
58
+ mode: {
59
+ type: "string",
60
+ description: "Default theme polarity (dark, light)",
61
+ },
62
+ },
63
+ async run({ args }) {
64
+ const root = process.cwd();
65
+
66
+ let config;
67
+ try {
68
+ config = readConfig(root);
69
+ } catch (error) {
70
+ if (error instanceof ConfigError) {
71
+ printError(error.message);
72
+ return;
73
+ }
74
+ throw error;
75
+ }
76
+
77
+ if (!config) {
78
+ printError(
79
+ `No ${CONFIG_FILE} found in ${root}. Run \`usalyt init\` first.`
80
+ );
81
+ return;
82
+ }
83
+
84
+ const flags = {
85
+ preset: args.preset,
86
+ base: args.base,
87
+ accent: args.accent,
88
+ radius: args.radius,
89
+ density: args.density,
90
+ font: args.font,
91
+ mode: args.mode,
92
+ };
93
+
94
+ if (!hasThemeFlags(flags)) {
95
+ printError(
96
+ "Pass --preset <name|query|url> or at least one token flag (--base, --accent, --radius, --density, --font, --mode)."
97
+ );
98
+ return;
99
+ }
100
+
101
+ const currentStart: ThemePresetConfig = config.theme?.preset
102
+ ? resolveThemeFromFlags({ preset: config.theme.preset }, DEFAULT_THEME_CONFIG)
103
+ : DEFAULT_THEME_CONFIG;
104
+
105
+ let resolvedTheme: ThemePresetConfig;
106
+ try {
107
+ resolvedTheme = resolveThemeFromFlags(flags, currentStart);
108
+ } catch (error) {
109
+ if (error instanceof ThemeOptionError) {
110
+ printError(error.message);
111
+ return;
112
+ }
113
+ throw error;
114
+ }
115
+
116
+ const cssEntry = config.tailwind?.css || detectCssEntry(root);
117
+ try {
118
+ const { registry } = loadRegistry();
119
+ const themeItem = registry.items.find((item) => item.name === "theme");
120
+ const themeContent = themeItem?.files[0]?.content;
121
+ if (!themeContent) {
122
+ printError("The packaged registry does not contain a theme stylesheet.");
123
+ return;
124
+ }
125
+
126
+ const finalThemeCss = applyThemePresetToCss(themeContent, resolvedTheme);
127
+ const result = injectThemeStylesheet({
128
+ root,
129
+ cssEntryRelative: cssEntry,
130
+ themeCss: finalThemeCss,
131
+ overwriteTheme: true,
132
+ });
133
+
134
+ const updatedConfig = {
135
+ ...config,
136
+ tailwind: {
137
+ ...config.tailwind,
138
+ css: cssEntry,
139
+ baseColor: resolvedTheme.base,
140
+ },
141
+ theme: toUsalytThemeConfig(resolvedTheme),
142
+ };
143
+ writeFileSync(configPathFor(root), serializeConfig(updatedConfig), "utf8");
144
+
145
+ const presetCode = encodeThemePreset(resolvedTheme);
146
+ console.log(`Applied theme preset "${presetCode}"`);
147
+ console.log(` theme: ${result.themeFile}`);
148
+ console.log(` config: ${CONFIG_FILE}`);
149
+ if (result.cssUpdated) {
150
+ console.log(` updated: ${result.cssEntry}`);
151
+ }
152
+ } catch (error) {
153
+ if (error instanceof UnsafePathError) {
154
+ printError(error.message);
155
+ return;
156
+ }
157
+ printError(`could not apply theme preset: ${(error as Error).message}`);
158
+ }
159
+ },
160
+ });
161
+
162
+ const listThemesCommand = defineCommand({
163
+ meta: {
164
+ name: "list",
165
+ description: "List available curated theme presets",
166
+ },
167
+ args: {
168
+ json: {
169
+ type: "boolean",
170
+ description: "Output theme presets as JSON",
171
+ },
172
+ },
173
+ run({ args }) {
174
+ if (args.json) {
175
+ console.log(JSON.stringify({ presets: NAMED_THEME_PRESETS }, null, 2));
176
+ return;
177
+ }
178
+ console.log("Available Usalyt theme presets:");
179
+ for (const preset of NAMED_THEME_PRESETS) {
180
+ console.log(` ${preset.id.padEnd(18)} ${preset.description}`);
181
+ }
182
+ },
183
+ });
184
+
185
+ export const themeCommand = defineCommand({
186
+ meta: {
187
+ name: "theme",
188
+ description: "List or apply Usalyt theme presets and design token overrides",
189
+ },
190
+ subCommands: {
191
+ apply: applyThemeCommand,
192
+ list: listThemesCommand,
193
+ },
194
+ });
package/src/index.ts CHANGED
@@ -1,19 +1,24 @@
1
1
  #!/usr/bin/env node
2
2
  import { defineCommand, runMain } from "citty";
3
+ import packageJson from "../package.json" with { type: "json" };
3
4
  import { addCommand } from "./commands/add";
5
+ import { diffCommand } from "./commands/diff";
4
6
  import { initCommand } from "./commands/init";
5
7
  import { listCommand } from "./commands/list";
8
+ import { themeCommand } from "./commands/theme";
6
9
 
7
10
  const main = defineCommand({
8
11
  meta: {
9
12
  name: "usalyt",
10
- version: "0.0.1",
13
+ version: packageJson.version,
11
14
  description: "Usalyt component distribution CLI",
12
15
  },
13
16
  subCommands: {
14
17
  init: initCommand,
15
18
  add: addCommand,
19
+ diff: diffCommand,
16
20
  list: listCommand,
21
+ theme: themeCommand,
17
22
  },
18
23
  });
19
24
 
package/src/lib/config.ts CHANGED
@@ -3,6 +3,16 @@ import path from "node:path";
3
3
 
4
4
  export const CONFIG_FILE = "usalyt.json";
5
5
 
6
+ export interface UsalytThemeConfig {
7
+ preset?: string;
8
+ base?: string;
9
+ accent?: string;
10
+ radius?: string;
11
+ density?: string;
12
+ font?: string;
13
+ defaultMode?: string;
14
+ }
15
+
6
16
  export interface UsalytConfig {
7
17
  style?: string;
8
18
  rsc?: boolean;
@@ -12,8 +22,10 @@ export interface UsalytConfig {
12
22
  css?: string;
13
23
  baseColor?: string;
14
24
  };
25
+ theme?: UsalytThemeConfig;
15
26
  aliases: {
16
27
  components: string;
28
+ blocks?: string;
17
29
  utils: string;
18
30
  };
19
31
  }
@@ -84,6 +96,20 @@ export function readConfig(root: string): UsalytConfig | null {
84
96
  }
85
97
 
86
98
  const defaults = defaultConfig();
99
+ const parsedTheme = isRecord(parsed.theme)
100
+ ? {
101
+ ...(typeof parsed.theme.preset === "string" ? { preset: parsed.theme.preset } : {}),
102
+ ...(typeof parsed.theme.base === "string" ? { base: parsed.theme.base } : {}),
103
+ ...(typeof parsed.theme.accent === "string" ? { accent: parsed.theme.accent } : {}),
104
+ ...(typeof parsed.theme.radius === "string" ? { radius: parsed.theme.radius } : {}),
105
+ ...(typeof parsed.theme.density === "string" ? { density: parsed.theme.density } : {}),
106
+ ...(typeof parsed.theme.font === "string" ? { font: parsed.theme.font } : {}),
107
+ ...(typeof parsed.theme.defaultMode === "string"
108
+ ? { defaultMode: parsed.theme.defaultMode }
109
+ : {}),
110
+ }
111
+ : undefined;
112
+
87
113
  return {
88
114
  style: typeof parsed.style === "string" ? parsed.style : defaults.style,
89
115
  rsc: typeof parsed.rsc === "boolean" ? parsed.rsc : defaults.rsc,
@@ -99,8 +125,12 @@ export function readConfig(root: string): UsalytConfig | null {
99
125
  : defaults.tailwind!.baseColor,
100
126
  }
101
127
  : defaults.tailwind,
128
+ ...(parsedTheme && Object.keys(parsedTheme).length > 0 ? { theme: parsedTheme } : {}),
102
129
  aliases: {
103
130
  components: requireAlias(parsed.aliases, "components"),
131
+ ...(typeof parsed.aliases.blocks === "string" && parsed.aliases.blocks.trim() !== ""
132
+ ? { blocks: parsed.aliases.blocks }
133
+ : {}),
104
134
  utils: requireAlias(parsed.aliases, "utils"),
105
135
  },
106
136
  };
@@ -1,4 +1,5 @@
1
1
  import path from "node:path";
2
+ import type { TsconfigPathMapping } from "./project";
2
3
 
3
4
  const IMPORT_PATTERN = /(\bfrom\s*|\bimport\s*)(["'])([^"'\n]+)\2/g;
4
5
 
@@ -51,7 +52,29 @@ function pathPosixDirname(value: string): string {
51
52
  return index === -1 ? "" : value.slice(0, index);
52
53
  }
53
54
 
54
- function toImportSpecifier(fromFile: string, toFile: string): string {
55
+ function toImportSpecifier(
56
+ fromFile: string,
57
+ toFile: string,
58
+ root?: string,
59
+ tsconfigPaths?: TsconfigPathMapping | null
60
+ ): string {
61
+ if (root && tsconfigPaths) {
62
+ const relToRoot = path.relative(root, toFile).split(path.sep).join("/");
63
+ const baseDir = tsconfigPaths.targetDir;
64
+ const withinTarget =
65
+ baseDir === ""
66
+ ? !relToRoot.startsWith("..")
67
+ : relToRoot.startsWith(`${baseDir}/`);
68
+
69
+ if (withinTarget) {
70
+ const subPath = baseDir === "" ? relToRoot : relToRoot.slice(baseDir.length + 1);
71
+ const stripped = subPath
72
+ .replace(/\.(tsx?|jsx?|mts?|cts?|mjs?|cjs?)$/, "")
73
+ .replace(/\/index$/, "");
74
+ return `${tsconfigPaths.prefix}${stripped}`;
75
+ }
76
+ }
77
+
55
78
  const relative = path.relative(path.dirname(fromFile), toFile).split(path.sep).join("/");
56
79
  let specifier = relative.replace(/\.(tsx?|jsx?|mts?|cts?|mjs?|cjs?)$/, "");
57
80
  specifier = specifier.replace(/\/index$/, "");
@@ -63,16 +86,19 @@ function toImportSpecifier(fromFile: string, toFile: string): string {
63
86
 
64
87
  /**
65
88
  * Rewrite registry-internal relative imports so references between installed
66
- * files keep resolving at their configured destinations. Bare package imports
67
- * are left untouched.
89
+ * files keep resolving at their configured destinations. Uses the consumer's
90
+ * tsconfig path alias when available, falling back to relative paths. Bare
91
+ * package imports are left untouched.
68
92
  */
69
93
  export function rewriteRelativeImports(options: {
70
94
  content: string;
71
95
  sourcePath: string;
72
96
  destPath: string;
73
97
  sourceToDest: ReadonlyMap<string, string>;
98
+ root?: string;
99
+ tsconfigPaths?: TsconfigPathMapping | null;
74
100
  }): string {
75
- const { content, sourcePath, destPath, sourceToDest } = options;
101
+ const { content, sourcePath, destPath, sourceToDest, root, tsconfigPaths } = options;
76
102
  const knownSources = new Set(sourceToDest.keys());
77
103
 
78
104
  return content.replace(
@@ -86,7 +112,7 @@ export function rewriteRelativeImports(options: {
86
112
  if (!targetDest || targetDest === destPath) {
87
113
  return match;
88
114
  }
89
- return `${prefix}${quote}${toImportSpecifier(destPath, targetDest)}${quote}`;
115
+ return `${prefix}${quote}${toImportSpecifier(destPath, targetDest, root, tsconfigPaths)}${quote}`;
90
116
  }
91
117
  );
92
118
  }
@@ -3,6 +3,7 @@ import path from "node:path";
3
3
  import type { UsalytConfig } from "./config";
4
4
  import { rewriteRelativeImports } from "./imports";
5
5
  import { assertWithinRoot, isWithinRoot, resolveAliasWithinRoot, UnsafePathError } from "./paths";
6
+ import { detectTsconfigPathMapping, type TsconfigPathMapping } from "./project";
6
7
  import type { Registry, RegistryItem } from "./registry";
7
8
 
8
9
  export class UnknownItemsError extends Error {
@@ -31,6 +32,7 @@ export interface PlannedWrite {
31
32
 
32
33
  export interface InstallPlan {
33
34
  writes: PlannedWrite[];
35
+ skipped: PlannedWrite[];
34
36
  dependencies: string[];
35
37
  conflicts: string[];
36
38
  }
@@ -38,6 +40,7 @@ export interface InstallPlan {
38
40
  const SAFE_ITEM_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
39
41
  const SAFE_FILE_BASENAME = /^[A-Za-z0-9._-]+$/;
40
42
  const REWRITE_EXTENSIONS = /\.(tsx?|jsx?|mts?|cts?)$/;
43
+ const USE_CLIENT_DIRECTIVE = /^\s*["']use client["'];?\r?\n\r?\n?/;
41
44
 
42
45
  function collectRegistryDependencies(
43
46
  targets: string[],
@@ -75,10 +78,35 @@ function extensionOf(alias: string): string {
75
78
  return path.extname(alias);
76
79
  }
77
80
 
81
+ function transformSourceContent(options: {
82
+ content: string;
83
+ sourcePath: string;
84
+ destPath: string;
85
+ sourceToDest: ReadonlyMap<string, string>;
86
+ root: string;
87
+ rsc: boolean;
88
+ tsconfigPaths: TsconfigPathMapping | null;
89
+ }): string {
90
+ let result = rewriteRelativeImports({
91
+ content: options.content,
92
+ sourcePath: options.sourcePath,
93
+ destPath: options.destPath,
94
+ sourceToDest: options.sourceToDest,
95
+ root: options.root,
96
+ tsconfigPaths: options.tsconfigPaths,
97
+ });
98
+ if (!options.rsc) {
99
+ result = result.replace(USE_CLIENT_DIRECTIVE, "");
100
+ }
101
+ return result;
102
+ }
103
+
78
104
  /**
79
105
  * Build every destination and its rewritten content before touching the
80
106
  * filesystem. Unknown items, unsafe destinations, directory collisions, and
81
- * conflicts are all detected here so a rejected request writes nothing.
107
+ * conflicts on explicitly requested items are detected before any write.
108
+ * Existing transitive registry dependencies are skipped rather than failing
109
+ * subsequent installs.
82
110
  */
83
111
  export function buildInstallPlan(options: {
84
112
  registry: Registry;
@@ -86,8 +114,14 @@ export function buildInstallPlan(options: {
86
114
  root: string;
87
115
  targets: string[];
88
116
  overwrite: boolean;
117
+ tsconfigPaths?: TsconfigPathMapping | null;
89
118
  }): InstallPlan {
90
119
  const { registry, config, root, targets, overwrite } = options;
120
+ const tsconfigPaths =
121
+ options.tsconfigPaths !== undefined
122
+ ? options.tsconfigPaths
123
+ : detectTsconfigPathMapping(root);
124
+ const requestedSet = new Set(targets);
91
125
  const itemsByName = new Map(registry.items.map((item) => [item.name, item]));
92
126
  const unknown = targets.filter((target) => !itemsByName.has(target));
93
127
  if (unknown.length > 0) {
@@ -95,18 +129,34 @@ export function buildInstallPlan(options: {
95
129
  }
96
130
 
97
131
  const plannedNames = collectRegistryDependencies(targets, itemsByName);
98
- const componentsDir = resolveAliasWithinRoot(root, config.aliases.components, "components");
132
+ const componentsDir = resolveAliasWithinRoot(
133
+ root,
134
+ config.aliases.components,
135
+ "components",
136
+ tsconfigPaths
137
+ );
138
+ const blocksDir = config.aliases.blocks
139
+ ? resolveAliasWithinRoot(root, config.aliases.blocks, "blocks", tsconfigPaths)
140
+ : componentsDir;
99
141
 
100
142
  const utilsAlias = config.aliases.utils;
101
- let utilsDestination = resolveAliasWithinRoot(root, utilsAlias, "utils");
143
+ let utilsDestination = resolveAliasWithinRoot(root, utilsAlias, "utils", tsconfigPaths);
102
144
  const utilsExtension = extensionOf(utilsAlias);
103
145
  if (utilsExtension === "") {
104
146
  utilsDestination = `${utilsDestination}.ts`;
105
147
  assertWithinRoot(root, utilsDestination, "utils");
106
148
  }
107
149
 
150
+ const cssEntryRelative = (config.tailwind?.css || "src/styles/globals.css")
151
+ .replace(/\\/g, "/")
152
+ .replace(/^\.?\//, "");
153
+ const themeDestination = path.join(
154
+ path.dirname(path.resolve(root, cssEntryRelative)),
155
+ "usalyt-theme.css"
156
+ );
157
+
108
158
  const sourceToDest = new Map<string, string>();
109
- const destinationOwner = new Map<string, string>();
159
+ const destinationOwner = new Map<string, { sourcePath: string; itemName: string }>();
110
160
 
111
161
  for (const name of plannedNames) {
112
162
  const item = itemsByName.get(name)!;
@@ -132,6 +182,10 @@ export function buildInstallPlan(options: {
132
182
  );
133
183
  }
134
184
  destination = utilsDestination;
185
+ } else if (file.path.startsWith("styles/")) {
186
+ destination = themeDestination;
187
+ } else if (item.type === "registry:block") {
188
+ destination = path.join(blocksDir, item.name, baseName);
135
189
  } else {
136
190
  destination = path.join(componentsDir, item.name, baseName);
137
191
  }
@@ -139,18 +193,20 @@ export function buildInstallPlan(options: {
139
193
  assertWithinRoot(root, destination, `"${item.name}"`);
140
194
 
141
195
  const owner = destinationOwner.get(destination);
142
- if (owner !== undefined && owner !== file.path) {
196
+ if (owner !== undefined && owner.sourcePath !== file.path) {
143
197
  throw new UnsupportedItemError(
144
- `Registry files "${owner}" and "${file.path}" resolve to the same destination: ${destination}`
198
+ `Registry files "${owner.sourcePath}" and "${file.path}" resolve to the same destination: ${destination}`
145
199
  );
146
200
  }
147
- destinationOwner.set(destination, file.path);
201
+ destinationOwner.set(destination, { sourcePath: file.path, itemName: item.name });
148
202
  sourceToDest.set(file.path, destination);
149
203
  }
150
204
  }
151
205
 
152
206
  const conflicts: string[] = [];
153
- for (const destination of destinationOwner.keys()) {
207
+ const skippedDestinations = new Set<string>();
208
+
209
+ for (const [destination, owner] of destinationOwner.entries()) {
154
210
  let entry;
155
211
  try {
156
212
  entry = lstatSync(destination);
@@ -170,7 +226,7 @@ export function buildInstallPlan(options: {
170
226
  const realRoot = realpathSync(root);
171
227
  if (!isWithinRoot(realRoot, realDestination)) {
172
228
  throw new UnsafePathError(
173
- destinationOwner.get(destination) ?? "destination",
229
+ owner.sourcePath,
174
230
  destination,
175
231
  `The destination "${destination}" resolves outside the consumer root ${root} through a symbolic link.`
176
232
  );
@@ -183,29 +239,44 @@ export function buildInstallPlan(options: {
183
239
  );
184
240
  }
185
241
  if (!overwrite) {
186
- conflicts.push(relativeDisplayPath(root, destination));
242
+ if (requestedSet.has(owner.itemName)) {
243
+ conflicts.push(relativeDisplayPath(root, destination));
244
+ } else {
245
+ skippedDestinations.add(destination);
246
+ }
187
247
  }
188
248
  }
189
249
 
250
+ const rsc = config.rsc !== false;
190
251
  const writes: PlannedWrite[] = [];
252
+ const skipped: PlannedWrite[] = [];
253
+
191
254
  for (const name of plannedNames) {
192
255
  const item = itemsByName.get(name)!;
193
256
  for (const file of item.files) {
194
257
  const destination = sourceToDest.get(file.path)!;
195
258
  const content = REWRITE_EXTENSIONS.test(file.path)
196
- ? rewriteRelativeImports({
259
+ ? transformSourceContent({
197
260
  content: file.content,
198
261
  sourcePath: file.path,
199
262
  destPath: destination,
200
263
  sourceToDest,
264
+ root,
265
+ rsc,
266
+ tsconfigPaths,
201
267
  })
202
268
  : file.content;
203
- writes.push({
269
+ const planned: PlannedWrite = {
204
270
  item: item.name,
205
271
  destination,
206
272
  relativeDestination: relativeDisplayPath(root, destination),
207
273
  content,
208
- });
274
+ };
275
+ if (skippedDestinations.has(destination)) {
276
+ skipped.push(planned);
277
+ } else {
278
+ writes.push(planned);
279
+ }
209
280
  }
210
281
  }
211
282
 
@@ -219,6 +290,7 @@ export function buildInstallPlan(options: {
219
290
 
220
291
  return {
221
292
  writes,
293
+ skipped,
222
294
  dependencies: [...dependencies].sort(),
223
295
  conflicts: conflicts.sort(),
224
296
  };
package/src/lib/paths.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { existsSync, realpathSync } from "node:fs";
2
2
  import path from "node:path";
3
+ import type { TsconfigPathMapping } from "./project";
3
4
 
4
5
  export class UnsafePathError extends Error {
5
6
  readonly destination: string;
@@ -15,15 +16,24 @@ export class UnsafePathError extends Error {
15
16
 
16
17
  const WINDOWS_ABSOLUTE = /^[A-Za-z]:[\\/]/;
17
18
 
18
- /** Strip the `@/` or `~/` prefix and return the root-relative alias path. */
19
- export function aliasToRelativePath(alias: string, label: string): string {
19
+ /** Strip the configured prefix (or `@/`/`~/`) and return the root-relative alias path. */
20
+ export function aliasToRelativePath(
21
+ alias: string,
22
+ label: string,
23
+ tsconfigPaths?: TsconfigPathMapping | null
24
+ ): string {
20
25
  if (typeof alias !== "string" || alias.trim() === "") {
21
26
  throw new UnsafePathError(alias, "", `The ${label} alias must be a non-empty string.`);
22
27
  }
23
28
 
24
29
  const original = alias.trim();
25
30
  let value = original;
26
- if (value.startsWith("@/") || value.startsWith("~/")) {
31
+ let matchedPrefix: string | null = null;
32
+ if (tsconfigPaths && value.startsWith(tsconfigPaths.prefix)) {
33
+ matchedPrefix = tsconfigPaths.prefix;
34
+ value = value.slice(tsconfigPaths.prefix.length);
35
+ } else if (value.startsWith("@/") || value.startsWith("~/")) {
36
+ matchedPrefix = value.slice(0, 2);
27
37
  value = value.slice(2);
28
38
  }
29
39
 
@@ -42,6 +52,17 @@ export function aliasToRelativePath(alias: string, label: string): string {
42
52
  );
43
53
  }
44
54
 
55
+ if (
56
+ matchedPrefix &&
57
+ tsconfigPaths &&
58
+ matchedPrefix === tsconfigPaths.prefix &&
59
+ tsconfigPaths.targetDir !== "" &&
60
+ value !== tsconfigPaths.targetDir &&
61
+ !value.startsWith(`${tsconfigPaths.targetDir}/`)
62
+ ) {
63
+ value = path.posix.join(tsconfigPaths.targetDir, value);
64
+ }
65
+
45
66
  return value === "" ? "." : value;
46
67
  }
47
68
 
@@ -91,8 +112,13 @@ export function assertWithinRoot(root: string, destination: string, label: strin
91
112
  }
92
113
 
93
114
  /** Resolve a configured alias against the consumer root and enforce containment. */
94
- export function resolveAliasWithinRoot(root: string, alias: string, label: string): string {
95
- const relative = aliasToRelativePath(alias, label);
115
+ export function resolveAliasWithinRoot(
116
+ root: string,
117
+ alias: string,
118
+ label: string,
119
+ tsconfigPaths?: TsconfigPathMapping | null
120
+ ): string {
121
+ const relative = aliasToRelativePath(alias, label, tsconfigPaths);
96
122
  const destination = path.resolve(root, relative);
97
123
  assertWithinRoot(root, destination, label);
98
124
  return destination;