Files
blockly/tests/bootstrap_helper.js
Christopher Allen f528eccba7 fix(tests): Fix bootstrapping of generators in compressed mode (#6703)
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 <script> tag.

This commit introduces two new configuration options to bootstrap.js
(preconfigured with defaults that should work in most cases) mapping
global variable names to their corresponding module identifiers, and
modifies the code in bootstrap_helper.js to set global variables
appropriately in both uncompressed and compressed modes.
2022-12-14 19:47:27 +00:00

74 lines
2.3 KiB
JavaScript

/**
* @license
* Copyright 2022 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Helper script for bootstrap.js
*
* This is loaded, via goog.bootstrap(), after the other top-level
* Blockly modules. It simply calls goog.require() for each of them,
* to force the debug module loader to finish loading them before any
* non-module scripts (like msg/messages.js) that might have
* undeclared dependencies on them.
*/
(function() {
const info = window.bootstrapInfo;
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;
}
})();