Custom Code in Global Action Library

Is this possible to do, I want to use PDFLib in my Global Actions, but currently I cannot import it as I cannot use custom code there

Hi @ZaneLesley,

It’s all a bit weird, to be honest. The actions in the Action Library allow require, but not the normal static import. But when I tried using require I couldn’t load any module.

But, dynamic import works…

…meaning you can do something like:

import("https://cdn.jsdelivr.net/npm/pdf-lib@1.17.1/dist/pdf-lib.esm.min.js")
.then(pdfLib => console.log(pdfLib.rgb(0, 0.53, 0.71)));

image

So what I would recommend to you is using Promise.all() so you can add as many libraries as you want in one go and also adding it to the global window object. That way, once loaded, the libraries are available in all app-specific and global actions.

const [pdfLib] = await Promise.all([
  import("https://cdn.jsdelivr.net/npm/pdf-lib@1.17.1/dist/pdf-lib.esm.min.jss"),
]).catch(err => ...);

window.PDFDocument = pdfLib.PDFDocument;