@@ -7,10 +7,18 @@ const looksLikeAbsolutePath = (maybeAbsolutePath) => {
77
88const normalizePathSeparator = ( p ) => p . replace ( / \\ / g, "/" ) ;
99
10- /* A map from context dir strings to maps from identifier names to relative paths */
11- const relativePaths = new Map ( ) ;
10+ const _makePathsRelative = ( context , identifier ) => {
11+ return identifier
12+ . split ( / ( [ | ! ] ) / )
13+ . map ( str => looksLikeAbsolutePath ( str ) ?
14+ normalizePathSeparator ( path . relative ( context , str ) ) : str )
15+ . join ( "" ) ;
16+ }
1217
13- exports . makePathsRelative = ( context , identifier ) => {
18+ exports . makePathsRelative = ( context , identifier , cache ) => {
19+ if ( ! cache ) return _makePathsRelative ( context , identifier ) ;
20+
21+ const relativePaths = cache . relativePaths || ( cache . relativePaths = new Map ( ) ) ;
1422 if ( ! relativePaths . has ( context ) ) {
1523 relativePaths . set ( context , new Map ( ) ) ;
1624 }
@@ -20,11 +28,7 @@ exports.makePathsRelative = (context, identifier) => {
2028 if ( contextCache . has ( identifier ) ) {
2129 return contextCache . get ( identifier ) ;
2230 } else {
23- var relativePath = identifier
24- . split ( / ( [ | ! ] ) / )
25- . map ( str => looksLikeAbsolutePath ( str ) ?
26- normalizePathSeparator ( path . relative ( context , str ) ) : str )
27- . join ( "" ) ;
31+ let relativePath = _makePathsRelative ( context , identifier ) ;
2832 contextCache . set ( identifier , relativePath ) ;
2933 return relativePath ;
3034 }
0 commit comments