Files
blockly/tests/playgrounds/prepare.js
Christopher Allen b1f0a6a867 refactor(tests): Use goog.bootstrap to allow loading ES modules in playground (#5931)
* chore(deps): Update closure/goog/base.js, add goog.js

  * Update base.js from the latest version (20220104.0.0).
  * Also copy over goog.js, which provides access to asuitable subset
    of goog.* via an importable module).

* refactor(tests): Have playground.html load Blockly as a module

  N.B.:

  * We still need a preparation step, in order to load base.js and
    deps.js via <script> tags in uncompiled mode; in compiled mode
    it will instead load all the *_compressed.js files via <script>
    tags.

    Acess to the Blockly object is via:

        import Blockly from './playgrounds/blockly.mjs';

    (N.B: no "* as", since blockly.mjs has only a default export.)

  * There remain two serious defects when running in uncompiled mode:
    * It does not attempt to load msg/messages.js, causing startup to
      fail.
    * Module loading only works if there are no ES Modules; if there
      are, something goes wrong with base.js's attempt to sequence
      module loads causing goog.modules that import ES modules to get
      a null exports object for that import.  X-(

* fix(tests): Have playground.html load messages.js before generators

  This fixes the issue caused by missing messages when loading
  the generators.

* fix(tests): Move bootsrap calls to prepare.js

  Move the calls to goog.bootstrap from blockly.mjs to prepare.mjs.
  This is needed to work around a bug in the Cosure Library debug
  loader (https://github.com/google/closure-library/issues/1152).

  This gets a bit ugly because most of the code has to go in a
  <script> (because it needs goog.bootstrap, which was loaded by
  an earlier <script> tag).

* fix(documentation): Minor comment corrections for PR #5931
2022-02-23 10:30:21 +00:00

86 lines
3.2 KiB
JavaScript

/**
* @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 test/deps.js. To update
// deps.js, run `npm run build:deps`.
document.write('<script src="../../tests/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.blocks.all',
'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 {
// We need to load Blockly in compiled mode.
// Load blockly_compressed.js et al. using <script> tags.
document.write('<script src="../../blockly_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="../../blocks_compressed.js"></script>');
document.write('<script src="../../msg/messages.js"></script>');
}
})();