Skip to content

Commit 4d9fdaf

Browse files
committed
fix(utils): [toBareSpecifier] specifier conversion
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent e869100 commit 4d9fdaf

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/utils/to-bare-specifier.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import type { PackageScope, ParsedModuleId } from '#src/interfaces'
7+
import PACKAGE_NAME_REGEX from '#src/internal/regex-package-name'
78
import validateSet from '#src/internal/validate-set'
89
import validateURLString from '#src/internal/validate-url-string'
910
import type { ModuleId } from '#src/types'
@@ -18,6 +19,7 @@ import pathe from '@flex-development/pathe'
1819
import {
1920
DOT,
2021
cast,
22+
includes,
2123
isNIL,
2224
regexp,
2325
type Nullable
@@ -85,6 +87,9 @@ const toBareSpecifier = (
8587
// exit early if specifier is builtin module
8688
if (isBuiltin(specifier)) return toNodeURL(specifier)
8789

90+
// exit early if specifier is package name
91+
if (PACKAGE_NAME_REGEX.test(specifier)) return specifier
92+
8893
// ensure specifier is a path
8994
if (specifier.startsWith('file:')) specifier = fileURLToPath(specifier)
9095

@@ -122,9 +127,12 @@ const toBareSpecifier = (
122127
const { exports, main, name = '', types } = scope.pkgjson
123128

124129
// convert specifier to bare specifier
125-
specifier = url.pathname.includes(name)
126-
? name + url.pathname.replace(new RegExp(`.*?${regexp(name)}`), '')
127-
: name + specifier.replace(fileURLToPath(scope.dir.replace(/\/$/, '')), '')
130+
specifier = !includes(url.pathname, 'node_modules' + pathe.sep + name)
131+
? name + specifier.replace(fileURLToPath(scope.dir.replace(/\/$/, '')), '')
132+
: url.pathname.replace(
133+
new RegExp(`.*?node_modules${regexp(pathe.sep)}(?=${regexp(name)})`),
134+
''
135+
)
128136

129137
/**
130138
* Parsed module specifier.

0 commit comments

Comments
 (0)