diff --git a/example/ts-gen-plugins/typechain/index.ts b/example/ts-gen-plugins/typechain/index.ts index 54f5ee4..621c1d4 100644 --- a/example/ts-gen-plugins/typechain/index.ts +++ b/example/ts-gen-plugins/typechain/index.ts @@ -15,7 +15,7 @@ interface TOptions { output?: string; } -export default class Typechain extends TsGeneratorPlugin { +export class Typechain extends TsGeneratorPlugin { name = "Typechain"; private readonly runtimePathAbs: string; diff --git a/src/plugins/loadPlugin.ts b/src/plugins/loadPlugin.ts index 85b1e10..d5ba8f6 100644 --- a/src/plugins/loadPlugin.ts +++ b/src/plugins/loadPlugin.ts @@ -1,10 +1,29 @@ import { resolvePlugin } from "./resolvePlugin"; import { TDeps } from "../deps"; import { TContext, TsGeneratorPlugin } from "./types"; +import { Dictionary } from "../stl"; export function loadPlugin(deps: TDeps, ctx: TContext): TsGeneratorPlugin { const pluginPath = resolvePlugin(deps, ctx.rawConfig.generator, ctx.cwd); - const PluginCtr = require(pluginPath).default as { new (ctx: TContext): TsGeneratorPlugin }; + const PluginModule = require(pluginPath); + + const moduleExportsCount = Object.keys(PluginModule).length; + if (moduleExportsCount !== 1) { + throw new Error( + `Loading plugin ${ + ctx.rawConfig.generator + } failed. Plugin module has to export exactly one entity. Found ${moduleExportsCount} instead`, + ); + } + + const PluginCtr = getFirstKey<{ new (ctx: TContext): TsGeneratorPlugin }>(PluginModule); return new PluginCtr(ctx); } + +export function getFirstKey(object: Dictionary): T { + for (const k of Object.keys(object)) { + return object[k]; + } + throw new Error("Any key missing!"); +} diff --git a/test/integration/ts-gen-plugins/dummy-json/index.ts b/test/integration/ts-gen-plugins/dummy-json/index.ts index 2cec5b0..a861622 100644 --- a/test/integration/ts-gen-plugins/dummy-json/index.ts +++ b/test/integration/ts-gen-plugins/dummy-json/index.ts @@ -3,7 +3,7 @@ import { join } from "path"; const generateType = (name: string, type: string): string => `export const ${name}: ${type}`; -export default class JsonPlugin extends TsGeneratorPlugin { +export class JsonPlugin extends TsGeneratorPlugin { name = "JsonPlugin"; transformFile({ path, contents }: TFileDesc): TFileDesc {