/** * @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 (const depsFile of options.depsFiles) { document.write(``); } // Assemble a list of module targets to bootstrap. // // The first group of targets are those listed in options.modules // and options.requires. These are recorded on bootstrapInfo so // so bootstrap_helper.js can goog.require() them to force loading // to complete. // // 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.scripts, // again generating calls to goog.addDependency for each one // making it dependent on the previous one. let requires = (window.bootstrapInfo.requires = [ ...options.modules.map((module) => module.id), ...options.requires, ]); const scripts = ['tests/bootstrap_helper.js', ...options.scripts]; const scriptDeps = []; for (const script of scripts) { const fakeModuleName = `script.${script.replace(/[./]/g, '-')}`; scriptDeps.push( `goog.addDependency(${quote('../../../../' + script)}, ` + `[${quote(fakeModuleName)}], [${requires.map(quote).join()}], ` + `{'lang': 'es6'});`, ); 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(``); } 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 */ return "'" + str.replace(singleRE, (c) => replacements[c]) + "'"; } })();