fix: js library default export to a unique name instead of default key in self (#36483)
This commit is contained in:
parent
9731728956
commit
2443b8ce9d
|
|
@ -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,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user