Skip to content
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

Importing javascript es module from blob url or data url does not work #436

Closed
stad-nico opened this issue Oct 25, 2024 · 3 comments · Fixed by #437
Closed

Importing javascript es module from blob url or data url does not work #436

stad-nico opened this issue Oct 25, 2024 · 3 comments · Fixed by #437

Comments

@stad-nico
Copy link

I am using angulars native federation to dynamically import javascript plugins in runtime. Because I am using autogenerated services from openapi I cannot use await import(<pluginUrl>) but instead the service fetches the content and provides it as a blob.
However creating a blob url or data url and importing this url does not work:

this.pluginsService.getSourceCode(plugin.id, 'response').pipe(
    tap(async (p) => {
        const blob = new Blob([p.body!], { type: 'text/javascript' });
        const blobUrl = URL.createObjectURL(blob);
        const m = await import(blobUrl);
    })
).subscribe()

fails with

TypeError: Failed to construct 'URL': Invalid URL
    at pushSourceURL (es-module-shims.js:744:25)
    at resolveDeps (es-module-shims.js:761:7)
    at topLevelLoad (es-module-shims.js:604:5)

However creating a script tag and using innerHTML to load the js does work:

const script = document.createElement("script");
script.type = "module-shim";
script.innerHTML = await blob.text();
document.body.append(script)

But this way you dont have access to the exports of the js module.

@guybedford
Copy link
Owner

Blobs should be supported just fine. It seems like the issue is something in the dependency graph that you have an invalid URL. Try adding a log statement at that line to see what URL is not a valid URL being generated and that might well illuminate the root case.

Added a test in #437 that verifies blob URLs do behave correctly.

@stad-nico
Copy link
Author

stad-nico commented Oct 29, 2024

function pushSourceURL (commentPrefix, commentStart) {
    const urlStart = commentStart + commentPrefix.length;
    const commentEnd = source.indexOf('\n', urlStart);
    const urlEnd = commentEnd !== -1 ? commentEnd : source.length;
    pushStringTo(urlStart);
    console.log(source.slice(urlStart, urlEnd), load.r)
    resolvedSource += new URL(source.slice(urlStart, urlEnd), load.r).href;
    lastIndex = urlEnd;
}

logs pihub-plugin-ui.mjs.map blob:http://localhost:4200/f4a62098-42b3-4c5f-9938-bcdb8190d372.

I am not sure which part causes the error, the .js.map extension is being logged by other imported files and it works fine for those, so maybe the base blob:http://localhost:4200/f4a62098-42b3-4c5f-9938-bcdb8190d372 is the issue?

@guybedford
Copy link
Owner

Thanks, that helped track it down. Fix in #437.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants