refactor: simplify plugin module loading

This commit is contained in:
Peter Steinberger
2026-05-02 01:41:05 +01:00
parent f6f8e6e242
commit 23fd8a90f9
39 changed files with 704 additions and 527 deletions

View File

@@ -1,12 +1,15 @@
import fs from "node:fs";
import path from "node:path";
import { getRuntimeConfig } from "../../config/config.js";
import { getCachedPluginJitiLoader, type PluginJitiLoaderCache } from "../jiti-loader-cache.js";
import { loadPluginManifestRegistry } from "../manifest-registry.js";
import {
isJavaScriptModulePath,
tryNativeRequireJavaScriptModule,
} from "../native-module-require.js";
import {
getCachedPluginSourceModuleLoader,
type PluginModuleLoaderCache,
} from "../plugin-module-loader-cache.js";
import type { PluginOrigin } from "../plugin-origin.types.js";
type PluginRuntimeRecord = {
@@ -109,20 +112,19 @@ export function resolvePluginRuntimeModulePath(
return null;
}
function getPluginBoundarySourceLoader(modulePath: string, loaders: PluginJitiLoaderCache) {
return getCachedPluginJitiLoader({
function getPluginBoundarySourceLoader(modulePath: string, loaders: PluginModuleLoaderCache) {
return getCachedPluginSourceModuleLoader({
cache: loaders,
modulePath,
importerUrl: import.meta.url,
jitiFilename: import.meta.url,
tryNative: false,
loaderFilename: import.meta.url,
});
}
// oxlint-disable-next-line typescript/no-unnecessary-type-parameters -- Dynamic plugin boundary loaders use caller-supplied module types.
export function loadPluginBoundaryModule<TModule>(
modulePath: string,
loaders: PluginJitiLoaderCache,
loaders: PluginModuleLoaderCache,
options: { origin?: PluginOrigin } = {},
): TModule {
if (isJavaScriptModulePath(modulePath)) {

View File

@@ -8,7 +8,7 @@ import {
optimizeImageToJpeg as optimizeImageToJpegImpl,
} from "../../media/web-media.js";
import type { PollInput } from "../../polls.js";
import type { PluginJitiLoaderCache } from "../jiti-loader-cache.js";
import type { PluginModuleLoaderCache } from "../plugin-module-loader-cache.js";
import type { PluginOrigin } from "../plugin-origin.types.js";
import {
loadPluginBoundaryModule,
@@ -109,7 +109,7 @@ let cachedHeavyModule: WebChannelHeavyRuntimeModule | null = null;
let cachedLightModulePath: string | null = null;
let cachedLightModule: WebChannelLightRuntimeModule | null = null;
const jitiLoaders: PluginJitiLoaderCache = new Map();
const moduleLoaders: PluginModuleLoaderCache = new Map();
function resolveWebChannelPluginRecord(): WebChannelPluginRecord {
return resolvePluginRuntimeRecordByEntryBaseNames(["light-runtime-api", "runtime-api"], () => {
@@ -135,7 +135,7 @@ function resolveWebChannelRuntimeModulePath(
function loadCurrentHeavyModuleSync(): WebChannelHeavyRuntimeModule {
const record = resolveWebChannelPluginRecord();
const modulePath = resolveWebChannelRuntimeModulePath(record, "runtime-api");
return loadPluginBoundaryModule<WebChannelHeavyRuntimeModule>(modulePath, jitiLoaders, {
return loadPluginBoundaryModule<WebChannelHeavyRuntimeModule>(modulePath, moduleLoaders, {
origin: record.origin,
});
}
@@ -146,7 +146,7 @@ function loadWebChannelLightModule(): WebChannelLightRuntimeModule {
if (cachedLightModule && cachedLightModulePath === modulePath) {
return cachedLightModule;
}
const loaded = loadPluginBoundaryModule<WebChannelLightRuntimeModule>(modulePath, jitiLoaders, {
const loaded = loadPluginBoundaryModule<WebChannelLightRuntimeModule>(modulePath, moduleLoaders, {
origin: record.origin,
});
cachedLightModulePath = modulePath;
@@ -160,7 +160,7 @@ async function loadWebChannelHeavyModule(): Promise<WebChannelHeavyRuntimeModule
if (cachedHeavyModule && cachedHeavyModulePath === modulePath) {
return cachedHeavyModule;
}
const loaded = loadPluginBoundaryModule<WebChannelHeavyRuntimeModule>(modulePath, jitiLoaders, {
const loaded = loadPluginBoundaryModule<WebChannelHeavyRuntimeModule>(modulePath, moduleLoaders, {
origin: record.origin,
});
cachedHeavyModulePath = modulePath;