diff --git a/src/@types/module.d.ts b/src/@types/module.d.ts index 496f156..e557f3e 100644 --- a/src/@types/module.d.ts +++ b/src/@types/module.d.ts @@ -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; diff --git a/src/index.ts b/src/index.ts index 7f72ab4..5dcc41f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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); diff --git a/tests/fixtures/tsconfig/src/resolve-target.ts b/tests/fixtures/tsconfig/src/resolve-target.ts index 3e88556..421aa39 100644 --- a/tests/fixtures/tsconfig/src/resolve-target.ts +++ b/tests/fixtures/tsconfig/src/resolve-target.ts @@ -1 +1,2 @@ -export default 'resolve-target'; +console.log('resolve-target loaded'); +export default 'resolve-target value'; diff --git a/tests/specs/typescript/ts.ts b/tests/specs/typescript/ts.ts index c7319c8..5093d50 100644 --- a/tests/specs/typescript/ts.ts +++ b/tests/specs/typescript/ts.ts @@ -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 }) => { diff --git a/tests/specs/typescript/tsconfig.ts b/tests/specs/typescript/tsconfig.ts index 2b26611..47edd78 100644 --- a/tests/specs/typescript/tsconfig.ts +++ b/tests/specs/typescript/tsconfig.ts @@ -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 () => { diff --git a/tests/utils/node-with-loader.ts b/tests/utils/node-with-loader.ts index a1363be..f3dc496 100644 --- a/tests/utils/node-with-loader.ts +++ b/tests/utils/node-with-loader.ts @@ -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 = { @@ -11,6 +11,8 @@ type Options = { nodeOptions?: string[]; }; +const cjsLoaderPath = path.resolve(__dirname, '../..'); + export const nodeWithLoader = async ( options: Options, ) => await execaNode( @@ -25,7 +27,7 @@ export const nodeWithLoader = async ( ...(options.nodeOptions ?? []), '--require', - path.resolve(__dirname, '../..'), + cjsLoaderPath, ], nodePath: options.nodePath, cwd: options.cwd, @@ -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', + }, + }, + ); + }, }; }