@@ -2,10 +2,10 @@ import fs from 'node:fs';
22import path from 'node:path' ;
33import assert from 'node:assert' ;
44import { debuglog , inspect } from 'node:util' ;
5- import is , { isAsyncFunction , isClass , isGeneratorFunction , isObject } from 'is-type-of' ;
65import homedir from 'node-homedir' ;
6+ import { isAsyncFunction , isClass , isGeneratorFunction , isObject , isPromise } from 'is-type-of' ;
77import type { Logger } from 'egg-logger' ;
8- import { getParamNames , readJSONSync } from 'utility' ;
8+ import { getParamNames , readJSONSync , readJSON } from 'utility' ;
99import { extend } from 'extend2' ;
1010import { Request , Response , Context , Application } from '@eggjs/koa' ;
1111import { pathMatching , type PathMatchingOptions } from 'egg-path-matching' ;
@@ -462,7 +462,7 @@ export class EggLoader {
462462 plugin . path = this . getPluginPath ( plugin ) ;
463463
464464 // read plugin information from ${plugin.path}/package.json
465- this . #mergePluginConfig( plugin ) ;
465+ await this . #mergePluginConfig( plugin ) ;
466466
467467 // disable the plugin that not match the serverEnv
468468 if ( env && plugin . env . length > 0 && ! plugin . env . includes ( env ) ) {
@@ -538,7 +538,7 @@ export class EggLoader {
538538 for ( const name in customPlugins ) {
539539 this . #normalizePluginConfig( customPlugins , name , configPath ) ;
540540 }
541- debug ( 'Loaded custom plugins: %j ' , Object . keys ( customPlugins ) ) ;
541+ debug ( 'Loaded custom plugins: %o ' , customPlugins ) ;
542542 }
543543 return customPlugins ;
544544 }
@@ -623,16 +623,18 @@ export class EggLoader {
623623 // "strict": true, whether check plugin name, default to true.
624624 // }
625625 // }
626- #mergePluginConfig( plugin : EggPluginInfo ) {
626+ async #mergePluginConfig( plugin : EggPluginInfo ) {
627627 let pkg ;
628628 let config ;
629629 const pluginPackage = path . join ( plugin . path ! , 'package.json' ) ;
630- if ( fs . existsSync ( pluginPackage ) ) {
631- pkg = readJSONSync ( pluginPackage ) ;
630+ if ( await utils . existsPath ( pluginPackage ) ) {
631+ pkg = await readJSON ( pluginPackage ) ;
632632 config = pkg . eggPlugin ;
633633 if ( pkg . version ) {
634634 plugin . version = pkg . version ;
635635 }
636+ // support commonjs and esm dist files
637+ plugin . path = this . #formatPluginPathFromPackageJSON( plugin . path ! , pkg ) ;
636638 }
637639
638640 const logger = this . options . logger ;
@@ -712,9 +714,9 @@ export class EggLoader {
712714 }
713715
714716 // Following plugins will be enabled implicitly.
715- // - configclient required by [hsfclient ]
716- // - eagleeye required by [hsfclient ]
717- // - diamond required by [hsfclient ]
717+ // - configclient required by [rpcClient ]
718+ // - monitor required by [rpcClient ]
719+ // - diamond required by [rpcClient ]
718720 if ( implicitEnabledPlugins . length ) {
719721 let message = implicitEnabledPlugins
720722 . map ( name => ` - ${ name } required by [${ requireMap [ name ] } ]` )
@@ -769,20 +771,43 @@ export class EggLoader {
769771
770772 #resolvePluginPath( plugin : EggPluginInfo ) {
771773 const name = plugin . package || plugin . name ;
772-
773774 try {
774775 // should find the plugin directory
775776 // pnpm will lift the node_modules to the sibling directory
776777 // 'node_modules/.pnpm/yadan@2.0.0/node_modules/yadan/node_modules',
777778 // 'node_modules/.pnpm/yadan@2.0.0/node_modules', <- this is the sibling directory
778779 // 'node_modules/.pnpm/egg@2.33.1/node_modules/egg/node_modules',
779780 // 'node_modules/.pnpm/egg@2.33.1/node_modules', <- this is the sibling directory
780- const filePath = utils . resolvePath ( `${ name } /package.json` , { paths : [ ...this . lookupDirs ] } ) ;
781- return path . dirname ( filePath ) ;
782- } catch ( err : any ) {
781+ const pluginPkgFile = utils . resolvePath ( `${ name } /package.json` , { paths : [ ...this . lookupDirs ] } ) ;
782+ return path . dirname ( pluginPkgFile ) ;
783+ } catch ( err ) {
783784 debug ( '[resolvePluginPath] error: %o' , err ) ;
784- throw new Error ( `Can not find plugin ${ name } in "${ [ ...this . lookupDirs ] . join ( ', ' ) } "` ) ;
785+ throw new Error ( `Can not find plugin ${ name } in "${ [ ...this . lookupDirs ] . join ( ', ' ) } "` , {
786+ cause : err ,
787+ } ) ;
788+ }
789+ }
790+
791+ #formatPluginPathFromPackageJSON( pluginPath : string , pluginPkg : {
792+ eggPlugin ?: {
793+ exports ?: {
794+ import ?: string ;
795+ require ?: string ;
796+ } ;
797+ } ;
798+ } ) {
799+ if ( pluginPkg . eggPlugin ?. exports ) {
800+ if ( typeof require === 'function' ) {
801+ if ( pluginPkg . eggPlugin . exports . require ) {
802+ pluginPath = path . join ( pluginPath , pluginPkg . eggPlugin . exports . require ) ;
803+ }
804+ } else {
805+ if ( pluginPkg . eggPlugin . exports . import ) {
806+ pluginPath = path . join ( pluginPath , pluginPkg . eggPlugin . exports . import ) ;
807+ }
808+ }
785809 }
810+ return pluginPath ;
786811 }
787812
788813 #extendPlugins( targets : Record < string , EggPluginInfo > , plugins : Record < string , EggPluginInfo > ) {
@@ -1036,9 +1061,10 @@ export class EggLoader {
10361061 debug ( 'loadExtend %s, filepaths: %j' , name , filepaths ) ;
10371062
10381063 const mergeRecord = new Map ( ) ;
1039- for ( let filepath of filepaths ) {
1040- filepath = this . resolveModule ( filepath ) ! ;
1064+ for ( const rawFilepath of filepaths ) {
1065+ const filepath = this . resolveModule ( rawFilepath ) ! ;
10411066 if ( ! filepath ) {
1067+ debug ( 'loadExtend %o not found' , rawFilepath ) ;
10421068 continue ;
10431069 }
10441070 if ( filepath . endsWith ( '/index.js' ) ) {
@@ -1421,7 +1447,7 @@ export class EggLoader {
14211447 let mod = await this . requireFile ( fullpath ) ;
14221448 if ( typeof mod === 'function' && ! isClass ( mod ) ) {
14231449 mod = mod ( ...inject ) ;
1424- if ( is . promise ( mod ) ) {
1450+ if ( isPromise ( mod ) ) {
14251451 mod = await mod ;
14261452 }
14271453 }
0 commit comments