Skip to content

Commit 639a876

Browse files
p0nch000nmn
authored andcommitted
Considered deno.json, fix pr review comments
Fixed flow and prettier tests failing Correct install of json5 dependency fix: Resolve alias paths to absolute paths
1 parent f81b4f1 commit 639a876

File tree

5 files changed

+378
-12
lines changed

5 files changed

+378
-12
lines changed

flow-typed/json5.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// @flow
2+
type TSConfig = {
3+
compilerOptions?: {
4+
baseUrl?: string,
5+
paths?: { [key: string]: Array<string> },
6+
},
7+
};
8+
9+
type DenoConfig = {
10+
imports?: { [key: string]: string | Array<string> },
11+
};
12+
13+
type PackageJSON = {
14+
name?: string,
15+
imports?: { [key: string]: string | Array<string> },
16+
};
17+
18+
type ConfigType = TSConfig | DenoConfig | PackageJSON;
19+
20+
declare module 'json5' {
21+
declare module.exports: {
22+
parse: (input: string) => mixed,
23+
stringify: (
24+
value: mixed,
25+
replacer?: ?Function | ?Array<mixed>,
26+
space?: string | number,
27+
) => string,
28+
};
29+
}

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*
8+
*/
9+
10+
'use strict';
11+
12+
import path from 'path';
13+
import fs from 'fs';
14+
import os from 'os';
15+
import StateManager from '../src/utils/state-manager';
16+
17+
describe('StyleX Alias Configuration', () => {
18+
let tmpDir;
19+
let state;
20+
21+
beforeEach(() => {
22+
// Create a temporary directory for our test files
23+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'stylex-test-'));
24+
25+
// Create a mock babel state
26+
state = {
27+
file: {
28+
metadata: {},
29+
},
30+
filename: path.join(tmpDir, 'src/components/Button.js'),
31+
};
32+
});
33+
34+
afterEach(() => {
35+
// Clean up temporary directory
36+
fs.rmSync(tmpDir, { recursive: true, force: true });
37+
});
38+
39+
const setupFiles = (files) => {
40+
for (const [filePath, content] of Object.entries(files)) {
41+
const fullPath = path.join(tmpDir, filePath);
42+
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
43+
fs.writeFileSync(fullPath, JSON.stringify(content, null, 2));
44+
}
45+
};
46+
47+
test('discovers aliases from package.json imports', () => {
48+
setupFiles({
49+
'package.json': {
50+
name: 'test-package',
51+
imports: {
52+
'#components': './src/components',
53+
'#utils/*': './src/utils/*',
54+
},
55+
},
56+
});
57+
58+
const manager = new StateManager(state);
59+
60+
expect(manager.options.aliases).toEqual({
61+
components: ['./src/components'],
62+
'utils/*': ['./src/utils/*'],
63+
});
64+
});
65+
66+
test('discovers aliases from tsconfig.json', () => {
67+
setupFiles({
68+
'package.json': { name: 'test-package' },
69+
'tsconfig.json': {
70+
compilerOptions: {
71+
baseUrl: '.',
72+
paths: {
73+
'@components/*': ['src/components/*'],
74+
'@utils/*': ['src/utils/*'],
75+
},
76+
},
77+
},
78+
});
79+
80+
const manager = new StateManager(state);
81+
82+
expect(manager.options.aliases).toEqual({
83+
'@components': ['src/components'],
84+
'@utils': ['src/utils'],
85+
});
86+
});
87+
88+
test('discovers aliases from deno.json', () => {
89+
setupFiles({
90+
'package.json': { name: 'test-package' },
91+
'deno.json': {
92+
imports: {
93+
'@components/': './src/components/',
94+
'@utils/': './src/utils/',
95+
},
96+
},
97+
});
98+
99+
const manager = new StateManager(state);
100+
101+
expect(manager.options.aliases).toEqual({
102+
'@components/': ['./src/components/'],
103+
'@utils/': ['./src/utils/'],
104+
});
105+
});
106+
107+
test('merges aliases from all config files', () => {
108+
setupFiles({
109+
'package.json': {
110+
name: 'test-package',
111+
imports: {
112+
'#components': './src/components',
113+
},
114+
},
115+
'tsconfig.json': {
116+
compilerOptions: {
117+
baseUrl: '.',
118+
paths: {
119+
'@utils/*': ['src/utils/*'],
120+
},
121+
},
122+
},
123+
'deno.json': {
124+
imports: {
125+
'@styles/': './src/styles/',
126+
},
127+
},
128+
});
129+
130+
const manager = new StateManager(state);
131+
132+
expect(manager.options.aliases).toEqual({
133+
components: ['./src/components'],
134+
'@utils': ['src/utils'],
135+
'@styles/': ['./src/styles/'],
136+
});
137+
});
138+
139+
test('manual configuration overrides discovered aliases', () => {
140+
setupFiles({
141+
'package.json': {
142+
name: 'test-package',
143+
imports: {
144+
'#components': './src/components',
145+
},
146+
},
147+
});
148+
149+
state.opts = {
150+
aliases: {
151+
components: './custom/path',
152+
},
153+
};
154+
155+
const manager = new StateManager(state);
156+
157+
expect(manager.options.aliases).toEqual({
158+
components: ['./custom/path'],
159+
});
160+
});
161+
162+
test('handles missing configuration files gracefully', () => {
163+
const manager = new StateManager(state);
164+
expect(manager.options.aliases).toBeNull();
165+
});
166+
167+
test('handles invalid JSON files gracefully', () => {
168+
setupFiles({
169+
'package.json': '{invalid json',
170+
'tsconfig.json': '{also invalid',
171+
'deno.json': '{more invalid',
172+
});
173+
174+
const manager = new StateManager(state);
175+
expect(manager.options.aliases).toBeNull();
176+
});
177+
});

packages/babel-plugin/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"@babel/core": "^7.25.8",
2020
"@babel/traverse": "^7.25.7",
2121
"@babel/types": "^7.25.8",
22-
"@dual-bundle/import-meta-resolve": "^4.1.0"
22+
"@dual-bundle/import-meta-resolve": "^4.1.0",
23+
"json5": "^2.2.3"
2324
},
2425
"jest": {
2526
"verbose": true,

0 commit comments

Comments
 (0)