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,25 @@ module.exports = class AliasPlugin {
30
31
*/
31
32
apply ( resolver ) {
32
33
const target = resolver . ensureHook ( this . target ) ;
34
+ const cache = { } ;
35
+ const getAbsolutePathWithSlashEnding = maybeAbsolutePath => {
36
+ if ( cache [ maybeAbsolutePath ] === undefined ) {
37
+ const type = getType ( maybeAbsolutePath ) ;
38
+ if ( type === PathType . AbsolutePosix || type === PathType . AbsoluteWin ) {
39
+ cache [ maybeAbsolutePath ] = resolver
40
+ . join ( maybeAbsolutePath , "_" )
41
+ . slice ( 0 , - 1 ) ;
42
+ } else {
43
+ cache [ maybeAbsolutePath ] = null ;
44
+ }
45
+ }
46
+ return cache [ maybeAbsolutePath ] ;
47
+ } ;
48
+ const isSubPath = ( path , maybeSubPath ) => {
49
+ const absolutePath = getAbsolutePathWithSlashEnding ( maybeSubPath ) ;
50
+ if ( ! absolutePath ) return false ;
51
+ return path . startsWith ( absolutePath ) ;
52
+ } ;
33
53
resolver
34
54
. getHook ( this . source )
35
55
. tapAsync ( "AliasPlugin" , ( request , resolveContext , callback ) => {
@@ -42,11 +62,9 @@ module.exports = class AliasPlugin {
42
62
if (
43
63
innerRequest === item . name ||
44
64
( ! item . onlyModule &&
45
- innerRequest . startsWith (
46
- request . request
47
- ? `${ item . name } /`
48
- : resolver . join ( item . name , "_" ) . slice ( 0 , - 1 )
49
- ) )
65
+ ( request . request
66
+ ? innerRequest . startsWith ( `${ item . name } /` )
67
+ : isSubPath ( innerRequest , item . name ) ) )
50
68
) {
51
69
const remainingRequest = innerRequest . substr ( item . name . length ) ;
52
70
const resolveWithAlias = ( alias , callback ) => {
0 commit comments