From 56d4fbb39fb2b7fc190f793d68d82048ace40a05 Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Fri, 15 Oct 2021 22:09:55 +0100 Subject: [PATCH] fix: Don't kludge accessors in compiled mode (#5591) In compiled mode we don't need to add exports to the global Blockly object because they'll already be there - and attempting to do so causes problems when a project imports multiple separate copies of Blockly (which it shouldn't, but many plugins do). This is part of the fix for google/blockly-samples#902. --- core/blockly.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/core/blockly.js b/core/blockly.js index 7120e6fb6..56e9e0010 100644 --- a/core/blockly.js +++ b/core/blockly.js @@ -742,7 +742,24 @@ exports.zelos = zelos; // declareLegacyNamespace only copies normal data properties, not // accessors. This can be removed once all remaining calls to // declareLegacyNamspace have been removed. -if (globalThis.Blockly && typeof globalThis.Blockly === 'object') { +// +// This is only needed in uncompiled mode (see +// google/blockly-samples#902); in compiled mode the exports object is +// already the value of globalThis.Blockly. Because +// closure/goog/base.js is not included in the compiler input, we +// can't use goog.global['COMPILED'] to check if we are running in +// compiled mode. Instead, use existence of globalThis.goog itself +// for this purpose. +// +// Note that this code will still attempt to redefine accessors on a +// previously-imported copy of the Blockly library if both are +// imported in uncompiled mode. This will fail with TypeError as the +// accessors are nonconfigurable (which is good, as otherwise one +// accessors on one copy would call get/set functions on the other +// copy!) +if (globalThis.goog && globalThis.Blockly && + typeof globalThis.Blockly === 'object' && + globalThis.Blockly !== exports) { const descriptors = Object.getOwnPropertyDescriptors(exports); const accessors = {}; for (const key in descriptors) {