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

fix(tsconfig paths): support paths starting with slash #41

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
} from 'get-tsconfig';
import type { TransformOptions } from 'esbuild';

const isPathPattern = /^\.{0,2}\//;
const isRelativePathPattern = /^\.{1,2}\//;
const isTsFilePatten = /\.[cm]?tsx?$/;
const nodeModulesPath = `${path.sep}node_modules${path.sep}`;

Expand Down Expand Up @@ -147,7 +147,7 @@

// Add support for "node:" protocol
const resolveFilename = Module._resolveFilename;
Module._resolveFilename = function (request, parent, isMain, options) {

Check warning on line 150 in src/index.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected unnamed function
// Added in v12.20.0
// https://nodejs.org/api/esm.html#esm_node_imports
if (!supportsNodePrefix && request.startsWith('node:')) {
Expand All @@ -158,7 +158,7 @@
tsconfigPathsMatcher

// bare specifier
&& !isPathPattern.test(request)
&& !isRelativePathPattern.test(request)

// Dependency paths should not be resolved using tsconfig.json
&& !parent?.filename?.includes(nodeModulesPath)
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/tsconfig/src/paths-slash-match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import value from '/nested-resolve-target';

console.log(value);
3 changes: 2 additions & 1 deletion tests/fixtures/tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"paths": {
"paths-exact-match": ["resolve-target"],
"p/*": ["utils/*"],
"*/s": ["utils/*"]
"*/s": ["utils/*"],
"/*": ["utils/*"]
},
},
}
7 changes: 5 additions & 2 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.16.2', // Pre ESM import
'12',
'14',
'16',
'17',
'18',
]
: []
),
Expand Down
7 changes: 7 additions & 0 deletions tests/specs/typescript/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,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',
Expand Down
Loading