/** * @license * Copyright 2021 Google LLC * SPDX-License-Identifier: Apache-2.0 */ /** * @fileoverview Bootstrap code to load Blockly, typically in * uncompressed mode. * * Load this file in a '); // Prevent spurious transpilation warnings. document.write(''); // Load dependency graph info from the specified deps files - // typically just build/deps.js. To update deps after changing // any module's goog.requires / imports, run `npm run build:deps`. for (let i = 0; i < options.depsFiles.length; i++) { document.write( ''); } // Record require targets for bootstrap_helper.js. window.bootstrapInfo.requires = options.requires; // Assemble a list of module targets to bootstrap. // // The first group of targets are those listed in // options.requires. // // The next target is a fake one that will load // bootstrap_helper.js. We generate a call to goog.addDependency // to tell the debug module loader that it can be loaded via a // fake module name, and that it depends on all the targets in the // first group (and indeed bootstrap_helper.js will make a call to // goog.require for each one). // // We then create another target for each of // options.additionalScripts, again generating calls to // goog.addDependency for each one making it dependent on the // previous one. let requires = options.requires.slice(); const scripts = ['tests/bootstrap_helper.js'].concat(options.additionalScripts); const scriptDeps = []; for (let script, i = 0; script = scripts[i]; i++) { const fakeModuleName = 'script.' + script.replace(/[./]/g, "-"); scriptDeps.push(' goog.addDependency(' + quote('../../' + script) + ', [' + quote(fakeModuleName) + '], [' + requires.map(quote).join() + "], {'lang': 'es6'});\n"); requires = [fakeModuleName]; } // Finally, write out a script containing the generated // goog.addDependency calls and a call to goog.bootstrap // requesting the loading of the final target, which will cause // all the previous ones to be loaded recursively. Wrap this in a // promise and save it so it can be awaited in bootstrap_done.mjs. document.write( '\n'); } else { // We need to load Blockly in compressed mode. Load // blockly_compressed.js et al. using '); } } return; // All done. Only helper functions after this point. /** * Convert a string into a string literal. Strictly speaking we * only need to escape backslash, \r, \n, \u2028 (line separator), * \u2029 (paragraph separator) and whichever quote character we're * using, but for simplicity we escape all the control characters. * * Based on https://github.com/google/CodeCity/blob/master/server/code.js * * @param {string} str The string to convert. * @return {string} The value s as a eval-able string literal. */ function quote(str) { /* eslint-disable no-control-regex, no-multi-spaces */ /** Regexp for characters to be escaped in a single-quoted string. */ const singleRE = /[\x00-\x1f\\\u2028\u2029']/g; /** Map of control character replacements. */ const replacements = { '\x00': '\\0', '\x01': '\\x01', '\x02': '\\x02', '\x03': '\\x03', '\x04': '\\x04', '\x05': '\\x05', '\x06': '\\x06', '\x07': '\\x07', '\x08': '\\b', '\x09': '\\t', '\x0a': '\\n', '\x0b': '\\v', '\x0c': '\\f', '\x0d': '\\r', '\x0e': '\\x0e', '\x0f': '\\x0f', '"': '\\"', "'": "\\'", '\\': '\\\\', '\u2028': '\\u2028', '\u2029': '\\u2029', }; /* eslint-enable no-control-regex, no-multi-spaces */ /** * Replacer function. * @param {string} c Single UTF-16 code unit ("character") string to * be replaced. * @return {string} Multi-character string containing escaped * representation of c. */ function replace(c) { return replacements[c]; } return "'" + str.replace(singleRE, replace) + "'"; } })();