6
6
"use strict" ;
7
7
8
8
const forEachBail = require ( "./forEachBail" ) ;
9
+ const { PathType, getType } = require ( "./util/path" ) ;
9
10
10
11
/** @typedef {import("./Resolver") } Resolver */
11
12
/** @typedef {import("./Resolver").ResolveRequest } ResolveRequest */
@@ -30,6 +31,18 @@ module.exports = class AliasPlugin {
30
31
*/
31
32
apply ( resolver ) {
32
33
const target = resolver . ensureHook ( this . target ) ;
34
+ const getAbsolutePathWithSlashEnding = maybeAbsolutePath => {
35
+ const type = getType ( maybeAbsolutePath ) ;
36
+ if ( type === PathType . AbsolutePosix || type === PathType . AbsoluteWin ) {
37
+ return resolver . join ( maybeAbsolutePath , "_" ) . slice ( 0 , - 1 ) ;
38
+ }
39
+ return null ;
40
+ } ;
41
+ const isSubPath = ( path , maybeSubPath ) => {
42
+ const absolutePath = getAbsolutePathWithSlashEnding ( maybeSubPath ) ;
43
+ if ( ! absolutePath ) return false ;
44
+ return path . startsWith ( absolutePath ) ;
45
+ } ;
33
46
resolver
34
47
. getHook ( this . source )
35
48
. tapAsync ( "AliasPlugin" , ( request , resolveContext , callback ) => {
@@ -42,11 +55,9 @@ module.exports = class AliasPlugin {
42
55
if (
43
56
innerRequest === item . name ||
44
57
( ! item . onlyModule &&
45
- innerRequest . startsWith (
46
- request . request
47
- ? `${ item . name } /`
48
- : resolver . join ( item . name , "_" ) . slice ( 0 , - 1 )
49
- ) )
58
+ ( request . request
59
+ ? innerRequest . startsWith ( `${ item . name } /` )
60
+ : isSubPath ( innerRequest , item . name ) ) )
50
61
) {
51
62
const remainingRequest = innerRequest . substr ( item . name . length ) ;
52
63
const resolveWithAlias = ( alias , callback ) => {
0 commit comments