Skip to content

Commit

Permalink
fix(plugin-system): Updated the resolver implementation to use `enhan…
Browse files Browse the repository at this point in the history
…ced-resolve` package
  • Loading branch information
sullivanpj committed Jan 23, 2024
1 parent cd93fe2 commit bda2c10
Show file tree
Hide file tree
Showing 11 changed files with 2,242 additions and 4,655 deletions.
761 changes: 144 additions & 617 deletions docs/api-reports/packages/plugin-system/api-report.md

Large diffs are not rendered by default.

5,480 changes: 2,011 additions & 3,469 deletions docs/api-reports/packages/plugin-system/documents-model.api.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions docs/website/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const nextConfig = {
// https://nextjs.org/docs/app/api-reference/next-config-js/poweredByHeader
poweredByHeader: false,

// biome-ignore lint/nursery/useAwait: <explanation>
rewrites: async () => {
return [
{ source: "/healthz", destination: "/api/health" },
Expand All @@ -53,6 +54,7 @@ const nextConfig = {
];
},

// biome-ignore lint/nursery/useAwait: <explanation>
redirects: async () => {
try {
return get("redirects");
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,6 @@
"@storm-stack/docs-website": "workspace:*",
"@storm-stack/frontend-table": "workspace:*",
"@storm-stack/plugin-system": "workspace:*"
},
"patchedDependencies": {
"oxc-resolver@0.1.1": "patches/oxc-resolver@0.1.1.patch"
}
},
"triggerEmptyDevReleaseByIncrementingThisNumber": 0
Expand Down
15 changes: 2 additions & 13 deletions packages/plugin-system/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
},
"type": "module",
"dependencies": {
"enhanced-resolve": "^5.15.0",
"glob": "^10.3.10",
"md5": "^2.3.0",
"oxc-resolver": "^0.1.1",
"pnpapi": "^0.0.0",
"toposort": "^2.0.2"
},
"devDependencies": {
Expand All @@ -21,17 +22,5 @@
},
"publishConfig": {
"access": "public"
},
"optionalDependencies": {
"@oxc-resolver/binding-darwin-arm64": "^0.1.1",
"@oxc-resolver/binding-darwin-x64": "^0.1.1",
"@oxc-resolver/binding-linux-arm-gnueabihf": "^0.1.1",
"@oxc-resolver/binding-linux-arm64-gnu": "^0.1.1",
"@oxc-resolver/binding-linux-arm64-musl": "^0.1.1",
"@oxc-resolver/binding-linux-x64-gnu": "^0.1.1",
"@oxc-resolver/binding-linux-x64-musl": "^0.1.1",
"@oxc-resolver/binding-wasm32-wasi": "^0.1.1",
"@oxc-resolver/binding-win32-arm64-msvc": "^0.1.1",
"@oxc-resolver/binding-win32-x64-msvc": "^0.1.1"
}
}
1 change: 1 addition & 0 deletions packages/plugin-system/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"defaultConfiguration": "production",
"skipNativeModulesPlugin": true,
"platform": "node",
"external": ["pnpapi"],
"assets": [
{
"input": "packages/plugin-system",
Expand Down
15 changes: 5 additions & 10 deletions packages/plugin-system/src/loader/plugin-loader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { StormDateTime } from "@storm-stack/date-time";
import { StormError } from "@storm-stack/errors";
import { findFileName, findFilePath, isDirectory, isFile } from "@storm-stack/file-system";
import type { ResolverFactory } from "oxc-resolver";
import { PluginSystemErrorCode } from "../errors";
import type { IPluginLoader, IPluginModule, PluginDefinition, PluginInstance } from "../types";
import { createResolver } from "../utilities/create-resolver";
Expand All @@ -14,7 +12,7 @@ export abstract class PluginLoader<
TPluginModule extends IPluginModule<TContext> = IPluginModule<TContext>
> implements IPluginLoader<TContext, TPluginModule>
{
protected resolver: ResolverFactory;
protected resolver: (request: string) => Promise<string>;

public constructor(public readonly tsconfig: string) {
this.resolver = createResolver(tsconfig);
Expand Down Expand Up @@ -67,16 +65,13 @@ export abstract class PluginLoader<
};

protected resolve = async (definition: PluginDefinition, _: Record<string, any> = {}) => {
const resolved = this.resolver.sync(
isDirectory(definition.provider) ? definition.provider : findFilePath(definition.provider),
isFile(definition.provider) ? findFileName(definition.provider) : "./index.js"
);
if (resolved.error || !resolved.path) {
const resolved = await this.resolver(definition.provider);
if (!resolved) {
throw new StormError(PluginSystemErrorCode.plugin_loading_failure, {
message: resolved.error
message: `Cannot find plugin ${definition.provider}`
});
}

return await import(resolved.path);
return await import(resolved);
};
}
26 changes: 7 additions & 19 deletions packages/plugin-system/src/manager/plugin-manager.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { readFile } from "node:fs/promises";
import { StormDateTime } from "@storm-stack/date-time";
import { StormError } from "@storm-stack/errors";
import {
exists,
findContainingFolder,
findFileName,
findFilePath,
isDirectory,
isFile,
joinPaths
} from "@storm-stack/file-system";
import { exists, findContainingFolder, findFilePath, joinPaths } from "@storm-stack/file-system";
import type { IStormLog } from "@storm-stack/logging";
import { StormParser } from "@storm-stack/serialization";
import {
Expand All @@ -24,7 +16,6 @@ import {
} from "@storm-stack/utilities";
import { glob } from "glob";
import md5 from "md5";
import type { ResolverFactory } from "oxc-resolver";
import toposort from "toposort";
import { PluginSystemErrorCode } from "../errors";
import {
Expand Down Expand Up @@ -53,7 +44,7 @@ export class PluginManager<TContext = any, TPluginModule extends IPluginModule<T
private _logger: IStormLog;

private _loaders: Map<string, IPluginLoader<TContext, TPluginModule>>;
private _loaderResolver: ResolverFactory;
private _loaderResolver: (request: string) => Promise<string>;

public static create = async <
TContext = any,
Expand Down Expand Up @@ -97,7 +88,7 @@ export class PluginManager<TContext = any, TPluginModule extends IPluginModule<T
this._store = new Map<string, PluginInstance<TContext, TPluginModule>>();
this._hooks = new Map<string, PluginHookFn<TContext>[]>();
this._loaders = new Map<string, IPluginLoader<TContext, TPluginModule>>();
this._loaderResolver = createResolver(this._options.tsconfig);
this._loaderResolver = createResolver(options.rootPath);
}

public discover = async (): Promise<Map<string, PluginDefinition>> => {
Expand Down Expand Up @@ -361,17 +352,14 @@ export class PluginManager<TContext = any, TPluginModule extends IPluginModule<T

let module!: IPluginLoader<TContext, TPluginModule>;
try {
const resolved = this._loaderResolver.sync(
isDirectory(loader) ? loader : findFilePath(loader),
isFile(loader) ? findFileName(loader) : "./index.js"
);
if (resolved.error || !resolved.path) {
const resolved = await this._loaderResolver(loader);
if (!resolved) {
throw new StormError(PluginSystemErrorCode.module_not_found, {
message: resolved.error
message: `Cannot find plugin loader ${loader}`
});
}

module = await import(resolved.path);
module = await import(resolved);
} catch (origError) {
this._logger.error(`Unable to initialize loader module ${loader}: ${origError}`);

Expand Down
51 changes: 41 additions & 10 deletions packages/plugin-system/src/utilities/create-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
import { isFile } from "@storm-stack/file-system";
import { ResolverFactory } from "oxc-resolver";
import fs from "node:fs";
import { StormError } from "@storm-stack/errors";
import { isString } from "@storm-stack/utilities";
import { CachedInputFileSystem, ResolverFactory } from "enhanced-resolve";
import { PluginSystemErrorCode } from "..";

/**
* Create a resolver factory
*
* @param tsconfig - The path to the tsconfig file
* @returns The resolver factory
*/
export const createResolver = (tsconfig: string): ResolverFactory => {
const resolverFactory = new ResolverFactory({
tsconfig: {
configFile: isFile(tsconfig) ? tsconfig : `${tsconfig}/tsconfig.json`,
},
descriptionFiles: ["package.json", "plugin.json"],
});
export const createResolver = (rootPath?: string): ((request: string) => Promise<string>) => {
const resolverFactory = ResolverFactory.createResolver({
fileSystem: new CachedInputFileSystem(fs, 4000),
extensions: [".js", ".json"],
descriptionFiles: ["package.json", "plugin.json"]
});

return resolverFactory;
return (request: string) => {
const resolveContext = {
issuer: rootPath
};

return new Promise((resolve, reject) => {
resolverFactory.resolve(
resolveContext,
rootPath ? rootPath : __dirname,
request,
{},
(error, result) => {
if (error) {
reject(error);
} else {
if (!result || !isString(result)) {
reject(
StormError.create({
code: PluginSystemErrorCode.module_not_found,
message: `Cannot find plugin ${request}`
})
);
}

resolve(result as string);
}
}
);
});
};
};

0 comments on commit bda2c10

Please sign in to comment.