From a25b92542d8984f4df5f0b7052c470dbccb437a1 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Fri, 15 Sep 2023 00:18:19 +0900 Subject: [PATCH] fix(tsconfig paths): support paths starting with slash (#75) --- src/loaders.ts | 4 ++-- .../package-module/tsconfig/src/paths-slash-match.ts | 3 +++ tests/fixtures/package-module/tsconfig/tsconfig.json | 3 ++- tests/index.ts | 7 +++++-- tests/specs/typescript/tsconfig.ts | 7 +++++++ 5 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/package-module/tsconfig/src/paths-slash-match.ts diff --git a/src/loaders.ts b/src/loaders.ts index c68494f..ad41edf 100644 --- a/src/loaders.ts +++ b/src/loaders.ts @@ -125,7 +125,7 @@ async function tryDirectory( } } -const isPathPattern = /^\.{0,2}\//; +const isRelativePathPattern = /^\.{1,2}\//; const supportsNodePrefix = ( compareNodeVersion([14, 13, 1]) >= 0 @@ -151,7 +151,7 @@ export const resolve: resolve = async function ( const isPath = ( specifier.startsWith(fileProtocol) - || isPathPattern.test(specifier) + || isRelativePathPattern.test(specifier) ); if ( diff --git a/tests/fixtures/package-module/tsconfig/src/paths-slash-match.ts b/tests/fixtures/package-module/tsconfig/src/paths-slash-match.ts new file mode 100644 index 0000000..c906c5a --- /dev/null +++ b/tests/fixtures/package-module/tsconfig/src/paths-slash-match.ts @@ -0,0 +1,3 @@ +import value from '/nested-resolve-target'; + +console.log(value); diff --git a/tests/fixtures/package-module/tsconfig/tsconfig.json b/tests/fixtures/package-module/tsconfig/tsconfig.json index 9d0c412..ea35d97 100644 --- a/tests/fixtures/package-module/tsconfig/tsconfig.json +++ b/tests/fixtures/package-module/tsconfig/tsconfig.json @@ -7,7 +7,8 @@ "paths": { "paths-exact-match": ["resolve-target"], "p/*": ["utils/*"], - "*/s": ["utils/*"] + "*/s": ["utils/*"], + "/*": ["utils/*"] }, }, } diff --git a/tests/index.ts b/tests/index.ts index 62a19cb..1711bcf 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -2,16 +2,19 @@ import { describe } from 'manten'; import { createNode } from './utils/node-with-loader.js'; const nodeVersions = [ - '18', '20', ...( - process.env.CI + ( + process.env.CI + && process.platform !== 'win32' + ) ? [ '12.20.0', // CJS named export detection added '12', '14', '16', '17', + '18', ] : [] ), diff --git a/tests/specs/typescript/tsconfig.ts b/tests/specs/typescript/tsconfig.ts index 2ae6e9c..23afa76 100644 --- a/tests/specs/typescript/tsconfig.ts +++ b/tests/specs/typescript/tsconfig.ts @@ -113,6 +113,13 @@ export default testSuite(async ({ describe }, node: NodeApis) => { expect(nodeProcess.stdout).toBe('nested-resolve-target'); }); + test('resolves paths slash prefix', async () => { + const nodeProcess = await node.load('./src/paths-slash-match.ts', { + cwd: './tsconfig', + }); + expect(nodeProcess.stdout).toBe('nested-resolve-target'); + }); + test('resolves paths suffix', async () => { const nodeProcess = await node.load('./src/paths-suffix-match.ts', { cwd: './tsconfig',