Skip to content

Commit 7adabd5

Browse files
authored
fix: support load plugin from typescript dir (#287)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **Dependencies** - Updated `@eggjs/utils` from version 4.1.5 to 4.2.4 - **Improvements** - Enhanced plugin loading mechanism for TypeScript projects - Improved error handling and logging for TypeScript configuration files - **Testing** - Added new test case for TypeScript plugin loading scenarios These updates improve the framework's compatibility and robustness when working with TypeScript plugins. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent ca75960 commit 7adabd5

File tree

8 files changed

+62
-18
lines changed

8 files changed

+62
-18
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"dependencies": {
4141
"@eggjs/koa": "^2.20.6",
4242
"@eggjs/router": "^3.0.5",
43-
"@eggjs/utils": "^4.1.5",
43+
"@eggjs/utils": "^4.2.4",
4444
"egg-logger": "^3.5.0",
4545
"egg-path-matching": "^2.0.0",
4646
"extend2": "^4.0.0",

src/loader/egg_loader.ts

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import { debuglog, inspect } from 'node:util';
55
import { homedir } from 'node-homedir';
66
import { isAsyncFunction, isClass, isGeneratorFunction, isObject, isPromise } from 'is-type-of';
77
import type { Logger } from 'egg-logger';
8-
import { getParamNames, readJSONSync, readJSON } from 'utility';
8+
import {
9+
getParamNames, readJSONSync, readJSON, exists,
10+
} from 'utility';
911
import { extend } from 'extend2';
1012
import { Request, Response, Application, Context as KoaContext } from '@eggjs/koa';
13+
import { register as tsconfigPathsRegister } from 'tsconfig-paths';
14+
import { isESM, isSupportTypeScript } from '@eggjs/utils';
1115
import { pathMatching, type PathMatchingOptions } from 'egg-path-matching';
1216
import { now, diff } from 'performance-ms';
1317
import { 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>) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.plugin = 'override plugin';
2+
3+
exports.middleware = [];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
agg: {
5+
path: path.join(__dirname, '../plugins/g'),
6+
},
7+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "plugin-ts-src"
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"eggPlugin": {
3+
"name": "g",
4+
"exports": {
5+
"require": "./dist/commonjs",
6+
"import": "./dist/esm",
7+
"typescript": "./src"
8+
}
9+
},
10+
"version": "1.0.0"
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 'bar';

test/loader/mixin/load_plugin.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,13 @@ describe('test/loader/mixin/load_plugin.test.ts', () => {
222222
assert(!message);
223223
});
224224

225+
it('should load plugin when eggPlugin.exports.typescript = "./src" exists', async () => {
226+
app = createApp('plugin-ts-src');
227+
const loader = app.loader;
228+
await loader.loadPlugin();
229+
assert.match(loader.allPlugins.agg.path!, /src$/);
230+
});
231+
225232
it('should loadConfig plugins with custom plugins config', async () => {
226233
const baseDir = getFilepath('plugin');
227234
const plugins = {

0 commit comments

Comments
 (0)