refactor(tests): Revise tests/playgrounds/prepare.js as tests/bootstrap.js to improve support for ES modules and post-bootstrap scripts (#6214)

* refactor(tests): Move and rename prepare.js, blockly.mjs

  Since prepare.js and blockly.mjs are going to be needed for running
  all tests in uncompiled mode (not just the playgrounds), move them
  tests/.  Further, rename prepare.js to bootstrap.js to better reflect
  its purpose.

* feat(tests): Introduce BLOCKLY_BOOTSTRAP_OPTIONS

  Provide a mechanism for web pages that use bootstrap.js to control
  what is loaded and how.

* fix(tests): Use the blockly repository path for all script src= URLs

  Previously the (non-advanced) playground was only correctly loadging
  on localhost because you can put an arbitrary number of "../"s in front
  of a relative URL and it just takes you to the root directory.

* fix(tests): Don't use template literals in bootstrap.js

  This is necessary (but not necessarily sufficient) to be able to
  load the file in IE 11.

* fix(tests): Throw error if attempting to bootstrap in node.js

* feat(tests): Make bootstrap.js more configurable.

* Terminology change: use "compressed" and "uncompressed" to describe
  what Closure Compiler calls "compiled" and "uncompiled", to reduce
  confusion with the compilation that will be done by tsc.

* Get the list of modules to bootstrap (in compressed mode), or
  scripts to load (in compressed mode) from BLOCKLY_BOOTSTRAP_OPTIONS,
  to allow calling scripts to to specify exactly what to load.

* feat(tests): Use a proper quote function

  We need to generate string literals.  Best to use a quote function
  instead of concatenating on quote marks withou escaping.  Copy a
  well-tested one from Code City.

* feat(tests): Support an additionalScripts option

  This is a list of scripts to load (in order) once the required modules
  have been bootstrapped.

  We do this using goog.addDependency to make the first script depend
  on the required modules, then each subsequent script depend on the
  previous one, and then finally goog.bootstrapping the last such script.

* refactor(tests): Remove special handling of msg/messages.js

* refactor(tests): Use additionalScripts for all script loading

  Use additionalScripts option for all script loading in
  playground.html and advanced_playground.html.

* refactor(tests): Use bootstrap instead of uncompressed in Mocha tests

  Use tests/bootstrap.js instead of blockly_uncompressed.js to load
  blockly in uncompressed mode in the Mocha tests.

  This entails adding a new item, despFiles, to BLOCKLY_BOOTSTRAP_OPTIONS,
  to allow tests/deps.mocha.js to be loaded at the appropriate point.

  Mention of blockly_uncompressed.js is removed from
  tests/mocah/.mocharc.js; it's not clear to me what effect the "file:"
  directive in this file might have previously had and I was not able to
  find documentation for it on mochajs.org, but in any case removing it
  appears to have had no ill effect.

* refactor(tests): Use bootstrap instead of uncompressed in generator tests

  This entails adding an additional check in bootstrap so as to load
  uncompressed when loading from a file: URL, since these are not
  localhost URLs - though in fact the generator tests run equally well
  in compressed mode, albeit against (for now) the previously-check-in
  build products rather than the live code.

* refactor(test): Use bootstrap.js in multi_playground.html

  This removes the last use of load_all.js, so remove it.

* chore(tests): Delete blockly_uncompressed.js

  Its function has now been entirely subsumed by tests/bootstrap.js,
  so remove it and update any remaining mentions of it.

  Also fix formatting and positions of some comments in playground.html.

* fix(tests): Rewrite bootstrap sequencing code

  An earlier commit modified the generated <script> to use
  goog.addDependency to trick the debug module loader into loading
  .additionalScripts (via goog.bootstrap), but it turns out there is
  a small problem: scripts like msg/messages.js have undeclared
  dependencies on the Blockly module, and without a call to
  goog.require('Blockly') in them they can end up being run before
  the Blockly module is fully loaded.

  (This problem only occurs when there are ES Modules, rather than
  merely goog.modules, in the mix.)

  Fix this by adding a script, bootstrap_helper.js, to be loaded
  options.requires and any options.additionalScripts that makes an
  explicit call to goog.require for each of option.requires.

  Also refactor the code so that instead of generating a loop which
  calls goog.addDependency, we generate the addDependency calls
  directly.  This makes debugging a bit easer as we can use the browser's
  dev tools to inspect the generated calls in the DOM tree.

* fix(tests): Prevent spurious transpilation warnings

  For some reason when the debug module loader encounters ES modules
  it starts to complain about being unable to transpile some ES202x
  features in other (non-ESM) modules, even though it doesn't normally
  try to transpile those.

  Since uncompressed-mode testing is almost exclusively on modern
  browsers we don't care about transpiling these features, so suppress
  the warnings instead.

* refactor(tests): Rename blockly.mjs to bootstrap_done.mjs; simplify

  Since blockly.mjs is no longer returning just the exports object
  from core/blockly.js (see PR #5995), it might be better named after
  its actual purpose: to wait for bootstrapping to be done.

  Remove all the code that was used to pass the blockly.js exports
  object along from the bootstrap callback to the blockly.mjs export,
  since there's no reason to go to a lot of trouble to set a local
  variable named Blockly to the same value as a global variable named
  Blockly.

  (Something like this may be needed again in future, but certainly in
  a different form.)

* chore(tests): Use freshly-build files in compressed mode.

  Use the freshly-built build/*_compresssed.js files when bootstrapping
  in compressed mode, rather than using the checked-in files in the
  repository root.

  This helps ensure that compressed and uncompressed mode will be
  testing (as closely as possible) the same code.

* chore(tests): Rename BlocklyLoader to blocklyLoader; record compressed

  - Rename the BlocklyLoader global to blocklyLoader (since it is not
    a class constructor).
  - Create it regardless of whether we are bootstrapping in
    uncompressed or loading compressed via <script> tags.
  - Record which we are doing as .compressed, and use this property
    to choose playground background colour.

* chore(tests): Resolve comments for PR #6214

  Mostly documentation changes, but notably renaming blocklyLoader to
  bootstrapInfo.

* Revert "chore(tests): Use freshly-build files in compressed mode."

  This reverts commit de8d356838.
This commit is contained in:
Christopher Allen
2022-06-15 19:35:01 +01:00
committed by GitHub
parent 16b5ccd2ea
commit 46df7d132d
14 changed files with 455 additions and 374 deletions

View File

@@ -1,58 +0,0 @@
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Bootstrap code to load Blockly in uncompiled mode.
*/
'use strict';
/* eslint-disable no-var */
/**
* Blockly uncompiled-mode startup code. If running in a browser
* loads closure/goog/base.js and build/deps.js, then (in any case)
* requires Blockly.requires.
*/
(function(globalThis) {
/* eslint-disable-next-line no-undef */
var IS_NODE_JS = !!(typeof module !== 'undefined' && module.exports);
if (IS_NODE_JS) {
// Load Blockly.
goog.require('Blockly.requires');
/* eslint-disable no-undef */
module.exports = Blockly;
} else {
var BLOCKLY_DIR = '';
// Find name of current directory.
var scripts = document.getElementsByTagName('script');
var re = /(.+)[\\/]blockly_(?:.*)uncompressed\.js$/;
for (var script, i = 0; (script = scripts[i]); i++) {
var match = re.exec(script.src);
if (match) {
BLOCKLY_DIR = match[1];
break;
}
}
if (!BLOCKLY_DIR) {
alert('Could not detect Blockly\'s directory name.');
}
// Disable loading of closure/goog/deps.js (which doesn't exist).
globalThis.CLOSURE_NO_DEPS = true;
// Load Closure Library base.js (the only part of the libary we use,
// mainly for goog.require / goog.provide / goog.module).
document.write(
'<script src="' + BLOCKLY_DIR + '/closure/goog/base.js"></script>');
// Load dependency graph info from build/deps.js. To update
// deps.js, run `npm run build:deps`.
document.write(
'<script src="' + BLOCKLY_DIR + '/build/deps.js"></script>');
// Load the rest of Blockly.
document.write('<script>goog.require(\'Blockly\');</script>');
}
/* eslint-disable-next-line no-invalid-this */
})(this);

View File

@@ -262,7 +262,7 @@ function buildJavaScript(done) {
/**
* This task updates DEPS_FILE (deps.js), used by
* blockly_uncompressed.js when loading Blockly in uncompiled mode.
* bootstrap.js when loading Blockly in uncompiled mode.
*
* Also updates TEST_DEPS_FILE (deps.mocha.js), used by the mocha test
* suite.
@@ -673,7 +673,6 @@ function buildAdvancedCompilationTest() {
* php_compressed.js
* lua_compressed.js
* dart_compressed.js
* blockly_uncompressed.js
* msg/json/*.js
* test/deps*.js
*/

View File

@@ -19,17 +19,16 @@ var path = require('path');
// - tests/scripts/compile_typings.sh
// - tests/scripts/check_metadata.sh
// - tests/scripts/update_metadata.sh
// - blockly_uncompressed.js (for location of deps.js)
// - tests/playgrounds/prepare.js (for location of deps.js)
// - tests/bootstrap.js (for location of deps.js)
// - tests/mocha/index.html (for location of deps.mocha.js)
// Directory to write compiled output to.
exports.BUILD_DIR = 'build';
// Dependencies file (for blockly_uncompressed.js):
// Dependencies file (used by bootstrap.js in uncompiled mode):
exports.DEPS_FILE = path.join(exports.BUILD_DIR, 'deps.js');
// Dependencies file (for blockly_uncompressed.js):
// Mocha test dependencies file (used by tests/mocha/index.html):
exports.TEST_DEPS_FILE = path.join(exports.BUILD_DIR, 'deps.mocha.js');
// Directory to write typings output to.

246
tests/bootstrap.js vendored Normal file
View File

@@ -0,0 +1,246 @@
/**
* @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 <script> tag in a web page to use the
* Closure Library debug module loader to load Blockly in
* uncompressed mode.
*
* You must use a <script src=> tag to load this script first
* (after setting BLOCKLY_BOOTSTRAP_OPTIONS in a preceeding
* <script> tag, if desired - see below), then import
* bootstrap_done.mjs in a <script type=module> to wait for
* bootstrapping to finish.
*
* See tests/playground.html for example usage.
*
* Exception: for speed and compatibility reasons, if this is
* script is loaded in Internet Explorer or from a host other than
* localhost, blockly_compressed.js (et al.) will be loaded
* instead. IE 11 doesn't understand modern JavaScript, and
* because of the sequential, non-parallel module loading carried
* out by thedeubg module loader can be painfully tedious over a
* slow network connection. (This can be overridden by the page
* if desired.)
*
* (Note also that this file eschews certain modern JS constructs,
* like template literals, for compatibility with IE 11.)
*
* The bootstrap code will consult a BLOCKLY_BOOTSTRAP_OPTIONS
* global variable to determine what to load. This must be set
* before loading this script. See documentation inline below.
*
*/
'use strict';
(function() {
// Values used to compute default bootstrap options.
const isIe = navigator.userAgent.indexOf('MSIE') !== -1 ||
navigator.appVersion.indexOf('Trident/') > -1;
const localhosts = ['localhost', '127.0.0.1', '[::1]'];
const isLocalhost = localhosts.includes(location.hostname);
const isFileUrl = location.origin === 'file://';
// Default bootstrap options. These can be overridden by setting
// the same property on BLOCKLY_BOOTSTRAP_OPTIONS.
const options = {
// Decide whether to use compressed mode or not. Please see issue
// #5557 for more information.
loadCompressed: isIe || !(isLocalhost || isFileUrl),
// URL of the blockly repository. This is needed for a few reasons:
//
// - We need an absolute path instead of relative path because the
// advanced_playground and the regular playground are in
// different folders.
// - We need to get the root directory for blockly because it is
// different for github.io, appspot and local.
//
// Default value will work so long as top-level page is loaded
// from somewhere in tests/.
root: window.location.href.replace(/\/tests\/.*$/, '/'),
// 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 scripts to load in compressed mode, instead of
// requires. Paths relative to root.
compressedScripts: [
'blockly_compressed.js',
'blocks_compressed.js',
'dart_compressed.js',
'javascript_compressed.js',
'lua_compressed.js',
'php_compressed.js',
'python_compressed.js',
],
// Additional scripts to be loaded after Blockly is loaded,
// whether Blockly is loaded from compressed or uncompressed.
// Paths relative to root.
additionalScripts: [
'msg/messages.js',
],
};
if (typeof window.BLOCKLY_BOOTSTRAP_OPTIONS === 'object') {
Object.assign(options, window.BLOCKLY_BOOTSTRAP_OPTIONS);
}
/* eslint-disable-next-line no-undef */
if (typeof module === 'object' && typeof module.exports === 'object') {
// Running in node.js. Maybe we wish to support this.
// blockly_uncompressed formerly did, though it appears that the
// code had not been working for some time (at least since PR
// #5718 back in December 2021. For now just throw an error.
throw new Error('Bootstrapping in node.js not implemented.');
}
// Create a global variable to remember some state that will be
// needed by later scripts.
window.bootstrapInfo = {
/** boolean */ compressed: options.loadCompressed,
/** ?Array<string> */ requires: null,
/** ?Promise */ done: null,
};
if (!options.loadCompressed) {
// We can load Blockly in uncompressed mode. Note that this section
// needs to parse in IE11 (mostly ES5.1, but allowing e.g. const),
// but it does not need to be _executable_ in IE11 - it is safe to
// use ES6 builtins.
// Disable loading of closure/goog/deps.js (which doesn't exist).
window.CLOSURE_NO_DEPS = true;
// Load the Closure Library's base.js (the only part of the
// libary we use, mainly for goog.require / goog.provide /
// goog.module).
document.write(
'<script src="' + options.root + '/closure/goog/base.js"></script>');
// Prevent spurious transpilation warnings.
document.write('<script>goog.TRANSPILE = "never";</script>');
// 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(
'<script src="' + options.root + options.depsFiles[i] + '">' +
'</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 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(
'<script>\n' + scriptDeps.join('') +
' window.bootstrapInfo.done = new Promise((resolve, reject) => {\n' +
' goog.bootstrap([' + requires.map(quote).join() + '], resolve);\n' +
' });\n' +
'</script>\n');
} else {
// We need to load Blockly in compressed mode. Load
// blockly_compressed.js et al. using <script> tags.
const scripts =
options.compressedScripts.concat(options.additionalScripts);
for (let i = 0; i < scripts.length; i++) {
document.write(
'<script src="' + options.root + scripts[i] + '"></script>');
}
}
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) + "'";
}
})();

