33/**
44 * External dependencies
55 */
6- import { readFile , writeFile , copyFile , mkdir , unlink } from 'fs/promises' ;
6+ import {
7+ readFile ,
8+ writeFile ,
9+ copyFile ,
10+ mkdir ,
11+ unlink ,
12+ stat ,
13+ } from 'fs/promises' ;
714import path from 'path' ;
815import { parseArgs } from 'node:util' ;
916import esbuild from 'esbuild' ;
@@ -138,6 +145,18 @@ function normalizePath( p ) {
138145 return p . replace ( / \\ / g, '/' ) ;
139146}
140147
148+ function pathIsFile ( p ) {
149+ return stat ( p )
150+ . then ( ( s ) => s . isFile ( ) )
151+ . catch ( ( ) => false ) ;
152+ }
153+
154+ function pathIsDirectory ( p ) {
155+ return stat ( p )
156+ . then ( ( s ) => s . isDirectory ( ) )
157+ . catch ( ( ) => false ) ;
158+ }
159+
141160function transformPhpContent ( content , transforms ) {
142161 const {
143162 functionPrefix = '' ,
@@ -1002,7 +1021,7 @@ async function transpilePackage( packageName ) {
10021021 name : 'externalize-except-css' ,
10031022 setup ( build ) {
10041023 // Externalize all non-CSS imports
1005- build . onResolve ( { filter : / .* / } , ( args ) => {
1024+ build . onResolve ( { filter : / .* / } , async ( args ) => {
10061025 // Skip entry points
10071026 if ( args . kind === 'entry-point' ) {
10081027 return null ;
@@ -1013,6 +1032,33 @@ async function transpilePackage( packageName ) {
10131032 return null ;
10141033 }
10151034
1035+ // Fully resolve local dependencies without file extension.
1036+ if (
1037+ ( args . path . startsWith ( '.' ) ||
1038+ path . isAbsolute ( args . path ) ) &&
1039+ ! args . path . endsWith ( '.js' )
1040+ ) {
1041+ const baseDir = path . dirname ( args . importer ) ;
1042+ for ( const ext of [ '.js' , '.jsx' , '.ts' , '.tsx' ] ) {
1043+ const isFile = await pathIsFile (
1044+ path . resolve ( baseDir , args . path + ext )
1045+ ) ;
1046+ if ( isFile ) {
1047+ return { path : args . path + '.js' , external : true } ;
1048+ }
1049+ }
1050+
1051+ const isDir = await pathIsDirectory (
1052+ path . resolve ( baseDir , args . path )
1053+ ) ;
1054+ if ( isDir ) {
1055+ return {
1056+ path : args . path + '/index.js' ,
1057+ external : true ,
1058+ } ;
1059+ }
1060+ }
1061+
10161062 // Externalize everything else (keep imports as-is)
10171063 return { path : args . path , external : true } ;
10181064 } ) ;
0 commit comments