Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit 5e23dc3

Browse files
fix: require flag to handle tsconfig aliases (#29)
1 parent 99d0afc commit 5e23dc3

File tree

6 files changed

+53
-7
lines changed

6 files changed

+53
-7
lines changed

src/@types/module.d.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ declare module 'module' {
1212
export const _extensions: NodeJS.RequireExtensions;
1313
export function _resolveFilename(
1414
request: string,
15-
parent: any,
15+
parent: {
16+
17+
/**
18+
* Can be null if the parent id is 'internal/preload' (e.g. via --require)
19+
* which doesn't have a file path.
20+
*/
21+
filename: string | null;
22+
},
1623
isMain: boolean,
1724
options?: any,
1825
): string;

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Module._resolveFilename = function (request, parent, isMain, options) {
136136
&& !isPathPattern.test(request)
137137

138138
// Dependency paths should not be resolved using tsconfig.json
139-
&& !parent?.filename.includes(nodeModulesPath)
139+
&& !parent?.filename?.includes(nodeModulesPath)
140140
) {
141141
const possiblePaths = tsconfigPathsMatcher(request);
142142

Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export default 'resolve-target';
1+
console.log('resolve-target loaded');
2+
export default 'resolve-target value';

tests/specs/typescript/ts.ts

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
5454
assertResults(nodeProcess.stdout);
5555
expect(nodeProcess.stdout).toMatch('{"default":1234}');
5656
});
57+
58+
test('Require flag', async () => {
59+
const nodeProcess = await node.requireFlag(importPath);
60+
assertResults(nodeProcess.stdout);
61+
});
5762
});
5863

5964
describe('full path via .js', ({ test }) => {

tests/specs/typescript/tsconfig.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,21 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
2626
const nodeProcess = await node.load('./src/base-url.ts', {
2727
cwd: './tsconfig',
2828
});
29-
expect(nodeProcess.stdout).toBe('resolve-target');
29+
expect(nodeProcess.stdout).toBe('resolve-target loaded\nresolve-target value');
30+
});
31+
32+
test('Require flag', async () => {
33+
const nodeProcess = await node.requireFlag('resolve-target', {
34+
cwd: './tsconfig',
35+
});
36+
expect(nodeProcess.stdout).toMatch('resolve-target loaded');
3037
});
3138

3239
test('resolves paths exact match', async () => {
3340
const nodeProcess = await node.load('./src/paths-exact-match.ts', {
3441
cwd: './tsconfig',
3542
});
36-
expect(nodeProcess.stdout).toBe('resolve-target');
43+
expect(nodeProcess.stdout).toBe('resolve-target loaded\nresolve-target value');
3744
});
3845

3946
test('resolves paths prefix', async () => {

tests/utils/node-with-loader.ts

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'fs/promises';
22
import path from 'path';
3-
import { execaNode } from 'execa';
3+
import { execaNode, execa } from 'execa';
44
import getNode from 'get-node';
55

66
type Options = {
@@ -11,6 +11,8 @@ type Options = {
1111
nodeOptions?: string[];
1212
};
1313

14+
const cjsLoaderPath = path.resolve(__dirname, '../..');
15+
1416
export const nodeWithLoader = async (
1517
options: Options,
1618
) => await execaNode(
@@ -25,7 +27,7 @@ export const nodeWithLoader = async (
2527
...(options.nodeOptions ?? []),
2628

2729
'--require',
28-
path.resolve(__dirname, '../..'),
30+
cjsLoaderPath,
2931
],
3032
nodePath: options.nodePath,
3133
cwd: options.cwd,
@@ -129,6 +131,30 @@ export async function createNode(
129131
cwd: fixturePath,
130132
});
131133
},
134+
requireFlag(
135+
filePath: string,
136+
options?: {
137+
cwd?: string;
138+
},
139+
) {
140+
return execa(
141+
node.path,
142+
[
143+
'--require',
144+
cjsLoaderPath,
145+
'--require',
146+
filePath,
147+
'--eval',
148+
'null',
149+
],
150+
{
151+
cwd: path.join(fixturePath, options?.cwd ?? ''),
152+
env: {
153+
ESBK_DISABLE_CACHE: '1',
154+
},
155+
},
156+
);
157+
},
132158
};
133159
}
134160

0 commit comments

Comments
 (0)