11import path from 'path' ;
22
3- import resolve from 'resolve ' ;
3+ import { warn } from './log ' ;
44import mapToRelative from './mapToRelative' ;
5- import { toLocalPath , toPosixPath , replaceExtension } from './utils' ;
5+ import { nodeResolvePath , replaceExtension , toLocalPath , toPosixPath } from './utils' ;
66
77
88function findPathInRoots ( sourcePath , { extensions, root } ) {
99 // Search the source path inside every custom root directory
1010 let resolvedSourceFile ;
1111
1212 root . some ( ( basedir ) => {
13- try {
14- // Check if the file exists (will throw if not)
15- resolvedSourceFile = resolve . sync ( `./${ sourcePath } ` , {
16- basedir,
17- extensions,
18- } ) ;
19- return true ;
20- } catch ( e ) {
21- return false ;
22- }
13+ resolvedSourceFile = nodeResolvePath ( `./${ sourcePath } ` , basedir , extensions ) ;
14+ return resolvedSourceFile !== null ;
2315 } ) ;
2416
2517 return resolvedSourceFile ;
@@ -43,40 +35,17 @@ function getRealPathFromRootConfig(sourcePath, currentFile, opts) {
4335 ) ) ) ;
4436}
4537
46- function getRealPathFromAliasConfig ( sourcePath , currentFile , { alias, cwd } ) {
47- const moduleSplit = sourcePath . split ( '/' ) ;
48- let aliasPath ;
49-
50- while ( moduleSplit . length ) {
51- const m = moduleSplit . join ( '/' ) ;
52- if ( { } . hasOwnProperty . call ( alias , m ) ) {
53- aliasPath = alias [ m ] ;
54- break ;
55- }
56- moduleSplit . pop ( ) ;
57- }
58-
59- // no alias mapping found
60- if ( ! aliasPath ) {
61- return null ;
62- }
63-
64- // remove legacy "npm:" prefix for npm packages
65- aliasPath = aliasPath . replace ( / ^ ( n p m : ) / , '' ) ;
66- const newPath = sourcePath . replace ( moduleSplit . join ( '/' ) , aliasPath ) ;
67-
68- // alias to npm module don't need relative mapping
69- if ( aliasPath [ 0 ] !== '.' ) {
70- return newPath ;
38+ function checkIfPackageExists ( modulePath , currentFile , extensions ) {
39+ const resolvedPath = nodeResolvePath ( modulePath , currentFile , extensions ) ;
40+ if ( resolvedPath === null ) {
41+ warn ( `Could not resolve "${ modulePath } " in file ${ currentFile } .` ) ;
7142 }
72-
73- return toLocalPath ( toPosixPath ( mapToRelative ( cwd , currentFile , newPath ) ) ) ;
7443}
7544
76- function getRealPathFromRegExpConfig ( sourcePath , currentFile , { regExps } ) {
45+ function getRealPathFromAliasConfig ( sourcePath , currentFile , opts ) {
7746 let aliasedSourceFile ;
7847
79- regExps . find ( ( [ regExp , substitute ] ) => {
48+ opts . alias . find ( ( [ regExp , substitute ] ) => {
8049 const execResult = regExp . exec ( sourcePath ) ;
8150
8251 if ( execResult === null ) {
@@ -87,13 +56,26 @@ function getRealPathFromRegExpConfig(sourcePath, currentFile, { regExps }) {
8756 return true ;
8857 } ) ;
8958
59+ if ( ! aliasedSourceFile ) {
60+ return null ;
61+ }
62+
63+ if ( aliasedSourceFile [ 0 ] === '.' ) {
64+ return toLocalPath ( toPosixPath (
65+ mapToRelative ( opts . cwd , currentFile , aliasedSourceFile ) ) ,
66+ ) ;
67+ }
68+
69+ if ( process . env . NODE_ENV !== 'production' ) {
70+ checkIfPackageExists ( aliasedSourceFile , currentFile , opts . extensions ) ;
71+ }
72+
9073 return aliasedSourceFile ;
9174}
9275
9376const resolvers = [
9477 getRealPathFromRootConfig ,
9578 getRealPathFromAliasConfig ,
96- getRealPathFromRegExpConfig ,
9779] ;
9880
9981export default function getRealPath ( sourcePath , { file, opts } ) {
0 commit comments