How to use external libaries of type “module”
-
Christopher Steinbach5 months ago #47489
Hi Simplifier Community,
how can I use a external library that requires a script type “module”. Like for example pdfjs@v4 is now using js modules.
<script src=”//mozilla.github.io/pdf.js/build/pdf.mjs” type=”module”></script>
Is there an argument for Simplifier´s addScript-method to set the script type?
Out of the box addScript is not using module. This means you get an error “unexpected token export”.
Maybe there is a solution to create a custom script tag with a preload script loaded with addScript. But I don’t have a enough knowledge how simplifier bundes his libraries to implement such a solution.
Best Regards
Christopher
Steffen DeckerHas successfully completed the online course Intermediate (200)5 months ago #47514::Hi Christopher,
until we officially support static modules, you can also dynamically load modules via the official import() declaration. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/importFor example when using import inside a Simplifier library:
import(“https://mozilla.github.io/pdf.js/build/pdf.mjs”).then((pdflib) => {
window.pdflib= pdflib;
});You can of course use the import function directly inside a client side business object, without exposing the loaded library as a window property.
Kind regards
SteffenChristopher Steinbach5 months ago #47520::Hi Steffen,
thank you for this great idea. I think that does the trick for me.
Here’s my final solution for the latest pdfJS libarary using modules:
const fullLibPath =
${SimplifierLoader.libraryRoot}/${sLibPath}
;
import(${fullLibPath}/pdf.min.mjs
).then((pdfjsLib) => {
window.pdfjsLib = pdfjsLib;
window.pdfjsLib.GlobalWorkerOptions.workerSrc =${fullLibPath}/pdf.worker.min.mjs
;
});I added the simplifier lib path to use the pdfjs lib uploaded to the simplifier server instead of the mozilla served lib.
Lukas HenningerHas successfully completed the online course IntroductionHas successfully completed the online course Intermediate (200)Has successfully completed the online course Advanced (300)Has successfully completed the online course Basics (100)Has successfully completed the online course Advanced (310)Has successfully completed the online course Advanced (320)
You must be logged in to reply to this topic.