Skip to content

Commit 3e8ea36

Browse files
committed
fix: conflict with orignal egg master branch
2 parents b6e83aa + cf7203e commit 3e8ea36

17 files changed

Lines changed: 181 additions & 74 deletions

File tree

‎.github/workflows/nodejs.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ jobs:
1111
name: Node.js
1212
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
1313
with:
14-
version: '18.19.0, 18, 20, 22'
14+
version: '18.19.0, 18, 20, 22, 23'
1515
secrets:
1616
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ test/fixtures/*/timing.json
1414
lib/
1515
.tshy*
1616
dist
17+
.package-lock.json

‎CHANGELOG.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Changelog
22

3+
## [6.1.0](https://github.com/eggjs/egg-core/compare/v6.0.3...v6.1.0) (2024-12-17)
4+
5+
6+
### Features
7+
8+
* support `pkg.eggPlugin.exports` property ([#274](https://github.com/eggjs/egg-core/issues/274)) ([df9efed](https://github.com/eggjs/egg-core/commit/df9efedb22c630b13a4f1374f96450f6ba1107ab))
9+
10+
## [6.0.3](https://github.com/eggjs/egg-core/compare/v6.0.2...v6.0.3) (2024-12-13)
11+
12+
13+
### Bug Fixes
14+
15+
* dont import default value ([#273](https://github.com/eggjs/egg-core/issues/273)) ([16274e7](https://github.com/eggjs/egg-core/commit/16274e744007bcbeef9a99210fa00c8812d05718))
16+
317
## [6.0.2](https://github.com/eggjs/egg-core/compare/v6.0.1...v6.0.2) (2024-06-30)
418

519

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[![Test coverage][codecov-image]][codecov-url]
66
[![Known Vulnerabilities][snyk-image]][snyk-url]
77
[![npm download][download-image]][download-url]
8+
[![Node.js Version](https://img.shields.io/node/v/@eggjs/core.svg?style=flat)](https://nodejs.org/en/download/)
89

910
[npm-image]: https://img.shields.io/npm/v/@eggjs/core.svg?style=flat-square
1011
[npm-url]: https://npmjs.org/package/@eggjs/core

‎package.json‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@eggjs/core",
3-
"version": "6.0.2",
3+
"version": "6.1.0",
44
"publishConfig": {
55
"access": "public"
66
},
@@ -35,7 +35,7 @@
3535
},
3636
"homepage": "https://github.com/eggjs/egg-core#readme",
3737
"dependencies": {
38-
"@eggjs/koa": "^2.18.2",
38+
"@eggjs/koa": "^2.19.2",
3939
"@eggjs/router": "^3.0.5",
4040
"@eggjs/utils": "^4.0.2",
4141
"egg-logger": "^3.5.0",
@@ -66,7 +66,7 @@
6666
"mm": "3",
6767
"supertest": "7",
6868
"ts-node": "10",
69-
"tshy": "1",
69+
"tshy": "3",
7070
"tshy-after": "1",
7171
"typescript": "5",
7272
"urllib": "4"
@@ -86,17 +86,16 @@
8686
"./package.json": "./package.json",
8787
".": {
8888
"import": {
89-
"source": "./src/index.ts",
9089
"types": "./dist/esm/index.d.ts",
9190
"default": "./dist/esm/index.js"
9291
},
9392
"require": {
94-
"source": "./src/index.ts",
9593
"types": "./dist/commonjs/index.d.ts",
9694
"default": "./dist/commonjs/index.js"
9795
}
9896
}
9997
},
10098
"main": "./dist/commonjs/index.js",
101-
"types": "./dist/commonjs/index.d.ts"
99+
"types": "./dist/commonjs/index.d.ts",
100+
"module": "./dist/esm/index.js"
102101
}

‎src/egg.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/* eslint-disable prefer-spread */
22
import assert from 'node:assert';
33
import { debuglog } from 'node:util';
4-
import is from 'is-type-of';
5-
import KoaApplication from '@eggjs/koa';
4+
import { Application as KoaApplication } from '@eggjs/koa';
65
import type { ContextDelegation, MiddlewareFunc } from '@eggjs/koa';
76
import { EggConsoleLogger } from 'egg-logger';
87
import { RegisterOptions, ResourcesController, EggRouter as Router } from '@eggjs/router';
@@ -157,10 +156,8 @@ export class EggCore extends KoaApplication {
157156
* override koa's app.use, support generator function
158157
* @since 1.0.0
159158
*/
160-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
161-
// @ts-ignore
162159
use(fn: MiddlewareFunc) {
163-
assert(is.function(fn), 'app.use() requires a function');
160+
assert(typeof fn === 'function', 'app.use() requires a function');
164161
debug('[use] add middleware: %o', fn._name || fn.name || '-');
165162
this.middleware.push(fn);
166163
return this;
@@ -226,7 +223,7 @@ export class EggCore extends KoaApplication {
226223
* Execute scope after loaded and before app start.
227224
*
228225
* Notice:
229-
* This method is now NOT recommanded and reguarded as a deprecated one,
226+
* This method is now NOT recommended and regarded as a deprecated one,
230227
* For plugin development, we should use `didLoad` instead.
231228
* For application development, we should use `willReady` instead.
232229
*
@@ -251,7 +248,12 @@ export class EggCore extends KoaApplication {
251248
* console.log('done');
252249
* });
253250
*/
251+
ready(): Promise<void>;
252+
ready(flagOrFunction: ReadyFunctionArg): void;
254253
ready(flagOrFunction?: ReadyFunctionArg) {
254+
if (flagOrFunction === undefined) {
255+
return this.lifecycle.ready();
256+
}
255257
return this.lifecycle.ready(flagOrFunction);
256258
}
257259

‎src/lifecycle.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import assert from 'node:assert';
22
import { EventEmitter } from 'node:events';
33
import { debuglog } from 'node:util';
4-
import is, { isClass } from 'is-type-of';
5-
import ReadyObject from 'get-ready';
4+
import { isClass } from 'is-type-of';
5+
import { Ready as ReadyObject } from 'get-ready';
66
import type { ReadyFunctionArg } from 'get-ready';
77
import { Ready } from 'ready-callback';
88
import { EggConsoleLogger } from 'egg-logger';
@@ -109,8 +109,13 @@ export class Lifecycle extends EventEmitter {
109109
});
110110
}
111111

