How to use external libaries of type “module”

  • Christopher Steinbach
        4 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 Decker
            Has successfully completed the online course Intermediate (200)
          4 months ago #47514
          Up
          3
          Down
          ::

          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/import

          For example when using import inside a Simplifier library:

          import(“https://mozilla.github.io/pdf.js/build/pdf.mjs&#8221;).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
          Steffen

          Christopher Steinbach
              4 months ago #47520
              Up
              1
              Down
              ::

              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 Henninger
                  Has successfully completed the online course Introduction
                  Has 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)
                4 months ago #47513
                Up
                0
                Down
                ::

                Hi Christopher,

                as far as I am aware, this currently not supported.
                I think it would be a good idea to create a FR for this, as this could be very handy overall.

                Kind regards

                Lukas

              Viewing 4 posts - 1 through 4 (of 4 total)

              You must be logged in to reply to this topic.