Plugins: fix loading of modules which resolve to Promises (#82299)

* Plugins: fix loading of modules which resolve to Promises

Prior to this commit we expected the default export of a plugin module
to be an object with a `plugin` field.

This is the case for the vast majority of plugins, but if a plugin uses
webpack's `asyncWebAssembly` feature then the default export will actually
be a promise which resolves to such an object.

This commit checks the result of the SystemJS import to make sure it has
a `plugin` field. If not, and if the `default` field looks like a Promise,
it recursively attempts to resolve the Promise until the object looks like
a plugin.

I think this may have broken with the SystemJS upgrade (#70068) because it used
to work without this change in Grafana 10.1, but it's difficult to say
for sure.

* Use Promise.resolve instead of await to clean up some logic

* Override systemJSPrototype.import instead of handling defaults inside importPluginModule

* Add comment to explain why we're overriding systemJS' import

Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>

---------

Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
This commit is contained in:
Ben Sully
2024-02-21 13:25:00 +00:00
committed by GitHub
parent 809c1eaddb
commit be71277d33

View File

@ -29,6 +29,17 @@ const systemJSPrototype: SystemJSWithLoaderHooks = SystemJS.constructor.prototyp
// the content of the plugin code at runtime which can only be done with fetch/eval.
systemJSPrototype.shouldFetch = () => true;
const originalImport = systemJSPrototype.import;
// Hook Systemjs import to support plugins that only have a default export.
systemJSPrototype.import = function (...args: Parameters<typeof originalImport>) {
return originalImport.apply(this, args).then((module) => {
if (module && module.__useDefault) {
return module.default;
}
return module;
});
};
const systemJSFetch = systemJSPrototype.fetch;
systemJSPrototype.fetch = function (url: string, options?: Record<string, unknown>) {
return decorateSystemJSFetch(systemJSFetch, url, options);