-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Support for building with ESM-only libraries #180
Comments
Since dynamic import has to follow CORS rules, here is an implementation to workaround that using GM_fetch/GM_xhr: async function importShim<ModuleType>(url: string): Promise<ModuleType> {
const script = await GM_fetch(url).then(res => res.text()); // recommend @trim21/gm-fetch
const scriptBlob = new Blob([script], { type: "text/javascript" });
const importUrl = URL.createObjectURL(scriptBlob);
return import(importUrl);
} Of course, this will only work if CSP allows dynamic import from blob URLs. Don't forget there is also |
you can use but it is not supported that target module text |
I've never seen
The |
sorry, it should be vite-plugin-monkey/packages/vite-plugin-monkey/src/client/index.ts Lines 66 to 71 in d8127bd
|
if your target module is the following code // it import another remote relative module
export * from './' your |
You're right. The The naming for |
Actually, we can create async function importShim(url) {
// for importing external modules outside of its own origin or using GM_getResourceText
if (url.startsWith("http:") || url.startsWith("https:") || url.startsWith("gm-resource:")) {
let scriptText = url.startsWith("gm-resource:")
? await (GM_getResourceText || GM.getResourceText)(url.split(":")[1])
: await GM_fetch(url).then(res => res.text());
const scriptBlob = new Blob([scriptText], { type: "text/javascript" });
const importUrl = URL.createObjectURL(scriptBlob);
return import(importUrl);
}
return import(url);
}
|
I'd like support for building with packages that only provide ESM dist, one example is jsx-dom, which doesn't provide UMD/IIFE dist, but only providing ESM.
We can already use ESM dist in the userscript via dynamic import in async IIFE:
Maybe we can add a new
build.externalDynamicImports
configuration that'll automatically resolve ESM-only external modules to dynamic import:(Edited for more clarity)
The text was updated successfully, but these errors were encountered: