11import path from 'path' ;
2+ import resolve from 'resolve' ;
23import mapToRelative from './mapToRelative' ;
34
45function createAliasFileMap ( pluginOpts ) {
@@ -10,6 +11,13 @@ function createAliasFileMap(pluginOpts) {
1011 ) , { } ) ;
1112}
1213
14+ function replaceExt ( p , ext ) {
15+ const filename = path . basename ( p , path . extname ( p ) ) + ext ;
16+ return path . join ( path . dirname ( p ) , filename ) ;
17+ }
18+
19+ const defaultBabelExtensions = [ '.js' , '.jsx' , '.es' , '.es6' ] ;
20+
1321export function mapModule ( source , file , pluginOpts ) {
1422 // Do not map source starting with a dot
1523 if ( source [ 0 ] === '.' ) {
@@ -21,9 +29,10 @@ export function mapModule(source, file, pluginOpts) {
2129 for ( let i = 0 ; i < rootDirs . length ; i ++ ) {
2230 try {
2331 // check if the file exists (will throw if not)
24- const p = path . resolve ( rootDirs [ i ] , source ) ;
25- require . resolve ( p ) ;
26- return mapToRelative ( file , p ) ;
32+ const extensions = pluginOpts . extensions || defaultBabelExtensions ;
33+ const fileAbsPath = resolve . sync ( `./${ source } ` , { basedir : path . resolve ( rootDirs [ i ] ) , extensions } ) ;
34+ // map the source and keep its extension if the import/require had one
35+ return mapToRelative ( file , replaceExt ( fileAbsPath , path . extname ( source ) ) ) ;
2736 } catch ( e ) {
2837 // empty...
2938 }
0 commit comments