Skip to content

Commit 655c9c2

Browse files
committed
refactor: wip
1 parent 8ca4ab1 commit 655c9c2

File tree

6 files changed

+57
-9
lines changed

6 files changed

+57
-9
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const helperValue = 'helper-module-value';
2+
export default helperValue;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"paths": {
4+
"@test/helper": ["./helper-module.ts"]
5+
}
6+
}
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// eslint-disable-next-line import/no-unresolved
2+
import helperValue from '@test/helper';
3+
4+
const config = {
5+
value: helperValue,
6+
};
7+
8+
export default config;

packages/utils/src/lib/import-module.int.test.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('importModule', () => {
4747
it('should throw if the file does not exist', async () => {
4848
await expect(
4949
importModule({ filepath: 'path/to/non-existent-export.mjs' }),
50-
).rejects.toThrow("File 'path/to/non-existent-export.mjs' does not exist");
50+
).rejects.toThrow(/path\/to\/non-existent-export\.mjs' does not exist$/);
5151
});
5252

5353
it('should throw if path is a directory', async () => {
@@ -59,8 +59,17 @@ describe('importModule', () => {
5959
it('should throw if file is not valid JS', async () => {
6060
await expect(
6161
importModule({ filepath: path.join(mockDir, 'invalid-js-file.json') }),
62-
).rejects.toThrow(
63-
`${path.join(mockDir, 'invalid-js-file.json')} is not a valid JS file`,
64-
);
62+
).rejects.toThrow(/invalid-js-file\.json/);
63+
});
64+
65+
it('should load a TS module using tsconfig paths', async () => {
66+
await expect(
67+
importModule({
68+
filepath: path.join(mockDir, 'uses-path-alias.ts'),
69+
tsconfig: path.join(mockDir, 'tsconfig-paths-test.json'),
70+
}),
71+
).resolves.toEqual({
72+
value: 'helper-module-value',
73+
});
6574
});
6675
});

packages/utils/src/lib/import-module.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,25 @@ export async function createTsJiti(
183183
? await jitiOptionsFromTsConfig(validPath)
184184
: {};
185185

186+
const mergedAlias = {
187+
...jitiOptions.alias,
188+
...tsDerivedJitiOptions.alias,
189+
};
190+
186191
return createJiti(id, {
187192
...jitiOptions,
188193
...tsDerivedJitiOptions,
189-
alias: {
190-
...jitiOptions.alias,
191-
...tsDerivedJitiOptions.alias,
192-
},
194+
alias: mergedAlias,
193195
nativeModules: [
194196
...new Set([
195197
...JITI_NATIVE_MODULES,
196198
...(jitiOptions.nativeModules ?? []),
197199
]),
198200
],
199-
tryNative: true,
201+
// Don't use native imports when we have aliases to resolve
202+
// When tryNative is true, jiti attempts to use native Node.js imports first,
203+
// which don't understand path aliases and will fail with ERR_MODULE_NOT_FOUND
204+
tryNative: Object.keys(mergedAlias).length === 0,
200205
});
201206
}
202207

packages/utils/src/lib/import-module.unit.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ describe('parseTsConfigToJitiConfig', () => {
109109
jsx: true,
110110
});
111111
});
112+
113+
it('handles path alias without wildcards and no baseUrl', () => {
114+
const compilerOptions: CompilerOptions = {
115+
paths: {
116+
'@test/helper': ['./helper-module.ts'],
117+
},
118+
};
119+
120+
const tsconfigDir = '/Users/test/project/mocks';
121+
const result = parseTsConfigToJitiConfig(compilerOptions, tsconfigDir);
122+
123+
expect(result).toStrictEqual({
124+
alias: {
125+
'@test/helper': expect.pathToEndWith('mocks/helper-module.ts'),
126+
},
127+
});
128+
});
112129
});
113130

114131
describe('toFileUrl', () => {

0 commit comments

Comments
 (0)