Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resolve modules if folder contains a package.json file #187

Merged
merged 4 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/index.ts
Expand Up @@ -250,6 +250,10 @@ const isFile = (path?: string | undefined): path is string => {
}
}

const isModule = (path?: string | undefined): path is string => {
return !!path && isFile(`${path}/package.json`)
JounQin marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {string} source the module to resolve; i.e './some-module'
* @param {string} file the importing file's full path; i.e. '/usr/local/bin/file.js'
Expand All @@ -267,7 +271,7 @@ function getMappedPath(
const originalExtensions = extensions
extensions = ['', ...extensions]

let paths: string[] | undefined = []
let paths: Array<string | undefined> | undefined = []

if (RELATIVE_PATH_PATTERN.test(source)) {
const resolved = path.resolve(path.dirname(file), source)
Expand All @@ -283,7 +287,7 @@ function getMappedPath(
]),
)
.flat(2)
.filter(isFile)
.filter(path => isFile(path) || isModule(path))
}

if (retry && paths.length === 0) {
Expand Down
3 changes: 3 additions & 0 deletions tests/withPaths/index.ts
Expand Up @@ -10,6 +10,9 @@ import 'folder/tsxImportee'
import 'folder/subfolder/tsImportee'
import 'folder/subfolder/tsxImportee'

// import module with typings set in package.json
import 'folder/module'

// import from node_module
import 'typescript'
import 'dummy.js'
1 change: 1 addition & 0 deletions tests/withPaths/module/module.d.ts
@@ -0,0 +1 @@
export {}
5 changes: 5 additions & 0 deletions tests/withPaths/module/package.json
@@ -0,0 +1,5 @@
{
"type": "commonjs",
"typings": "./module.d.ts",
"private": true
}