Skip to content

Commit a4654f6

Browse files
committed
fix: Resolve alias paths to absolute paths
1 parent 12bca76 commit a4654f6

File tree

5 files changed

+43
-53
lines changed

5 files changed

+43
-53
lines changed

apps/nextjs-example/app/Counter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use client';
1111

1212
import * as stylex from '@stylexjs/stylex';
13-
import { spacing, text, globalTokens as $ } from './globalTokens.stylex';
13+
import { spacing, text, globalTokens as $ } from '@/app/globalTokens.stylex';
1414
import { colors } from '@stylexjs/open-props/lib/colors.stylex';
1515
import { useState } from 'react';
1616

apps/nextjs-example/babel.config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
*
88
*/
9+
910
const path = require('path');
1011
module.exports = {
1112
presets: ['next/babel'],
@@ -19,9 +20,9 @@ module.exports = {
1920
runtimeInjection: false,
2021
genConditionalClasses: true,
2122
treeshakeCompensation: true,
22-
aliases: {
23-
'@/*': [path.join(__dirname, '*')],
24-
},
23+
// aliases: {
24+
// '@/*': [path.join(__dirname, '*')],
25+
// },
2526
unstable_moduleResolution: {
2627
type: 'commonJS',
2728
},

apps/nextjs-example/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"version": "0.0.0",
44
"private": true,
55
"scripts": {
6-
"clean": "rimraf .next",
76
"dev": "next dev",
87
"build": "next build",
98
"start": "next start",
@@ -17,7 +16,7 @@
1716
"next": "14.0.1"
1817
},
1918
"devDependencies": {
20-
"@stylexjs/eslint-plugin": "^0.7.5",
19+
"@stylexjs/eslint-plugin": "^0.10.1",
2120
"@stylexjs/postcss-plugin": "0.10.1",
2221
"@types/node": "^22.7.6",
2322
"@types/react": "^18.3.11",

apps/nextjs-example/tsconfig.json

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
{
22
"compilerOptions": {
33
"target": "es5",
4-
"lib": [
5-
"dom",
6-
"dom.iterable",
7-
"esnext"
8-
],
4+
"lib": ["dom", "dom.iterable", "esnext"],
95
"allowJs": true,
106
"skipLibCheck": true,
117
"strict": true,
@@ -23,18 +19,9 @@
2319
}
2420
],
2521
"paths": {
26-
"@/*": [
27-
"./*"
28-
],
22+
"@/*": ["./*"]
2923
}
3024
},
31-
"include": [
32-
"next-env.d.ts",
33-
"**/*.ts",
34-
"**/*.tsx",
35-
".next/types/**/*.ts"
36-
],
37-
"exclude": [
38-
"node_modules"
39-
]
40-
}
25+
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
26+
"exclude": ["node_modules"]
27+
}

packages/babel-plugin/src/utils/state-manager.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,19 @@ export default class StateManager {
640640

641641
const [_packageName, projectDir] = pkgInfo;
642642

643+
const resolveAliasPaths = (value: string | $ReadOnlyArray<string>) =>
644+
(Array.isArray(value) ? value : [value]).map((p) => {
645+
const endsWithStar = p.endsWith('/*');
646+
let basePath = p.replace(/\/\*$/, '');
647+
if (basePath.startsWith('.')) {
648+
basePath = path.resolve(projectDir, basePath);
649+
}
650+
if (endsWithStar) {
651+
basePath = basePath.endsWith('/') ? basePath + '*' : basePath + '/*';
652+
}
653+
return basePath;
654+
});
655+
643656
// Load aliases from package.json imports field
644657
try {
645658
const packageJsonPath = path.join(projectDir, 'package.json');
@@ -658,10 +671,7 @@ export default class StateManager {
658671
packageAliases = Object.fromEntries(
659672
Object.entries(imports)
660673
.filter(([key]) => key.startsWith('#'))
661-
.map(([key, value]) => [
662-
key.slice(1),
663-
Array.isArray(value) ? value : [value],
664-
]),
674+
.map(([key, value]) => [key.slice(1), resolveAliasPaths(value)]),
665675
);
666676
}
667677
}
@@ -680,25 +690,13 @@ export default class StateManager {
680690
throw new Error('Invalid tsconfig.json format');
681691
}
682692
const tsconfig: TSConfig = rawConfig as $FlowFixMe;
683-
const baseUrl = tsconfig.compilerOptions?.baseUrl || '.';
684-
if (tsconfig.compilerOptions?.paths) {
693+
const tsConfigAliasPaths = tsconfig.compilerOptions?.paths;
694+
if (tsConfigAliasPaths != null && isImportsObject(tsConfigAliasPaths)) {
685695
tsconfigAliases = Object.fromEntries(
686-
Object.entries(tsconfig.compilerOptions.paths).map(
687-
([key, value]) => [
688-
key.replace(/\/\*$/, ''),
689-
Array.isArray(value)
690-
? value.map((p) =>
691-
this.normalizePath(
692-
path.join(baseUrl, p.replace(/\/\*$/, '')),
693-
),
694-
)
695-
: [
696-
this.normalizePath(
697-
path.join(baseUrl, value.replace(/\/\*$/, '')),
698-
),
699-
],
700-
],
701-
),
696+
Object.entries(tsConfigAliasPaths).map(([key, value]) => [
697+
key,
698+
resolveAliasPaths(value),
699+
]),
702700
);
703701
}
704702
}
@@ -719,10 +717,9 @@ export default class StateManager {
719717
const denoConfig: DenoConfig = rawConfig as $FlowFixMe;
720718
if (denoConfig.imports) {
721719
denoAliases = Object.fromEntries(
722-
Object.entries(denoConfig.imports).map(([key, value]) => [
723-
key,
724-
Array.isArray(value) ? value : [value],
725-
]),
720+
Object.entries(denoConfig.imports).map(([key, value]) => {
721+
return [key, resolveAliasPaths(value)];
722+
}),
726723
);
727724
}
728725
}
@@ -899,15 +896,21 @@ function formatRelativePath(filePath: string) {
899896
return filePath.startsWith('.') ? filePath : './' + filePath;
900897
}
901898

902-
function isPackageJSON(obj: mixed): boolean %checks {
899+
function isImportsObject(obj: mixed): implies obj is { ... } {
900+
return obj != null && typeof obj === 'object';
901+
}
902+
903+
function isPackageJSON(obj: mixed): implies obj is { +imports: mixed, ... } {
903904
return (
904905
obj != null &&
905906
typeof obj === 'object' &&
906907
(!('imports' in obj) || typeof obj.imports === 'object')
907908
);
908909
}
909910

910-
function isTSConfig(obj: mixed): boolean {
911+
function isTSConfig(
912+
obj: mixed,
913+
): implies obj is { +compilerOptions: mixed, ... } {
911914
return (
912915
obj != null &&
913916
typeof obj === 'object' &&
@@ -917,7 +920,7 @@ function isTSConfig(obj: mixed): boolean {
917920
);
918921
}
919922

920-
function isDenoConfig(obj: mixed): boolean {
923+
function isDenoConfig(obj: mixed): implies obj is { +imports: mixed, ... } {
921924
return (
922925
obj != null &&
923926
typeof obj === 'object' &&

0 commit comments

Comments
 (0)