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

Commit

Permalink
fix: require flag to handle tsconfig aliases (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Nov 22, 2022
1 parent 99d0afc commit 5e23dc3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/@types/module.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ declare module 'module' {
export const _extensions: NodeJS.RequireExtensions;
export function _resolveFilename(
request: string,
parent: any,
parent: {

/**
* Can be null if the parent id is 'internal/preload' (e.g. via --require)
* which doesn't have a file path.
*/
filename: string | null;
},
isMain: boolean,
options?: any,
): string;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Module._resolveFilename = function (request, parent, isMain, options) {
&& !isPathPattern.test(request)

// Dependency paths should not be resolved using tsconfig.json
&& !parent?.filename.includes(nodeModulesPath)
&& !parent?.filename?.includes(nodeModulesPath)
) {
const possiblePaths = tsconfigPathsMatcher(request);

Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/tsconfig/src/resolve-target.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export default 'resolve-target';
console.log('resolve-target loaded');
export default 'resolve-target value';
5 changes: 5 additions & 0 deletions tests/specs/typescript/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});

test('Require flag', async () => {
const nodeProcess = await node.requireFlag(importPath);
assertResults(nodeProcess.stdout);
});
});

describe('full path via .js', ({ test }) => {
Expand Down
11 changes: 9 additions & 2 deletions tests/specs/typescript/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,21 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
const nodeProcess = await node.load('./src/base-url.ts', {
cwd: './tsconfig',
});
expect(nodeProcess.stdout).toBe('resolve-target');
expect(nodeProcess.stdout).toBe('resolve-target loaded\nresolve-target value');
});

test('Require flag', async () => {
const nodeProcess = await node.requireFlag('resolve-target', {
cwd: './tsconfig',
});
expect(nodeProcess.stdout).toMatch('resolve-target loaded');
});

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

test('resolves paths prefix', async () => {
Expand Down
30 changes: 28 additions & 2 deletions tests/utils/node-with-loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs/promises';
import path from 'path';
import { execaNode } from 'execa';
import { execaNode, execa } from 'execa';
import getNode from 'get-node';

type Options = {
Expand All @@ -11,6 +11,8 @@ type Options = {
nodeOptions?: string[];
};

const cjsLoaderPath = path.resolve(__dirname, '../..');

export const nodeWithLoader = async (
options: Options,
) => await execaNode(
Expand All @@ -25,7 +27,7 @@ export const nodeWithLoader = async (
...(options.nodeOptions ?? []),

'--require',
path.resolve(__dirname, '../..'),
cjsLoaderPath,
],
nodePath: options.nodePath,
cwd: options.cwd,
Expand Down Expand Up @@ -129,6 +131,30 @@ export async function createNode(
cwd: fixturePath,
});
},
requireFlag(
filePath: string,
options?: {
cwd?: string;
},
) {
return execa(
node.path,
[
'--require',
cjsLoaderPath,
'--require',
filePath,
'--eval',
'null',
],
{
cwd: path.join(fixturePath, options?.cwd ?? ''),
env: {
ESBK_DISABLE_CACHE: '1',
},
},
);
},
};
}

Expand Down

0 comments on commit 5e23dc3

Please sign in to comment.