fedwiki / wiki-client

Federated wiki client-side javascript as a npm module.
Other
115 stars 36 forks source link

Use dynamic import to load plugin client code #320

Closed nrn closed 2 months ago

nrn commented 3 months ago

I know it seems insane, but this seems to just work.

By using the built in import expression we can dynamically load scrips like we have been, but those scrips in turn can now use import statements and easily load their own dependencies without a build step. For a plugin that would often mean pulling from its own plugin folder like

import { test } from '/plugins/mech/test.mjs'

The grunt change is required to prevent babel from replacing native dynamic imports with it's own stuff. This is the only call to loadScript so it was easy to cut the unused options argument, and convert the response to normal promise methods.

This is going to require a lot of testing.

WardCunningham commented 3 months ago

I wonder how far back in browser versions we would have to go before this won't work? On a truly obsolete browser one would still have access to the built-in plugins. This would be a feature simplifying the life of new plugin authors and certainly a step in the right direction.

Oh, I see this has been considered in #189

paul90 commented 3 months ago

A number of plugins already import their own dependencies, without a build script. Albeit using a dynamic import, like const Plot = await import('https://cdn.jsdelivr.net/npm/@observablehq/plot@0.6/+esm').

But switching to static import would simplify importing things that are always needed, but dynamic is probably probably preferred for things that are not always needed.

Should work in quite old browsers, https://caniuse.com/?search=import(),

nrn commented 3 months ago

Yeah, that's a good point, we could use dynamic imports in plugin code since they can be used from either scripts or modules in the browser, or in node from CJS or .jsm, and can cross load any of them from any situation. This just seems like a really convenient boundary to put dynamic imports at so we can use import statements to simplify things in plugin land.

paul90 commented 3 months ago

Some plugins, such as code, which themselves are using dynamic plugins fail with this change. Others appear to work.

nrn commented 3 months ago

Aargh, caught out by strict mode compatibility! It's an easy fix in the code plugin, but I'm not sure how many might be a problem.

paul90 commented 3 months ago

Have created PRs to update the D3 plugins.

nrn commented 3 months ago

Took a look over them and these all look good, thank you!

I'll try and get them installed tonight or tomorrow to take them for a quick test drive.

paul90 commented 2 months ago

Finally got around to a rc for the change to the code plugin - it is installed on the test server. The only update not installed there is the change to the graphviz plugin.

paul90 commented 2 months ago

Merging as discussed in Sunday's call.

Have published a release candidate, but if used will need the updates from the plugins referenced above.

The next release will also include the changes in #321.