From 817ffab7548f8e0ad410103d7784d3ec9f0aecff Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Thu, 15 Jun 2023 21:03:04 +0100 Subject: [PATCH] refactor(tests): Update bootstrap.js to better support generator chunks (#7171) Refactor bootstrap.js and bootstrap_helper.js to be able to deal with generator chunks. In particular for each chunk, specify: - The goog.module ID to goog.require() in uncompressed mode. - The script filename to load in compressed mode. - Where the chunk's UMD wrapper will save the export object when loaded as a script. - What global variable the chunk's export object should be saved in (if desired). - Any individual named exports to destructure to global variables. This allows the bootstrap scripts to be slightly simpler while also being more flexible. --- scripts/gulpfiles/build_tasks.js | 4 +- tests/bootstrap.js | 137 +++++++++++---------- tests/bootstrap_helper.js | 19 ++- tests/generators/index.html | 2 +- tests/mocha/index.html | 16 +-- tests/playground.html | 2 +- tests/playgrounds/advanced_playground.html | 2 +- tests/playgrounds/shared_procedures.html | 2 +- 8 files changed, 88 insertions(+), 96 deletions(-) diff --git a/scripts/gulpfiles/build_tasks.js b/scripts/gulpfiles/build_tasks.js index 42af7cd2c..3f3e8cfd9 100644 --- a/scripts/gulpfiles/build_tasks.js +++ b/scripts/gulpfiles/build_tasks.js @@ -293,8 +293,8 @@ function buildJavaScript(done) { } /** - * This task updates DEPS_FILE (deps.js), used by - * bootstrap.js when loading Blockly in uncompiled mode. + * This task updates DEPS_FILE (deps.js), used by the debug module + * loader (via bootstrap.js) when loading Blockly in uncompiled mode. * * Also updates TEST_DEPS_FILE (deps.mocha.js), used by the mocha test * suite. diff --git a/tests/bootstrap.js b/tests/bootstrap.js index e5fcce38d..7a509e447 100644 --- a/tests/bootstrap.js +++ b/tests/bootstrap.js @@ -61,61 +61,68 @@ // List of deps files to load. Paths relative to root. depsFiles: ['build/deps.js'], - // List of goog.modules to goog.require. - requires: [ - 'Blockly', - 'Blockly.libraryBlocks', - 'Blockly.Dart.all', - 'Blockly.JavaScript.all', - 'Blockly.Lua.all', - 'Blockly.PHP.all', - 'Blockly.Python.all', + // List of modules to load. + // - id: goog.module ID to require in uncompressed mode. + // - script: path, relative to root, to .js file to load via + // `); } - // 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 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 @@ -177,12 +183,15 @@ // 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', ...options.additionalScripts]; + // 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, '-')}`; @@ -209,9 +218,9 @@ // We need to load Blockly in compressed mode. Load // blockly_compressed.js et al. using `); diff --git a/tests/bootstrap_helper.js b/tests/bootstrap_helper.js index 52f62d1cb..c4749bc87 100644 --- a/tests/bootstrap_helper.js +++ b/tests/bootstrap_helper.js @@ -25,18 +25,15 @@ } // 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 module of info.modules) { + const exports = info.compressed + ? get(module.scriptExport) + : goog.module.get(module.id); + if (module.importAt) { + window[module.importAt] = exports; } - } - 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; + for (const location in module.destructure) { + window[location] = exports[module.destructure[location]]; } } diff --git a/tests/generators/index.html b/tests/generators/index.html index fba4d370f..e35dbfac6 100644 --- a/tests/generators/index.html +++ b/tests/generators/index.html @@ -5,7 +5,7 @@ Blockly Generator Tests