mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: Introduce magic modules to fe sandbox loader (#89274)
* feat(fe-sandbox): introduce module and exports magic modules to executed plugin code * refactor(fe-sandbox): rename module and exports vars for clarity
This commit is contained in:
parent
f0ff7d57b4
commit
e5474511d8
@ -162,7 +162,7 @@ async function doImportPluginModuleInSandbox(meta: SandboxPluginMeta): Promise<S
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const resolvedDeps = resolvePluginDependencies(dependencies, meta.id);
|
const resolvedDeps = resolvePluginDependencies(dependencies, meta);
|
||||||
// execute the plugin's code
|
// execute the plugin's code
|
||||||
const pluginExportsRaw = factory.apply(null, resolvedDeps);
|
const pluginExportsRaw = factory.apply(null, resolvedDeps);
|
||||||
// only after the plugin has been executed
|
// only after the plugin has been executed
|
||||||
@ -213,7 +213,21 @@ async function doImportPluginModuleInSandbox(meta: SandboxPluginMeta): Promise<S
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolvePluginDependencies(deps: string[], pluginId: string) {
|
/**
|
||||||
|
*
|
||||||
|
* This function resolves the dependencies using the array of AMD deps.
|
||||||
|
* Additionally it supports the RequireJS magic modules `module` and `exports`.
|
||||||
|
* https://github.com/requirejs/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#magic
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
function resolvePluginDependencies(deps: string[], pluginMeta: SandboxPluginMeta) {
|
||||||
|
const pluginExports = {};
|
||||||
|
const pluginModuleDep: ModuleMeta = {
|
||||||
|
id: pluginMeta.id,
|
||||||
|
uri: pluginMeta.module,
|
||||||
|
exports: pluginExports,
|
||||||
|
};
|
||||||
|
|
||||||
// resolve dependencies
|
// resolve dependencies
|
||||||
const resolvedDeps: CompartmentDependencyModule[] = [];
|
const resolvedDeps: CompartmentDependencyModule[] = [];
|
||||||
for (const dep of deps) {
|
for (const dep of deps) {
|
||||||
@ -222,10 +236,18 @@ function resolvePluginDependencies(deps: string[], pluginId: string) {
|
|||||||
resolvedDep = resolvedDep.default;
|
resolvedDep = resolvedDep.default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (dep === 'module') {
|
||||||
|
resolvedDep = pluginModuleDep;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dep === 'exports') {
|
||||||
|
resolvedDep = pluginExports;
|
||||||
|
}
|
||||||
|
|
||||||
if (!resolvedDep) {
|
if (!resolvedDep) {
|
||||||
const error = new Error(`[sandbox] Could not resolve dependency ${dep}`);
|
const error = new Error(`[sandbox] Could not resolve dependency ${dep}`);
|
||||||
logError(error, {
|
logError(error, {
|
||||||
pluginId,
|
pluginId: pluginMeta.id,
|
||||||
dependency: dep,
|
dependency: dep,
|
||||||
error: String(error),
|
error: String(error),
|
||||||
});
|
});
|
||||||
@ -235,3 +257,9 @@ function resolvePluginDependencies(deps: string[], pluginId: string) {
|
|||||||
}
|
}
|
||||||
return resolvedDeps;
|
return resolvedDeps;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ModuleMeta {
|
||||||
|
id: string;
|
||||||
|
uri: string;
|
||||||
|
exports: System.Module;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user