Skip to content

Commit

Permalink
fix: support load plugin from typescript dir
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 4, 2025
1 parent ca75960 commit 6f41af7
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dependencies": {
"@eggjs/koa": "^2.20.6",
"@eggjs/router": "^3.0.5",
"@eggjs/utils": "^4.1.5",
"@eggjs/utils": "^4.2.4",
"egg-logger": "^3.5.0",
"egg-path-matching": "^2.0.0",
"extend2": "^4.0.0",
Expand Down
46 changes: 29 additions & 17 deletions src/loader/egg_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import { debuglog, inspect } from 'node:util';
import { homedir } from 'node-homedir';
import { isAsyncFunction, isClass, isGeneratorFunction, isObject, isPromise } from 'is-type-of';
import type { Logger } from 'egg-logger';
import { getParamNames, readJSONSync, readJSON } from 'utility';
import {
getParamNames, readJSONSync, readJSON, exists,
} from 'utility';
import { extend } from 'extend2';
import { Request, Response, Application, Context as KoaContext } from '@eggjs/koa';
import { register as tsconfigPathsRegister } from 'tsconfig-paths';
import { isESM, isSupportTypeScript } from '@eggjs/utils';
import { pathMatching, type PathMatchingOptions } from 'egg-path-matching';
import { now, diff } from 'performance-ms';
import { CaseStyle, FULLPATH, FileLoader, FileLoaderOptions } from './file_loader.js';
Expand Down Expand Up @@ -65,7 +69,6 @@ export class EggLoader {
readonly appInfo: EggAppInfo;
dirs?: EggDirInfo[];


/**
* @class
* @param {Object} options - options
Expand Down Expand Up @@ -95,12 +98,11 @@ export class EggLoader {
if (process.env.EGG_TYPESCRIPT === 'true' || (this.pkg.egg && this.pkg.egg.typescript)) {
// skip require tsconfig-paths if tsconfig.json not exists
const tsConfigFile = path.join(this.options.baseDir, 'tsconfig.json');
// FIXME: support esm
if (fs.existsSync(tsConfigFile) && typeof require === 'function') {
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('tsconfig-paths').register({ cwd: this.options.baseDir });
if (fs.existsSync(tsConfigFile)) {
tsconfigPathsRegister({ cwd: this.options.baseDir } as any);
} else {
this.logger.info('[@eggjs/core/egg_loader] skip register "tsconfig-paths" because tsconfig.json not exists at %s',
this.logger.info(
'[@eggjs/core/egg_loader] skip register "tsconfig-paths" because tsconfig.json not exists at %s',
tsConfigFile);
}
}
Expand Down Expand Up @@ -599,7 +601,7 @@ export class EggLoader {
plugin.version = pkg.version;
}
// support commonjs and esm dist files
plugin.path = this.#formatPluginPathFromPackageJSON(plugin.path!, pkg);
plugin.path = await this.#formatPluginPathFromPackageJSON(plugin.path!, pkg);
}

const logger = this.options.logger;
Expand Down Expand Up @@ -753,26 +755,36 @@ export class EggLoader {
}
}

#formatPluginPathFromPackageJSON(pluginPath: string, pluginPkg: {
async #formatPluginPathFromPackageJSON(pluginPath: string, pluginPkg: {
eggPlugin?: {
exports?: {
import?: string;
require?: string;
typescript?: string;
};
};
}) {
if (pluginPkg.eggPlugin?.exports) {
if (typeof require === 'function') {
if (pluginPkg.eggPlugin.exports.require) {
pluginPath = path.join(pluginPath, pluginPkg.eggPlugin.exports.require);
}): Promise<string> {
let realPluginPath = pluginPath;
const exports = pluginPkg.eggPlugin?.exports;
if (exports) {
if (isESM) {
if (exports.import) {
realPluginPath = path.join(pluginPath, exports.import);
}
} else {
if (pluginPkg.eggPlugin.exports.import) {
pluginPath = path.join(pluginPath, pluginPkg.eggPlugin.exports.import);
if (exports.require) {
realPluginPath = path.join(pluginPath, exports.require);
}
}

Check warning on line 778 in src/loader/egg_loader.ts

View check run for this annotation

Codecov / codecov/patch

src/loader/egg_loader.ts#L775-L778

Added lines #L775 - L778 were not covered by tests
if (exports.typescript && isSupportTypeScript()) {
if (!(await exists(realPluginPath))) {
// if require/import path not exists, use typescript path for development stage
realPluginPath = path.join(pluginPath, exports.typescript);
debug('[formatPluginPathFromPackageJSON] use typescript path %o', realPluginPath);
}
}
}
return pluginPath;
return realPluginPath;
}

#extendPlugins(targets: Record<string, EggPluginInfo>, plugins: Record<string, EggPluginInfo>) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/plugin-ts-src/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.plugin = 'override plugin';

exports.middleware = [];
7 changes: 7 additions & 0 deletions test/fixtures/plugin-ts-src/config/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const path = require('path');

module.exports = {
agg: {
path: path.join(__dirname, '../plugins/g'),
},
};
3 changes: 3 additions & 0 deletions test/fixtures/plugin-ts-src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "plugin-ts-src"
}
11 changes: 11 additions & 0 deletions test/fixtures/plugin-ts-src/plugins/g/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"eggPlugin": {
"name": "g",
"exports": {
"require": "./dist/commonjs",
"import": "./dist/esm",
"typescript": "./src"
}
},
"version": "1.0.0"
}
1 change: 1 addition & 0 deletions test/fixtures/plugin-ts-src/plugins/g/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'bar';
7 changes: 7 additions & 0 deletions test/loader/mixin/load_plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ describe('test/loader/mixin/load_plugin.test.ts', () => {
assert(!message);
});

it('should load plugin when eggPlugin.exports.typescript = "./src" exists', async () => {
app = createApp('plugin-ts-src');
const loader = app.loader;
await loader.loadPlugin();
assert.match(loader.allPlugins.agg.path!, /src$/);
});

it('should loadConfig plugins with custom plugins config', async () => {
const baseDir = getFilepath('plugin');
const plugins = {
Expand Down

0 comments on commit 6f41af7

Please sign in to comment.