@@ -5,9 +5,13 @@ import { debuglog, inspect } from 'node:util';
55import { homedir } from 'node-homedir' ;
66import { isAsyncFunction , isClass , isGeneratorFunction , isObject , isPromise } from 'is-type-of' ;
77import type { Logger } from 'egg-logger' ;
8- import { getParamNames , readJSONSync , readJSON } from 'utility' ;
8+ import {
9+ getParamNames , readJSONSync , readJSON , exists ,
10+ } from 'utility' ;
911import { extend } from 'extend2' ;
1012import { Request , Response , Application , Context as KoaContext } from '@eggjs/koa' ;
13+ import { register as tsconfigPathsRegister } from 'tsconfig-paths' ;
14+ import { isESM , isSupportTypeScript } from '@eggjs/utils' ;
1115import { pathMatching , type PathMatchingOptions } from 'egg-path-matching' ;
1216import { now , diff } from 'performance-ms' ;
1317import { CaseStyle , FULLPATH , FileLoader , FileLoaderOptions } from './file_loader.js' ;
@@ -65,7 +69,6 @@ export class EggLoader {
6569 readonly appInfo : EggAppInfo ;
6670 dirs ?: EggDirInfo [ ] ;
6771
68-
6972 /**
7073 * @class
7174 * @param {Object } options - options
@@ -95,12 +98,11 @@ export class EggLoader {
9598 if ( process . env . EGG_TYPESCRIPT === 'true' || ( this . pkg . egg && this . pkg . egg . typescript ) ) {
9699 // skip require tsconfig-paths if tsconfig.json not exists
97100 const tsConfigFile = path . join ( this . options . baseDir , 'tsconfig.json' ) ;
98- // FIXME: support esm
99- if ( fs . existsSync ( tsConfigFile ) && typeof require === 'function' ) {
100- // eslint-disable-next-line @typescript-eslint/no-var-requires
101- require ( 'tsconfig-paths' ) . register ( { cwd : this . options . baseDir } ) ;
101+ if ( fs . existsSync ( tsConfigFile ) ) {
102+ tsconfigPathsRegister ( { cwd : this . options . baseDir } as any ) ;
102103 } else {
103- this . logger . info ( '[@eggjs/core/egg_loader] skip register "tsconfig-paths" because tsconfig.json not exists at %s' ,
104+ this . logger . info (
105+ '[@eggjs/core/egg_loader] skip register "tsconfig-paths" because tsconfig.json not exists at %s' ,
104106 tsConfigFile ) ;
105107 }
106108 }
@@ -599,7 +601,7 @@ export class EggLoader {
599601 plugin . version = pkg . version ;
600602 }
601603 // support commonjs and esm dist files
602- plugin . path = this . #formatPluginPathFromPackageJSON( plugin . path ! , pkg ) ;
604+ plugin . path = await this . #formatPluginPathFromPackageJSON( plugin . path ! , pkg ) ;
603605 }
604606
605607 const logger = this . options . logger ;
@@ -753,26 +755,36 @@ export class EggLoader {
753755 }
754756 }
755757
756- #formatPluginPathFromPackageJSON( pluginPath : string , pluginPkg : {
758+ async #formatPluginPathFromPackageJSON( pluginPath : string , pluginPkg : {
757759 eggPlugin ?: {
758760 exports ?: {
759761 import ?: string ;
760762 require ?: string ;
763+ typescript ?: string ;
761764 } ;
762765 } ;
763- } ) {
764- if ( pluginPkg . eggPlugin ?. exports ) {
765- if ( typeof require === 'function' ) {
766- if ( pluginPkg . eggPlugin . exports . require ) {
767- pluginPath = path . join ( pluginPath , pluginPkg . eggPlugin . exports . require ) ;
766+ } ) : Promise < string > {
767+ let realPluginPath = pluginPath ;
768+ const exports = pluginPkg . eggPlugin ?. exports ;
769+ if ( exports ) {
770+ if ( isESM ) {
771+ if ( exports . import ) {
772+ realPluginPath = path . join ( pluginPath , exports . import ) ;
768773 }
769774 } else {
770- if ( pluginPkg . eggPlugin . exports . import ) {
771- pluginPath = path . join ( pluginPath , pluginPkg . eggPlugin . exports . import ) ;
775+ if ( exports . require ) {
776+ realPluginPath = path . join ( pluginPath , exports . require ) ;
777+ }
778+ }
779+ if ( exports . typescript && isSupportTypeScript ( ) ) {
780+ if ( ! ( await exists ( realPluginPath ) ) ) {
781+ // if require/import path not exists, use typescript path for development stage
782+ realPluginPath = path . join ( pluginPath , exports . typescript ) ;
783+ debug ( '[formatPluginPathFromPackageJSON] use typescript path %o' , realPluginPath ) ;
772784 }
773785 }
774786 }
775- return pluginPath ;
787+ return realPluginPath ;
776788 }
777789
778790 #extendPlugins( targets : Record < string , EggPluginInfo > , plugins : Record < string , EggPluginInfo > ) {
0 commit comments