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.
This commit is contained in:
Christopher Allen
2023-06-15 21:03:04 +01:00
committed by GitHub
parent 2d97e5aaf1
commit 817ffab754
8 changed files with 88 additions and 96 deletions

137
tests/bootstrap.js vendored
View File

@@ -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
// <script> in compressed mode.
// - scriptExport: path at which script will save exports object
// (see chunks in build_tasks.js); defaults to id.
// - importAt: gloal variable to set to export object.
// - destructure: map of globalVariable: exportName; globals will
// be set to the corresponding exports.
modules: [
{
id: 'Blockly',
script: 'dist/blockly_compressed.js',
scriptExport: 'Blocky',
importAt: 'Blockly',
},
{
id: 'Blockly.libraryBlocks',
script: 'dist/blocks_compressed.js',
scriptExport: 'Blockly.libraryBlocks',
importAt: 'libraryBlocks',
},
{
id: 'Blockly.Dart.all',
script: 'dist/dart_compressed.js',
scriptExport: 'dart',
destructure: {dartGenerator: 'dartGenerator'},
},
{
id: 'Blockly.JavaScript.all',
script: 'dist/javascript_compressed.js',
scriptExport: 'javascript',
destructure: {javascriptGenerator: 'javascriptGenerator'},
},
{
id: 'Blockly.Lua.all',
script: 'dist/lua_compressed.js',
scriptExport: 'lua',
destructure: {luaGenerator: 'luaGenerator'},
},
{
id: 'Blockly.PHP.all',
script: 'dist/php_compressed.js',
scriptExport: 'php',
destructure: {phpGenerator: 'phpGenerator'},
},
{
id: 'Blockly.Python.all',
script: 'dist/python_compressed.js',
scriptExport: 'python',
destructure: {pythonGenerator: 'pythonGenerator'},
},
],
// List of scripts to load in compressed mode, instead of
// requires. Paths relative to root.
compressedScripts: [
'dist/blockly_compressed.js',
'dist/blocks_compressed.js',
'dist/dart_compressed.js',
'dist/javascript_compressed.js',
'dist/lua_compressed.js',
'dist/php_compressed.js',
'dist/python_compressed.js',
],
// List of imports to give global names to. The keys are the
// global variable names, the values are goog.module IDs to name,
// or (when loading in compressed mode) the paths within the
// Blockly namespace at which the corresponding chunk is loaded by
// its UMD wrapper. Note that entries in this map are ignored if
// the corresponding goog.module has not been loaded via an entry
// in requires or compressedScripts.
namedImports: {
Blockly: 'Blockly',
libraryBlocks: 'Blockly.libraryBlocks',
},
// List of destructured imports. As for namedImports, but only
// the named export corresponding to the mentioned global variable
// will be imported.
//
// Exception: in compressed mode, the UMD wrappers generated by
// chunkWrapper() in scripts/gulpfiles/build_tasks.js already pull
// out the desired named export - so in that case the entries here
// are treated identically to those in namedImports.
destructuredImports: {
dartGenerator: 'Blockly.Dart',
javascriptGenerator: 'Blockly.JavaScript',
luaGenerator: 'Blockly.Lua',
phpGenerator: 'Blockly.PHP',
pythonGenerator: 'Blockly.Python',
},
// Additional goog.modules to goog.require (for side-effects only,
// in uncompressed mode only).
requires: [],
// Additional scripts to be loaded after Blockly is loaded,
// whether Blockly is loaded from compressed or uncompressed.
// Paths relative to root.
additionalScripts: ['build/msg/en.js'],
scripts: ['build/msg/en.js'],
};
if (typeof window.BLOCKLY_BOOTSTRAP_OPTIONS === 'object') {
Object.assign(options, window.BLOCKLY_BOOTSTRAP_OPTIONS);
@@ -134,9 +141,9 @@
// needed by later scripts.
window.bootstrapInfo = {
/** boolean */ compressed: options.loadCompressed,
/** Object<{id: string, script: string, scriptExport: string,
* destructure: Object<string>}> */ modules: options.modules,
/** ?Array<string> */ requires: null,
/** Object<string> */ namedImports: options.namedImports,
/** Object<string> */ destructuredImports: options.destructuredImports,
/** ?Promise */ done: null,
};
@@ -162,13 +169,12 @@
document.write(`<script src="${options.root + depsFile}"></script>`);
}
// 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 <script> tags.
const scripts = [
...options.compressedScripts,
...options.modules.map((module) => module.script),
'tests/bootstrap_helper.js',
...options.additionalScripts,
...options.scripts,
];
for (const script of scripts) {
document.write(`<script src="${options.root + script}"></script>`);

View File

@@ -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]];
}
}

View File

@@ -5,7 +5,7 @@
<title>Blockly Generator Tests</title>
<script>
var BLOCKLY_BOOTSTRAP_OPTIONS = {
additionalScripts: [
scripts: [
'build/msg/en.js',
'tests/generators/unittest_javascript.js',
'tests/generators/unittest_python.js',

View File

@@ -34,20 +34,6 @@
loadCompressed: false,
depsFiles: ['build/deps.js', 'build/deps.mocha.js'],
requires: [
// Blockly modules needed by tests.
'Blockly',
'Blockly.libraryBlocks',
'Blockly.Dart',
'Blockly.Dart.texts',
'Blockly.JavaScript',
'Blockly.JavaScript.texts',
'Blockly.Lua',
'Blockly.Lua.texts',
'Blockly.PHP',
'Blockly.PHP.texts',
'Blockly.Python',
'Blockly.Python.texts',
// Test modules.
'Blockly.test.astNode',
'Blockly.test.blockJson',
@@ -133,7 +119,7 @@
'Blockly.test.xml',
'Blockly.test.zoomControls',
],
additionalScripts: [
scripts: [
'build/msg/en.js',
'tests/playgrounds/screenshot.js',
'node_modules/@blockly/dev-tools/dist/index.js',

View File

@@ -8,7 +8,7 @@
compressed when it is being hosted or on Internet Explorer. -->
<script>
var BLOCKLY_BOOTSTRAP_OPTIONS = {
additionalScripts: [
scripts: [
'build/msg/en.js',
'tests/playgrounds/screenshot.js',
'node_modules/@blockly/dev-tools/dist/index.js',

View File

@@ -6,7 +6,7 @@
<script>
var BLOCKLY_BOOTSTRAP_OPTIONS = {
additionalScripts: [
scripts: [
'build/msg/en.js',
'tests/playgrounds/screenshot.js',
'tests/themes/test_themes.js',

View File

@@ -8,7 +8,7 @@
compressed when it is being hosted or on Internet Explorer. -->
<script>
var BLOCKLY_BOOTSTRAP_OPTIONS = {
additionalScripts: [
scripts: [
'build/msg/en.js',
'node_modules/@blockly/dev-tools/dist/index.js',
],