@@ -13,36 +13,20 @@ module.exports = (options = { plugins: [] }) => ({
1313 setup : function ( build ) {
1414 const { rootDir = options . rootDir || process . cwd ( ) } = options ;
1515 const tmpDirPath = tmp . dirSync ( ) . name ;
16-
1716 build . onResolve (
1817 { filter : / .\. ( c s s ) $ / , namespace : "file" } ,
1918 async ( args ) => {
20- let sourceFullPath ;
21-
22- // Manual attempt at resolving from node_modules and other typical directories
23- if ( args . path . startsWith ( '.' ) || path . isAbsolute ( args . path ) ) {
24- sourceFullPath = path . resolve ( args . resolveDir , args . path ) ;
25- } else {
26- const modulePaths = [
27- // possible locations for node modules, maybe this is not strictly necessary
28- path . resolve ( args . resolveDir , 'node_modules' , args . path ) ,
29- path . resolve ( rootDir , 'node_modules' , args . path ) ,
30- path . resolve ( rootDir , '../node_modules' , args . path )
31- ] ;
32-
33- for ( const modulePath of modulePaths ) {
34- if ( fs . existsSync ( modulePath ) ) {
35- // if we find the path we need, use it as the sourceFullPath
36- sourceFullPath = modulePath ;
37- break ;
38- }
39- }
40-
41- if ( ! sourceFullPath ) {
42- throw new Error ( `Cannot resolve module: ${ args . path } ` ) ;
43- }
19+ // use esbuild path resolution for node_modules, typescript paths, etc.
20+ // https://esbuild.github.io/plugins/#resolve
21+ const resolution = await build . resolve ( args . path , {
22+ resolveDir : args . resolveDir ,
23+ kind : args . kind ,
24+ } ) ;
25+ if ( resolution . errors . length > 0 ) {
26+ return { errors : result . errors }
4427 }
4528
29+ const sourceFullPath = resolution . path ;
4630 const sourceExt = path . extname ( sourceFullPath ) ;
4731 const sourceBaseName = path . basename ( sourceFullPath , sourceExt ) ;
4832 const sourceDir = path . dirname ( sourceFullPath ) ;
0 commit comments