Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support load plugin from typescript dir #287

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { 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 @@
readonly appInfo: EggAppInfo;
dirs?: EggDirInfo[];


/**
* @class
* @param {Object} options - options
Expand Down Expand Up @@ -95,12 +98,11 @@
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 @@
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 @@
}
}

#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
Loading