4
4
*/
5
5
6
6
import type { PackageScope , ParsedModuleId } from '#src/interfaces'
7
+ import PACKAGE_NAME_REGEX from '#src/internal/regex-package-name'
7
8
import validateSet from '#src/internal/validate-set'
8
9
import validateURLString from '#src/internal/validate-url-string'
9
10
import type { ModuleId } from '#src/types'
@@ -18,6 +19,7 @@ import pathe from '@flex-development/pathe'
18
19
import {
19
20
DOT ,
20
21
cast ,
22
+ includes ,
21
23
isNIL ,
22
24
regexp ,
23
25
type Nullable
@@ -85,6 +87,9 @@ const toBareSpecifier = (
85
87
// exit early if specifier is builtin module
86
88
if ( isBuiltin ( specifier ) ) return toNodeURL ( specifier )
87
89
90
+ // exit early if specifier is package name
91
+ if ( PACKAGE_NAME_REGEX . test ( specifier ) ) return specifier
92
+
88
93
// ensure specifier is a path
89
94
if ( specifier . startsWith ( 'file:' ) ) specifier = fileURLToPath ( specifier )
90
95
@@ -122,9 +127,12 @@ const toBareSpecifier = (
122
127
const { exports, main, name = '' , types } = scope . pkgjson
123
128
124
129
// 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
+ )
128
136
129
137
/**
130
138
* Parsed module specifier.
0 commit comments