Skip to content

Commit 7d2642d

Browse files
committed
fix: resolve modules if folder contains a package.json file
getMappedPath only resolves to files that exist. This does not work when a TSConfig path alias resolves to a module folder containing built assets, and described by a package.json, e.g. foo/dist. If the tsconfig path is mapped to a folder, then getMappedPath returns undefined as the folder is not a file. However, in somes cases it is ok for this to resolve as the package.json contains a `typings` entry that references a .d.ts file, which is correctly handled by enhanced-resolve
1 parent 85f64d0 commit 7d2642d

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

Diff for: src/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ const isFile = (path?: string | undefined): path is string => {
250250
}
251251
}
252252

253+
const isModule = (path?: string | undefined): path is string => {
254+
return !!path && isFile(`${path}/package.json`)
255+
}
256+
253257
/**
254258
* @param {string} source the module to resolve; i.e './some-module'
255259
* @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js'
@@ -267,7 +271,7 @@ function getMappedPath(
267271
const originalExtensions = extensions
268272
extensions = ['', ...extensions]
269273

270-
let paths: string[] | undefined = []
274+
let paths: Array<string | undefined> | undefined = []
271275

272276
if (RELATIVE_PATH_PATTERN.test(source)) {
273277
const resolved = path.resolve(path.dirname(file), source)
@@ -283,7 +287,7 @@ function getMappedPath(
283287
]),
284288
)
285289
.flat(2)
286-
.filter(isFile)
290+
.filter(path => isFile(path) || isModule(path))
287291
}
288292

289293
if (retry && paths.length === 0) {

Diff for: tests/withPaths/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import 'folder/tsxImportee'
1010
import 'folder/subfolder/tsImportee'
1111
import 'folder/subfolder/tsxImportee'
1212

13+
// import module with typings set in package.json
14+
import 'folder/module'
15+
1316
// import from node_module
1417
import 'typescript'
1518
import 'dummy.js'

Diff for: tests/withPaths/module/module.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {}

Diff for: tests/withPaths/module/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "commonjs",
3+
"typings": "./module.d.ts",
4+
"private": true
5+
}

0 commit comments

Comments
 (0)