View File

@@ -20,20 +20,20 @@
*
* See tests/playground.html for example usage.
*/
let Blockly;
if (window.BlocklyLoader) {
// Uncompiled mode. Use top-level await
// (https://v8.dev/features/top-level-await) to block loading of
// this module until goog.bootstrap()ping of Blockly is finished.
await window.BlocklyLoader;
Blockly = globalThis.Blockly;
} else if (window.Blockly) {
// Compiled mode. Retrieve the pre-installed Blockly global.
Blockly = globalThis.Blockly;
} else {
throw new Error('neither window.Blockly nor window.BlocklyLoader found');
if (!window.bootstrapInfo) {
throw new Error('window.bootstrapInfo not found. ' +
'Make sure to load bootstrap.js before importing bootstrap_done.mjs.');
}
export default Blockly;
if (window.bootstrapInfo.compressed) {
// Compiled mode. Nothing more to do.
} else {
// Uncompressed mode. Use top-level await
// (https://v8.dev/features/top-level-await) to block loading of
// this module until goog.bootstrap()ping of Blockly is finished.
await window.bootstrapInfo.done;
// Note that this module previously did an export default of the
// value returned by the bootstrapInfo.done promise. This was
// changed in PR #5995 because library blocks and generators cannot
// be accessed via that the core/blockly.js exports object.
}

20
tests/bootstrap_helper.js vendored Normal file
View File

@@ -0,0 +1,20 @@
/**
* @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.
*/
/* eslint-disable-next-line no-undef */
for (const require of window.bootstrapInfo.requires) {
goog.require(require);
}

View File

@@ -3,41 +3,36 @@
<head>
<meta charset="utf-8">
<title>Blockly Generator Tests</title>
<script src="../../blockly_uncompressed.js"></script>
<script src="../../msg/messages.js"></script>
<script>
// Require optional modules needed by tests.
goog.require('Blockly.Dart.all');
goog.require('Blockly.JavaScript.all');
goog.require('Blockly.Lua.all');
goog.require('Blockly.PHP.all');
goog.require('Blockly.Python.all');
goog.require('Blockly.libraryBlocks');
var BLOCKLY_BOOTSTRAP_OPTIONS = {
additionalScripts: [
'msg/messages.js',
'tests/generators/unittest_javascript.js',
'tests/generators/unittest_python.js',
'tests/generators/unittest_php.js',
'tests/generators/unittest_lua.js',
'tests/generators/unittest_dart.js',
'tests/generators/unittest.js',
],
}
</script>
<script src="unittest_javascript.js"></script>
<script src="unittest_python.js"></script>
<script src="unittest_php.js"></script>
<script src="unittest_lua.js"></script>
<script src="unittest_dart.js"></script>
<script src="unittest.js"></script>
<script src="../bootstrap.js"></script>
<script>
'use strict';
var demoWorkspace = null;
var outputCode = '';
function start() {
demoWorkspace = Blockly.inject('blocklyDiv',
{grid:
{spacing: 25,
length: 3,
colour: '#ccc',
snap: true},
media: '../../media/',
toolbox: document.getElementById('toolbox'),
zoom: {controls: true, wheel: true}
});
demoWorkspace = Blockly.inject('blocklyDiv', {
grid: {
spacing: 25,
length: 3,
colour: '#ccc',
snap: true
},
media: '../../media/',
toolbox: document.getElementById('toolbox'),
zoom: {controls: true, wheel: true}
});
changeIndex();
}
@@ -210,6 +205,12 @@ function changeIndex() {
demoWorkspace.getToolbox().flyout_.workspace_.options.oneBasedIndex = oneBasedIndex;
}
</script>
<script type=module>
// Wait for Blockly to finish loading before running tests.
import '../bootstrap_done.mjs';
start();
</script>
<style>
html, body {
@@ -240,7 +241,7 @@ h1 {
}
</style>
</head>
<body onload="start()">
<body>
<div id="blocklyDiv"></div>

View File

@@ -2,6 +2,5 @@
module.exports = {
ui: 'tdd',
file: '../blockly_uncompressed.js',
reporter: 'landing'
};

View File

@@ -5,9 +5,6 @@
<title>Mocha Tests for Blockly</title>
<link href="../../node_modules/mocha/mocha.css" rel="stylesheet" />
<script src="../../blockly_uncompressed.js"></script>
<script src="../../build/deps.mocha.js"></script>
<script src="../../msg/messages.js"></script>
</head>
<style>
#blocklyDiv {
@@ -21,6 +18,9 @@
<div id="mocha"></div>
<div id="failureCount" style="display:none" tests_failed="unset"></div>
<div id="failureMessages" style="display:none"></div>
<!-- Load mocha et al. before bootstrapping so that we can safely
goog.require() the test modules that make calls to (e.g.)
suite() at the top level. -->
<script src="../../node_modules/chai/chai.js"></script>
<script src="../../node_modules/mocha/mocha.js"></script>
<script src="../../node_modules/sinon/pkg/sinon.js"></script>
@@ -29,92 +29,95 @@
ui: 'tdd',
failZero: true,
});
</script>
<script>
// Require optional modules needed by tests.
goog.require('Blockly.Dart');
goog.require('Blockly.Dart.texts');
goog.require('Blockly.JavaScript');
goog.require('Blockly.JavaScript.texts');
goog.require('Blockly.Lua');
goog.require('Blockly.Lua.texts');
goog.require('Blockly.PHP');
goog.require('Blockly.PHP.texts');
goog.require('Blockly.Python');
goog.require('Blockly.Python.texts');
goog.require('Blockly.libraryBlocks.colour');
goog.require('Blockly.libraryBlocks.logic');
goog.require('Blockly.libraryBlocks.lists');
goog.require('Blockly.libraryBlocks.loops');
goog.require('Blockly.libraryBlocks.math');
goog.require('Blockly.libraryBlocks.procedures');
goog.require('Blockly.libraryBlocks.texts');
goog.require('Blockly.libraryBlocks.variables');
goog.require('Blockly.libraryBlocks.variablesDynamic');
var BLOCKLY_BOOTSTRAP_OPTIONS = {
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',
// Run tests.
goog.require('Blockly.test.astNode');
goog.require('Blockly.test.blockChangeEvent');
goog.require('Blockly.test.blockCreateEvent');
goog.require('Blockly.test.blockJson');
goog.require('Blockly.test.blocks');
goog.require('Blockly.test.comments');
goog.require('Blockly.test.commentDeserialization');
goog.require('Blockly.test.connectionChecker');
goog.require('Blockly.test.connectionDb');
goog.require('Blockly.test.connection');
goog.require('Blockly.test.contextMenuItem');
goog.require('Blockly.test.cursor');
goog.require('Blockly.test.dropdown');
goog.require('Blockly.test.event');
goog.require('Blockly.test.extensions');
goog.require('Blockly.test.fieldAngle');
goog.require('Blockly.test.fieldCheckbox');
goog.require('Blockly.test.fieldColour');
goog.require('Blockly.test.fieldDropdown');
goog.require('Blockly.test.fieldImage');
goog.require('Blockly.test.fieldLabelSerialization');
goog.require('Blockly.test.fieldLabel');
goog.require('Blockly.test.fieldMultiline');
goog.require('Blockly.test.fieldNumber');
goog.require('Blockly.test.fieldRegistry');
goog.require('Blockly.test.fieldTest');
goog.require('Blockly.test.fieldTextInput');
goog.require('Blockly.test.fieldVariable');
goog.require('Blockly.test.flyout');
goog.require('Blockly.test.generator');
goog.require('Blockly.test.gesture');
goog.require('Blockly.test.input');
goog.require('Blockly.test.insertionMarker');
goog.require('Blockly.test.jsoDeserialization');
goog.require('Blockly.test.jsoSerialization');
goog.require('Blockly.test.json');
goog.require('Blockly.test.keydown');
goog.require('Blockly.test.lists');
goog.require('Blockly.test.logicTernary');
goog.require('Blockly.test.metrics');
goog.require('Blockly.test.mutator');
goog.require('Blockly.test.names');
goog.require('Blockly.test.procedures');
goog.require('Blockly.test.registry');
goog.require('Blockly.test.serialization');
goog.require('Blockly.test.shortcutRegistry');
goog.require('Blockly.test.theme');
goog.require('Blockly.test.toolbox');
goog.require('Blockly.test.tooltip');
goog.require('Blockly.test.trashcan');
goog.require('Blockly.test.utils');
goog.require('Blockly.test.variableMap');
goog.require('Blockly.test.variableModel');
goog.require('Blockly.test.variables');
goog.require('Blockly.test.widgetDiv');
goog.require('Blockly.test.workspaceComment');
goog.require('Blockly.test.workspaceSvg');
goog.require('Blockly.test.workspace');
goog.require('Blockly.test.xml');
goog.require('Blockly.test.zoomControls');
// Test modules.
'Blockly.test.astNode',
'Blockly.test.blockChangeEvent',
'Blockly.test.blockCreateEvent',
'Blockly.test.blockJson',
'Blockly.test.blocks',
'Blockly.test.comments',
'Blockly.test.commentDeserialization',
'Blockly.test.connectionChecker',
'Blockly.test.connectionDb',
'Blockly.test.connection',
'Blockly.test.contextMenuItem',
'Blockly.test.cursor',
'Blockly.test.dropdown',
'Blockly.test.event',
'Blockly.test.extensions',
'Blockly.test.fieldAngle',
'Blockly.test.fieldCheckbox',
'Blockly.test.fieldColour',
'Blockly.test.fieldDropdown',
'Blockly.test.fieldImage',
'Blockly.test.fieldLabelSerialization',
'Blockly.test.fieldLabel',
'Blockly.test.fieldMultiline',
'Blockly.test.fieldNumber',
'Blockly.test.fieldRegistry',
'Blockly.test.fieldTest',
'Blockly.test.fieldTextInput',
'Blockly.test.fieldVariable',
'Blockly.test.flyout',
'Blockly.test.generator',
'Blockly.test.gesture',
'Blockly.test.input',
'Blockly.test.insertionMarker',
'Blockly.test.jsoDeserialization',
'Blockly.test.jsoSerialization',
'Blockly.test.json',
'Blockly.test.keydown',
'Blockly.test.lists',
'Blockly.test.logicTernary',
'Blockly.test.metrics',
'Blockly.test.mutator',
'Blockly.test.names',
'Blockly.test.procedures',
'Blockly.test.registry',
'Blockly.test.serialization',
'Blockly.test.shortcutRegistry',
'Blockly.test.theme',
'Blockly.test.toolbox',
'Blockly.test.tooltip',
'Blockly.test.trashcan',
'Blockly.test.utils',
'Blockly.test.variableMap',
'Blockly.test.variableModel',
'Blockly.test.variables',
'Blockly.test.widgetDiv',
'Blockly.test.workspaceComment',
'Blockly.test.workspaceSvg',
'Blockly.test.workspace',
'Blockly.test.xml',
'Blockly.test.zoomControls',
],
additionalScripts: [
'msg/messages.js',
'tests/playgrounds/screenshot.js',
'node_modules/@blockly/dev-tools/dist/index.js',
],
}
</script>
<script src="../bootstrap.js"></script>
<div id="blocklyDiv"></div>
@@ -175,7 +178,10 @@
<block type="test_field_block"></block>
</xml>
<script>
<script type=module>
// Wait for Blockly to finish loading before running tests.
import '../bootstrap_done.mjs';
let runner = mocha.run(function(failures) {
var failureDiv = document.getElementById('failureCount');
failureDiv.setAttribute('tests_failed', failures);

View File

@@ -4,14 +4,14 @@
<meta charset="utf-8">
<title>Multi-toolbox Playground</title>
<!-- This script loads uncompressed when on localhost and loads compressed when it is being hosted.
Please add any other dependencies to playgrounds/load_all.js.-->
<script src="playgrounds/load_all.js"></script>
<!-- This script loads uncompressed when on localhost and loads
compressed when it is being hosted or on Internet Explorer. -->
<script src="bootstrap.js"></script>
<script type=module>
// Wait for Blockly to finish loading.
import './bootstrap_done.mjs';
<script>
'use strict';
const IS_UNCOMPRESSED = !!document.getElementById('blockly-uncompressed-script');
const IS_UNCOMPRESSED = !window.bootstrapInfo.compressed; // See bootstrap.js
var options = {
comments: true,
@@ -74,6 +74,12 @@ function setBackgroundColour() {
document.body.style.backgroundColor = '#60fcfc'; // Unfamiliar blue.
}
}
// Call start(). Because this <script> has type=module, it is
// automatically deferred, so it will not be run until after the
// document has been parsed, but before firing DOMContentLoaded.
start();
</script>
<style>
@@ -105,7 +111,7 @@ h1 {
}
</style>
</head>
<body onload="start()">
<body>
<h1>Blockly Multi Playground</h1>
<form id="options">

View File

@@ -4,15 +4,23 @@
<meta charset="utf-8">
<title>Blockly Playground</title>
<!-- This script loads uncompressed when on localhost and loads compressed when it is being hosted.
Please add any other dependencies to playgrounds/prepare.js.-->
<script src="playgrounds/prepare.js"></script>
<script src="playgrounds/screenshot.js"></script>
<script src="../node_modules/@blockly/dev-tools/dist/index.js"></script>
<!-- This script loads uncompressed when on localhost and loads
compressed when it is being hosted or on Internet Explorer. -->
<script>
var BLOCKLY_BOOTSTRAP_OPTIONS = {
additionalScripts: [
'msg/messages.js',
'tests/playgrounds/screenshot.js',
'node_modules/@blockly/dev-tools/dist/index.js',
],
}
</script>
<script src="bootstrap.js"></script>
<script type=module>
import Blockly from './playgrounds/blockly.mjs';
// Wait for Blockly to finish loading.
import './bootstrap_done.mjs';
const IS_UNCOMPRESSED = Boolean(window.BlocklyLoader); // See prepare.js
const IS_UNCOMPRESSED = !window.bootstrapInfo.compressed; // See bootstrap.js
var workspace = null;
function start() {
@@ -385,10 +393,6 @@ var spaghettiXml = [
'next': { }
});
// Call start(). Because this <script> has type=module, it is
// automatically deferred, so it will not be run until after the
// document has been parsed, but before firing DOMContentLoaded.
function addEventHandlers() {
document.getElementById('save-json').addEventListener('click', saveJson);
document.getElementById('save-xml').addEventListener('click', saveXml);
@@ -426,6 +430,9 @@ function addEventHandlers() {
.addEventListener('click', function() { workspace.setVisible(false); });
}
// Call start(). Because this <script> has type=module, it is
// automatically deferred, so it will not be run until after the
// document has been parsed, but before firing DOMContentLoaded.
start();
</script>

View File

@@ -3,15 +3,23 @@
<head>
<meta charset="utf-8">
<title>Advanced Blockly Playground</title>
<script src="prepare.js"></script>
<script src="./screenshot.js"></script>
<script src="../themes/test_themes.js"></script>
<script src="../../node_modules/@blockly/dev-tools/dist/index.js"></script>
<script src="../../node_modules/@blockly/theme-modern/dist/index.js"></script>
<script>
var BLOCKLY_BOOTSTRAP_OPTIONS = {
additionalScripts: [
'msg/messages.js',
'tests/playgrounds/screenshot.js',
'tests/themes/test_themes.js',
'node_modules/@blockly/dev-tools/dist/index.js',
'node_modules/@blockly/theme-modern/dist/index.js',
],
}
</script>
<script src="../bootstrap.js"></script>
<script type="module">
import Blockly from './blockly.mjs';
'use strict';
// Wait for Blockly to finish loading.
import './bootstrap_done.mjs';
function start() {
setBackgroundColour();

View File

@@ -1,53 +0,0 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Loads uncompressed Blockly when running locally. Loads
* compressed otherwise.
*/
'use strict';
/**
* Loads all the compressed or uncompressed dependencies necessary to run the
* playground. This is necessary since the goog.module conversion. Please see
* issue #5557 for more information.
*/
(function() {
const isIe = navigator.userAgent.indexOf('MSIE') !== -1 ||
navigator.appVersion.indexOf('Trident/') > -1;
if ((location.hostname === 'localhost' || location.hostname === '127.0.0.1' ||
location.hostname === '[::1]') &&
!isIe) {
document.write(
`<script src="../blockly_uncompressed.js" id="blockly-uncompressed-script"></script>`);
document.write(`<script src="../msg/messages.js"></script>`);
document.write(`<script src="../tests/themes/test_themes.js"></script>`);
document.write(
`<script src="../node_modules/@blockly/block-test/dist/index.js"></script>`);
document.write(`<script>
// Custom requires for the playground.
goog.require('Blockly.libraryBlocks');
goog.require('Blockly.Dart.all');
goog.require('Blockly.JavaScript.all');
goog.require('Blockly.Lua.all');
goog.require('Blockly.PHP.all');
goog.require('Blockly.Python.all');
goog.require('Blockly.WorkspaceCommentSvg');
</script>`);
} else {
document.write(
`<script src="../blockly_compressed.js" id="blockly-compressed-script"></script>`);
document.write(`<script src="../blocks_compressed.js"></script>`);
document.write(`<script src="../dart_compressed.js"></script>`);
document.write(`<script src="../javascript_compressed.js"></script>`);
document.write(`<script src="../lua_compressed.js"></script>`);
document.write(`<script src="../php_compressed.js"></script>`);
document.write(`<script src="../python_compressed.js"></script>`);
document.write(`<script src="../msg/messages.js"></script>`);
}
})();

View File

@@ -1,99 +0,0 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview Load this file in a <script> tag to prepare for
* importing Blockly into a web page.
*
* You must use a <script> tag to load this script first, then
* import blockly.mjs in a <script type=module> to obtain the
* loaded value.
*
* See tests/playground.html for example usage.
*/
'use strict';
(function() {
// Decide whether we can load Blockly uncompiled, or must load the
// compiled version. Please see issue #5557 for more information.
const isIe = navigator.userAgent.indexOf('MSIE') !== -1 ||
navigator.appVersion.indexOf('Trident/') > -1;
const localhosts = ['localhost', '127.0.0.1', '[::1]'];
if (localhosts.includes(location.hostname) && !isIe) {
// We can load Blockly in uncompiled mode.
// Disable loading of closure/goog/deps.js (which doesn't exist).
window.CLOSURE_NO_DEPS = true;
// Load the Closure Library's base.js (the only part of the
// libary we use, mainly for goog.require / goog.provide /
// goog.module).
document.write('<script src="../../closure/goog/base.js"></script>');
// Load dependency graph info from build/deps.js. To update
// deps.js, run `npm run build:deps`.
document.write('<script src="../../build/deps.js"></script>');
// Msg loading kludge. This should go away once #5409 and/or
// #1895 are fixed.
// Load messages into a temporary Blockly.Msg object, deleting it
// afterwards (after saving the messages!)
window.Blockly = {Msg: Object.create(null)};
document.write('<script src="../../msg/messages.js"></script>');
document.write(`
<script>
window.BlocklyMsg = window.Blockly.Msg;
delete window.Blockly;
</script>`);
document.write(`
<script>
window.BlocklyLoader = new Promise((resolve, reject) => {
goog.bootstrap(
[
'Blockly',
'Blockly.libraryBlocks',
'Blockly.Dart.all',
'Blockly.JavaScript.all',
'Blockly.Lua.all',
'Blockly.PHP.all',
'Blockly.Python.all',
], resolve);
}).then(() => {
// Copy Messages from temporary Blockly.Msg object to the real one:
Object.assign(goog.module.get('Blockly').Msg, window.BlocklyMsg);
}).then(() => {
return goog.module.get('Blockly');
});
</script>`);
} else {
// The below code is necessary for a few reasons:
// - We need an absolute path instead of relative path because the
// advanced_playground the and regular playground are in different folders.
// - We need to get the root directory for blockly because it is
// different for github.io, appspot and local.
const files = [
'blockly_compressed.js',
'msg/messages.js',
'blocks_compressed.js',
'dart_compressed.js',
'javascript_compressed.js',
'lua_compressed.js',
'php_compressed.js',
'python_compressed.js',
];
// We need to load Blockly in compiled mode.
const hostName = window.location.host.replaceAll('.', '\\.');
const matches = new RegExp(hostName + '\\/(.*)tests').exec(window.location.href);
const root = matches && matches[1] ? matches[1] : '';
// Load blockly_compressed.js et al. using <script> tags.
for (let i = 0; i < files.length; i++) {
document.write('<script src="/' + root + files[i] + '"></script>');
}
}
})();