From a141fb3774315febd4fd269217b54c696e56c3e4 Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Wed, 14 Dec 2022 17:26:00 +0000 Subject: [PATCH] fix(tests): Fix bootstrapping of generators in compressed mode Previously we had code that would, in uncompiled mode, make sure that javascriptGenerator etc. were set to the corresponding module exports object (by fetching it with goog.module.get), but in compressed mode we made no effort to set (e.g.) javascriptGenerator to Blockly.JavaScript, which is where that export actually appears in the namespace tree when loading the chunk via a `); } diff --git a/tests/bootstrap_helper.js b/tests/bootstrap_helper.js index e9e7cd68ae6..4a22dadcb29 100644 --- a/tests/bootstrap_helper.js +++ b/tests/bootstrap_helper.js @@ -14,30 +14,60 @@ * undeclared dependencies on them. */ -/* eslint-disable-next-line no-undef */ -for (const require of window.bootstrapInfo.requires) { - goog.require(require); +(function() { + const info = window.bootstrapInfo; - // If require is a top-level chunk, create a global variable for it. - // This replaces the goog.module.declareLegacyNamespace calls that - // previously existed in each chunk entrypoint. - const exportName = { - 'Blockly.Dart': 'dartGenerator', - 'Blockly.Dart.all': 'dartGenerator', - 'Blockly.JavaScript': 'javascriptGenerator', - 'Blockly.JavaScript.all': 'javascriptGenerator', - 'Blockly.Lua': 'luaGenerator', - 'Blockly.Lua.all': 'luaGenerator', - 'Blockly.PHP': 'phpGenerator', - 'Blockly.PHP.all': 'phpGenerator', - 'Blockly.Python': 'pythonGenerator', - 'Blockly.Python.all': 'pythonGenerator', - }[require]; - if (exportName) { - window[exportName] = goog.module.get(require)[exportName]; - } else if (require === 'Blockly') { - window.Blockly = goog.module.get(require); - } else if (require === 'Blockly.libraryBlocks') { - window.libraryBlocks = goog.module.get(require); + if (!info.compressed) { + // Force debug module loader to finish loading all modules. + for (const require of info.requires) { + goog.require(require); + + // This is a kludge to work around an issue where attempting to + // load Blockly.libraryBlocks (blocks/blocks.js) fails if the + // Blockly global variable is not defined. + // + // This is apparently because the debug module loader fails to + // load Blockly.libraryBlocks.lists (blocks/lists.js) and + // .procedures (blocks/procedures.js) first, despite they both + // being required from blocks.js, and that is apparently because + // they both depend on Blockly.Xml which the debug loader seems + // to think has not been loaded yet even though it has. + if (require === 'Blockly') { + window.Blockly = goog.module.get('Blockly'); + } + } + } + + // Create global names for named and destructured imports. + for (const varName in info.namedImports) { + const id = info.namedImports[varName]; + const value = info.compressed ? get(id) : goog.module.get(id); + if (value) { + window[varName] = value; + } + } + for (const varName in info.destructuredImports) { + const id = info.destructuredImports[varName]; + const value = info.compressed ? get(id) : goog.module.get(id)[varName]; + if (value) { + window[varName] = value; + } + } + + return; // All done. Only helper functions after this point. + + /** + * Get the object referred to by a doted-itentifier path + * (e.g. foo.bar.baz). + * @param {string} path The path referring to the object. + * @return {string|null} The object, or null if not found. + */ + function get(path) { + let obj = window; + for (const part of path.split('.')) { + obj = obj[part]; + if (!obj) return null; + } + return obj; } -} +})();