diff --git a/app/client/src/workers/Evaluation/handlers/__tests__/jsLibrary.test.ts b/app/client/src/workers/Evaluation/handlers/__tests__/jsLibrary.test.ts index 9727c496fb8..9b9a4c1b23d 100644 --- a/app/client/src/workers/Evaluation/handlers/__tests__/jsLibrary.test.ts +++ b/app/client/src/workers/Evaluation/handlers/__tests__/jsLibrary.test.ts @@ -11,7 +11,17 @@ declare const self: WorkerGlobalScope; describe("Tests to assert install/uninstall flows", function () { beforeAll(() => { - self.importScripts = jest.fn(() => { + self.importScripts = jest.fn((url: string) => { + if (url.includes("jspdf-autotable")) { + const defaultVar = function () {}; + + defaultVar.Cell = function () {}; + self.Cell = function () {}; + self.default = defaultVar; + + return; + } + self.lodash = {}; }); @@ -150,4 +160,28 @@ describe("Tests to assert install/uninstall flows", function () { method: "Hello", }); }); + + it("should install a library with default export", async function () { + const res = await installLibrary({ + data: { + url: "https://cdn.jsdelivr.net/npm/jspdf-autotable@3.5.28/dist/jspdf.plugin.autotable.js", + takenAccessors: [], + takenNamesMap: {}, + }, + method: EVAL_WORKER_ASYNC_ACTION.INSTALL_LIBRARY, + webworkerTelemetry: {}, + }); + + expect(self.importScripts).toHaveBeenCalled(); + expect(mod.makeTernDefs).toHaveBeenCalledWith({}); + + expect(res).toEqual({ + accessor: ["jspdf_plugin_autotable_js"], + defs: { + "!name": "LIB/jspdf_plugin_autotable_js", + jspdf_plugin_autotable_js: undefined, + }, + success: true, + }); + }); }); diff --git a/app/client/src/workers/Evaluation/handlers/jsLibrary.ts b/app/client/src/workers/Evaluation/handlers/jsLibrary.ts index 87d22747c32..bdf704f2bab 100644 --- a/app/client/src/workers/Evaluation/handlers/jsLibrary.ts +++ b/app/client/src/workers/Evaluation/handlers/jsLibrary.ts @@ -128,10 +128,40 @@ export async function installLibrary( // Find keys add that were installed to the global scope. const keysAfterInstallation = Object.keys(self); - accessors.push( - ...difference(keysAfterInstallation, envKeysBeforeInstallation), + const differentiatingKeys = difference( + keysAfterInstallation, + envKeysBeforeInstallation, ); + if ( + differentiatingKeys.length > 0 && + differentiatingKeys.includes("default") + ) { + // Changing default export to library specific name + const uniqueName = generateUniqueAccessor( + url, + takenAccessors, + takenNamesMap, + ); + + // mapping default functionality to library name accessor + self[uniqueName] = self["default"]; + // deleting the reference of default key from the self object + delete self["default"]; + // mapping all the references of differentiating keys from the self object to the self[uniqueName] key object + differentiatingKeys.map((key) => { + if (key !== "default") { + self[uniqueName][key] = self[key]; + // deleting the references from the self object + delete self[key]; + } + }); + // pushing the uniqueName to the accessor array + accessors.push(uniqueName); + } else { + accessors.push(...differentiatingKeys); + } + /** * Check the list of installed library to see if their values have changed. * This is to check if the newly installed library overwrites an already existing one