Skip to content

Commit

Permalink
fix: Don't kludge accessors in compiled mode (google#5591)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cpcallen authored Oct 15, 2021
1 parent 90b3f75 commit 56d4fbb
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 56d4fbb

Please sign in to comment.