112-
ready(arg?: ReadyFunctionArg) {
113-
return this.#readyObject.ready(arg);
112+
ready(): Promise<void>;
113+
ready(flagOrFunction: ReadyFunctionArg): void;
114+
ready(flagOrFunction?: ReadyFunctionArg) {
115+
if (flagOrFunction === undefined) {
116+
return this.#readyObject.ready();
117+
}
118+
return this.#readyObject.ready(flagOrFunction);
114119
}
115120

116121
get app() {
@@ -184,7 +189,7 @@ export class Lifecycle extends EventEmitter {
184189
}
185190

186191
registerBeforeClose(fn: Fun) {
187-
assert(is.function(fn), 'argument should be function');
192+
assert(typeof fn === 'function', 'argument should be function');
188193
assert(this.#isClosed === false, 'app has been closed');
189194
this.#closeFunctionSet.add(fn);
190195
}

‎src/loader/egg_loader.ts‎

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import assert from 'node:assert';
44
import { debuglog, inspect } from 'node:util';
5-
import is, { isAsyncFunction, isClass, isGeneratorFunction, isObject } from 'is-type-of';
65
import homedir from 'node-homedir';
6+
import { isAsyncFunction, isClass, isGeneratorFunction, isObject, isPromise } from 'is-type-of';
77
import type { Logger } from 'egg-logger';
8-
import { getParamNames, readJSONSync } from 'utility';
8+
import { getParamNames, readJSONSync, readJSON } from 'utility';
99
import { extend } from 'extend2';
1010
import { Request, Response, Context, Application } from '@eggjs/koa';
1111
import { 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

Comments
 (0)